encrypt_x 2019-03-16 14:07 采纳率: 100%
浏览 1030
已采纳

Android Fragment切换导致listview有数据也不显示

网上的方法基本上都试过了,没效果。

适配器能进getView(),返回的count也正常、


```public class Index extends AppCompatActivity {
    boolean flag = true;//判断数据是否第一加载
    private List<listView> listView = new ArrayList<listView>();
    private ListFragment listFragment = new ListFragment();
    private MineFragment mineFragment = new MineFragment();
    private OrderFragment orderFragment = new OrderFragment();
    Fragment currentFragment = listFragment;
    FragmentManager fm = getSupportFragmentManager();
    Fragment f = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        //解析菜单文件
        initData();
        final ListViewAdapter listViewAdapter = new ListViewAdapter(Index.this,R.layout.listlayout,listView);
        final ListView dataList = findViewById(R.id.Data_list);
        dataList.setAdapter(listViewAdapter);
        //导航图片设置初始化
        ImageView imageView1 = findViewById(R.id.nav_index);
        ImageView imageView2 = findViewById(R.id.nav_order);
        ImageView imageView3 = findViewById(R.id.nav_mine);
        View.OnClickListener listener= new OnClickListener(){

            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.nav_index:
                        listViewAdapter.notifyDataSetChanged();
                        dataList.setAdapter(listViewAdapter);
                        showFragment(listFragment);
                        break;
                    case R.id.nav_mine:
                        showFragment(mineFragment);
                        break;
                    default:
                        showFragment(orderFragment);
                        break;
                }
            }
        };
        imageView1.setOnClickListener(listener);
        imageView2.setOnClickListener(listener);
        imageView3.setOnClickListener(listener);

    }
    private void showFragment(Fragment fragment){
        FragmentTransaction ft = fm.beginTransaction();
        if (currentFragment != fragment){//  判断传入的fragment是不是当前的currentFragmentgit
            ft.hide(currentFragment);//  不是则隐藏
            currentFragment = fragment;  //  然后将传入的fragment赋值给currentFragment
            if (!fragment.isAdded()){ //  判断传入的fragment是否已经被add()过
                ft.add(R.id.fragment,fragment).show(fragment).commit();
            }else{
                ft.show(fragment).commit();
            }
        }
    }

    private void initData() {
        listView list = new listView("系统提示","测试内容信息","2019-3-14",R.drawable.shop1);
        listView.add(list);
        listView list1 = new listView("二号餐厅","测试内容信息","2019-3-14",R.drawable.shop2);
        listView.add(list1);
    }
}
适配器代码
public class ListViewAdapter extends ArrayAdapter {
    private final int resourceId;
    public ListViewAdapter(Context context, int resource, List<listView> objects ) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        listView listView = (listView) getItem(position);// 获取当前项的Fruit实例
        View view = LayoutInflater.from(getContext()).inflate(resourceId,null);//实例化一个对象
        ImageView listImage = (ImageView) view.findViewById(R.id.shop_img);//获取该布局内的图片视图
        TextView listText = (TextView) view.findViewById(R.id.shop_name);//获取该布局内的文字视图
        listImage.setImageResource(listView.getImageId());//设置资源
        listText.setText(listView.getTitle());
        System.out.println("被调用");
        return view;
    }
}
listview的Xml
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/index_gray">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:background="@color/index_white"
        android:id="@+id/Data_list">

    </ListView>
    </LinearLayout>
</FrameLayout>
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/con_index"
    android:layout_width="match_parent"
    android:background="@color/index_gray"
    android:layout_height="match_parent">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:name="com.example.administrator.app.ListFragment"
        android:id="@+id/fragment"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:padding="5dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="@color/index_white">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <ImageView
            android:id="@+id/nav_index"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/shouye"
            android:layout_alignParentLeft="true"/>
        <ImageView
            android:id="@+id/nav_order"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/dingdan"
            android:layout_centerHorizontal="true"/>
        <ImageView
            android:id="@+id/nav_mine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/wode"
            android:layout_alignParentRight="true"/>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>
  • 写回答

3条回答 默认 最新

  • qq_21235081 2019-03-18 11:02
    关注

    你的listView是fragment里面的,你得把
    //解析菜单文件
    initData();
    final ListViewAdapter listViewAdapter = new ListViewAdapter(Index.this,R.layout.listlayout,listView);
    final ListView dataList = findViewById(R.id.Data_list);
    dataList.setAdapter(listViewAdapter);
    这些代码放到fragment里面去,不然 findViewById(R.id.Data_list) ;是找不到对应的listview的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大