RecyclerView- getItemCount()不被调用

时间:2019-05-29 04:03:22

标签: android android-recyclerview

有一个FragmentPagerAdapter和三个Fragment。首先,RecycleView行为正常。但是当我切换到没有数据的页面并返回到有数据的页面时,不会调用getItemCount(),因此视图为空。

[rankingListFragment1(mList.size()== 50)-> rankingListFragment2(mList.size()== 0)-> rankingListFragment2(mList.size()== 50)]

在Android 9.0中运行

FragmentPagerAdapter之类的

public class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            if (rankingListFragment1 == null) {
                rankingListFragment1 = new ChildFragment();
            }
            return rankingListFragment1;
        } else if (position == 1) {
            if (rankingListFragment2 == null) {
                rankingListFragment2 = new ChildFragment();
            }
            return rankingListFragment2;
        } else if (position == 2) {
            if (rangkingListFragment3 == null) {
                rangkingListFragment3 = new ChildFragment();
            }
            return rangkingListFragment3;
        } else {
            if (rangkingListFragment4 == null) {
                rangkingListFragment4 = new ChildFragment();
            }
            return rangkingListFragment4;
        }
    }
}

ChildFragment中的CharmAdapter:

public class CharmAdapter extends RecyclerView.Adapter<CharmViewHolder> {

   private ArrayList<Charm> mlist;

    public CharmAdapter(ArrayList<Charm> mlist) {
        if(this.mlist==null){
            this.mlist = new ArrayList<>();
        }
        this.mlist.clear();
        this.mlist.addAll(mlist);
    }

    public void setData(ArrayList<Charm> list){
        this.mlist.clear();
        if(list!=null&&list.size()>0){
            mlist.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && type.equals("3")) {
            return 1;
        }else if (position == 0 && comparison.equals("me")){
            return 3;
        }else {
            return 2;
        }
    }

    @Override
    public CharmViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CharmViewHolder holder = null;
        if (viewType == 1) {
            View view = getActivity().getLayoutInflater().inflate(R.layout.item, parent, false);
            holder = new CharmViewHolder(view);
        }else if (viewType == 3){
            View view = getActivity().getLayoutInflater().inflate(R.layout.item1, parent, false);
            holder = new CharmViewHolder(view);
        } else{
            View view = getActivity().getLayoutInflater().inflate(R.layout.item2, parent, false);
            holder = new CharmViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(CharmViewHolder holder, final int position) {
        int a = position;
        if (getItemViewType(position) == 1) {
            ...
        } else if (getItemViewType(position) == 2) {
            ...
        } else if (getItemViewType(position) == 3){
            ...
        }
    }

    @Override
    public int getItemCount() {
        if (type.equals("3")){
            if (mlist.size() >= 3)
                return mlist.size() - 2;
            else
                return 1;
        }else{
            return mlist.size();
        }
    }
}

在此处设置数据

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(ListRes event) {
    if (event != null && event.getCode() == 1) {
        list.clear();
        if (event.getList() != null) {
            list.addAll(event.getList());
        }

        listview.post(()->{
            adpter.setData(list);
        });
    }
}

RecyclerView初始化代码:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
if (list == null) {
    list = new ArrayList<>();
    adpter = new MyAdapter(list);
    recyclerView.setAdapter(adpter);
}

0 个答案:

没有答案