在浏览视图时,ViewPager卡住

时间:2016-10-15 13:19:08

标签: android android-fragments android-viewpager android-tablayout

我已经创建了一个带有tabslayout的viewpager。viewpager的每个视图都包含了片段内的recyclelerview。我遇到的问题是,当我滑动视图时它会卡住,同时以这样的方式滑动,即视图和视图没有变化我必须多次滑动才能更改视图。看起来viewpager没有检测到滑动事件。但是如果我在下面注释掉包含设置适配器的两行,那么我不会遇到这样的问题并且滑动是顺利的快。

 PlanListAdapter mAdapter = new PlanListAdapter(browsePlanModelList, getActivity());
 rvBrowsePlanList.setAdapter(mAdapter);

我哪里出错了,我该如何解决?

下面是片段和适配器的代码 -

public class FullTimeFragment extends Fragment {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_browse_plan_list, container, false);

        ArrayList<BrowsePlanModel> browsePlanModelList = (ArrayList<BrowsePlanModel>) getArguments().getSerializable(AppConstant.BROWSEPLANlIST);

        RecyclerView rvBrowsePlanList = (RecyclerView) view.findViewById(R.id.rvBrowsePlanList);
        //rvBrowsePlanList.addItemDecoration(new SimpleDividerIncludingLastItemDecoration(getResources()));
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        rvBrowsePlanList.setLayoutManager(mLayoutManager);
        rvBrowsePlanList.setItemAnimator(new DefaultItemAnimator());

        if(browsePlanModelList!=null)
        {
            BrowsePlanListAdapter mAdapter = new BrowsePlanListAdapter(browsePlanModelList, getActivity());
            rvBrowsePlanList.setAdapter(mAdapter);
        }

        return view;
    }
}

适配器

public class PlanListAdapter extends RecyclerView.Adapter<PlanListAdapter.MyViewHolder> {


    private List<BrowsePlanModel> planList;
    Context mContext;

    public PlanListAdapter(List<BrowsePlanModel> planList, Context mContext) {
        this.mContext = mContext;
        this.planList = planList;
    }

    @Override
    public PlanListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sffs_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(PlanListAdapter.MyViewHolder holder, int position) {


        holder.xyz.setText(planList.get(position).getxyx());
        holder.abc.setText(planList.get(position).getabc());
    }

    @Override
    public int getItemCount() {
        return planList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView xyz, abc;


        public MyViewHolder(View itemView) {
            super(itemView);

            xyz = (TextView) itemView.findViewById(R.id.xyz);
            abc = (TextView) itemView.findViewById(R.id.abc);

        }
    }
}

2 个答案:

答案 0 :(得分:1)

由于高分辨率图像,可能会发生这种情况。如果使用高分辨率图像,则在滑动时viewPager会卡住。尝试使用低分辨率图像。

答案 1 :(得分:1)

使用

myViewPager.setOffscreenPageLimit(number of pages in your viewpager minus 1);

如果不这样做,那么当前不可见的页面将被销毁,并且每次刷卡时都会重新创建。如果每个页面都从数据库填充RecyclerView等,这将导致巨大的延迟。