运行程序后首页不会加载,点击之后才会加载,请问这种情况怎么解决?

努力让自己变得更优秀 2019-05-11 09:33:09
如图 ,不会发gif,只能发静态图片了 ,大概就是这个意思:运行程序后不进行任何操作的时候,无法显示数据,点击屏幕之后才显示。
https://blog.csdn.net/weixin_43889378/article/details/90105812
部分代码:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<FrameLayout
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/nav_view"
android:orientation="vertical">
</FrameLayout>

<android.support.design.widget.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="labeled"
>
</android.support.design.widget.BottomNavigationView>

</RelativeLayout>


MainActivity.java

package com.hjl.topview2;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.hjl.topview2.view.HomeFragment;
import com.hjl.topview2.view.MineFragment;
import com.hjl.topview2.view.ProjectFragment;
import com.hjl.topview2.view.SystemFragment;

public class MainActivity extends AppCompatActivity {

private BottomNavigationView bottomNavigationView;
private HomeFragment homeFragment;
private SystemFragment systemFragment;
private ProjectFragment projectFragment;
private MineFragment mineFragment;
private Fragment[] fragments;
private int lastFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFragment();
}

private void initFragment() {
homeFragment = new HomeFragment();
systemFragment = new SystemFragment();
projectFragment = new ProjectFragment();
mineFragment = new MineFragment();
fragments = new Fragment[]{homeFragment, systemFragment, projectFragment, mineFragment};
lastFragment = 0;
//获取Fragment管理器 -> 产生FragmentTransaction对象 ->
getSupportFragmentManager().beginTransaction().replace(R.id.main_view, homeFragment).show(homeFragment).commit();
bottomNavigationView = findViewById(R.id.nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);
}

private BottomNavigationView.OnNavigationItemSelectedListener changeFragment = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
{
if (lastFragment != 0) {
switchFragment(lastFragment, 0);
lastFragment = 0;
}
return true;
}
case R.id.navigation_system:
{
if (lastFragment != 1) {
switchFragment(lastFragment, 1);
lastFragment = 1;
}
return true;
}
case R.id.navigation_project:
{
if (lastFragment != 2) {
switchFragment(lastFragment, 2);
lastFragment = 2;
}
return true;
}
case R.id.navigation_mine:
{
if (lastFragment != 3) {
switchFragment(lastFragment, 3);
lastFragment = 3;
}
return true;
}
}
return false;
}
};

private void switchFragment(int lastFragment, int index) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragments[lastFragment]);
if (!fragments[index].isAdded()) {
transaction.add(R.id.main_view, fragments[index]);
}
transaction.show(fragments[index]).commitAllowingStateLoss();
}
}


fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<android.support.v7.widget.Toolbar
android:id="@+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/margin_norma"
android:layout_marginEnd="@dimen/margin_norma">

<TextView
android:id="@+id/home_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title_home"
android:textColor="@color/colorWhite"
android:textSize="@dimen/toolbar_title_tx"
/>

<ImageView
android:id="@+id/home_search"
android:layout_width="@dimen/toolbar_iv_width"
android:layout_height="@dimen/toolbar_iv_height"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:contentDescription="@string/search"
android:scaleType="centerCrop"
android:src="@mipmap/search"
/>
</RelativeLayout>

</android.support.v7.widget.Toolbar>

<com.hjl.topview2.widget.RefreshLayout
android:id="@+id/home_swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/home_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:divider="#dddddd"
android:dividerHeight="3px"/>
</com.hjl.topview2.widget.RefreshLayout>


</LinearLayout>


...全文
1734 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
王能 2019-05-30
  • 打赏
  • 举报
回复
引用 5 楼 weixin_45069105 的回复:
同问,求大神出来解释
https://bbs.csdn.net/topics/392626334 这是他的原贴
weixin_45069105 2019-05-15
  • 打赏
  • 举报
回复
同问,求大神出来解释
  • 打赏
  • 举报
回复
引用 2 楼 王能 的回复:
代码都不需要看完,你默认进来没有调switchFragment1能显示出啥?
我用您教的解决SystemFragment的方法,解决了这个问题,但是不知道原因在哪
  • 打赏
  • 举报
回复
引用 2 楼 王能 的回复:
代码都不需要看完,你默认进来没有调switchFragment1能显示出啥?
在MainActivity.java的initFragment()方法中:
getSupportFragmentManager().beginTransaction().replace(R.id.main_view, homeFragment).show(homeFragment).commit();

不就相当于调了swithcFragment了吗?
王能 2019-05-13
  • 打赏
  • 举报
回复
代码都不需要看完,你默认进来没有调switchFragment1能显示出啥?
  • 打赏
  • 举报
回复
HomeFragmen.java

package com.hjl.topview2.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.hjl.topview2.R;
import com.hjl.topview2.adapter.ArticleAdapter;
import com.hjl.topview2.bean.Article;
import com.hjl.topview2.widget.RefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

private Toolbar toolbar;
private List<Article> articleList = new ArrayList<>();
private ArticleAdapter adapter;
private ListView listView;
private RefreshLayout refreshLayout;
private int pageNum = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
toolbar = view.findViewById(R.id.home_toolbar);
refreshLayout = view.findViewById(R.id.home_swipeRefreshLayout);
listView = view.findViewById(R.id.home_list_view);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
adapter = new ArticleAdapter(articleList);
listView.setAdapter(adapter);
initArticle(pageNum);
// 刷新事件
refreshLayout.setOnFlushListener(new RefreshLayout.OnFlushListener() {
@Override
public void onFlush() {
articleList.clear();
pageNum = 0;
initArticle(pageNum);
//同步ListView、adapter数据
adapter.notifyDataSetChanged();
refreshLayout.setFlushing(false);
}
});
// 加载事件
refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
@Override
public void onLoad() {
pageNum++;
initArticle(pageNum);
//同步ListView、adapter数据
adapter.notifyDataSetChanged();
refreshLayout.setLoading(false);
}
});
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

public void initArticle(final int pageNum) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
String url1 = "https://www.wanandroid.com/article/list/";
String url2 = "/json";
String string = url1 + (pageNum - 1) +
url2;
URL url = new URL(string);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
response.append(line);
}
parseJSONWithJSONObject(response.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}

private void parseJSONWithJSONObject(String jsonData) {
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONObject jsonObject1 = new JSONObject(jsonObject.getJSONObject("data").toString());
JSONArray jsonArray = new JSONArray(jsonObject1.getJSONArray("datas").toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Article article = new Article();
article.setAuthor(jsonObject2.getString("author"));
article.setChapterId(jsonObject2.getInt("chapterId"));
article.setChapterName(jsonObject2.getString("chapterName"));
article.setCollect(jsonObject2.getBoolean("collect"));
article.setCourseId(jsonObject2.getInt("courseId"));
article.setFresh(jsonObject2.getBoolean("fresh"));
article.setId(jsonObject2.getInt("id"));
article.setLink(jsonObject2.getString("link"));
article.setNiceDate(jsonObject2.getString("niceDate"));
article.setSuperChapterId(jsonObject2.getInt("superChapterId"));
article.setSuperChapterName(jsonObject2.getString("superChapterName"));
// article.setTags(jsonObject);
article.setTitle(jsonObject2.getString("title"));
articleList.add(article);
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧