努力让自己变得更优秀 在校学生  2019年05月11日
运行程序后首页不会加载,点击之后才会加载,请问这种情况怎么解决?
如图 ,不会发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>


...全文
1690 点赞 收藏 6
写回复
6 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告