如何在viewpager中设置多个View?

时间:2018-01-08 09:45:07

标签: android android-viewpager

最近我遇到了viewpager,但其中一个主要缺点是我们无法在viewpager中设置多个视图,所以我打算在屏幕上制作三个布局根据大小操作它但是它正在崩溃IndexOutOfBound异常,所以非常现在让我解释一下我的要求是什么,我需要每页三个视图,假设如果我列出大小为5我需要在第一页中有三个,而在下一个中我需要三个,现在让我发布我试过的内容到目前为止

这是PagerAdapter正在使用的:

public class SummaryViewPagerAdapter extends PagerAdapter {

    private List<SummaryModel>mSummarymodelist;
    private Context context;
    private LayoutInflater inflater;


    public SummaryViewPagerAdapter(Context context, List<SummaryModel> summaryModels ) {
          this.mSummarymodelist=summaryModels;
          this.context=context;
      }



    @Override
    public int getCount() {
        int count=(mSummarymodelist.size()+3-1)/3;
        return count;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
         return view == object;
    }
    private void populateTextView(TextView status_txt,TextView status_count,  String status, String color, RelativeLayout layout,String count) {

        status_txt.setText(status);
        status_count.setText(count);
        layout.setBackgroundColor(Color.parseColor(color));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.summary_viewholder,
                container, false);
        TextView status_txt= itemView.findViewById(R.id.txt_1);
        TextView count_txt=itemView.findViewById(R.id.count_1);
        RelativeLayout relativeLayout=itemView.findViewById(R.id.rl_1);
        position=position * 2;
        if (position <getRealCount()) {
            populateTextView(status_txt,count_txt,mSummarymodelist.get(position).getName(),mSummarymodelist.get(position).getColorCode(),relativeLayout, String.valueOf(mSummarymodelist.get(position).getCount()));
            status_txt.setVisibility(View.VISIBLE);
            count_txt.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);

        }
        else if(position==getRealCount()) {
            populateTextView(status_txt,count_txt,mSummarymodelist.get(position-1).getName(),mSummarymodelist.get(position-1).getColorCode(),relativeLayout, String.valueOf(mSummarymodelist.get(position-1).getCount()));
            status_txt.setVisibility(View.VISIBLE);
            count_txt.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);

        }
        else {
            status_txt.setVisibility(View.INVISIBLE);
            count_txt.setVisibility(View.INVISIBLE);
            relativeLayout.setVisibility(View.INVISIBLE);

        }
        position++;
        status_txt= itemView.findViewById(R.id.txt_2);
        count_txt=itemView.findViewById(R.id.count_2);
        relativeLayout=itemView.findViewById(R.id.rl_2);
        if (position < getRealCount()) {
            populateTextView(status_txt,count_txt, mSummarymodelist.get(position).getName(),mSummarymodelist.get(position).getColorCode(),relativeLayout, String.valueOf(mSummarymodelist.get(position).getCount()));
            status_txt.setVisibility(View.VISIBLE);
            count_txt.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);
        }
        else if(position==getRealCount()) {
            populateTextView(status_txt,count_txt,mSummarymodelist.get(position-2).getName(),mSummarymodelist.get(position-2).getColorCode(),relativeLayout, String.valueOf(mSummarymodelist.get(position-2).getCount()));
            status_txt.setVisibility(View.VISIBLE);
            count_txt.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);

        }
        else {
            status_txt.setVisibility(View.INVISIBLE);
            count_txt.setVisibility(View.INVISIBLE);
            relativeLayout.setVisibility(View.INVISIBLE);
        }
        position++;
        status_txt= itemView.findViewById(R.id.txt_3);
        count_txt=itemView.findViewById(R.id.count_3);
        relativeLayout=itemView.findViewById(R.id.rl_3);
        if (position <getRealCount()) {
            populateTextView(status_txt,count_txt, mSummarymodelist.get(position).getName(),mSummarymodelist.get(position).getColorCode(),relativeLayout, String.valueOf(mSummarymodelist.get(position).getCount()));
            status_txt.setVisibility(View.VISIBLE);
            count_txt.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);
        }
        else if(position==getRealCount()) {
            populateTextView(status_txt,count_txt,mSummarymodelist.get(position-3).getName(),mSummarymodelist.get(position-3).getColorCode(),relativeLayout, String.valueOf(mSummarymodelist.get(position-3).getCount()));
            status_txt.setVisibility(View.VISIBLE);
            count_txt.setVisibility(View.VISIBLE);
            relativeLayout.setVisibility(View.VISIBLE);

        }
        else {
            status_txt.setVisibility(View.INVISIBLE);
            count_txt.setVisibility(View.INVISIBLE);
            relativeLayout.setVisibility(View.INVISIBLE);
        }

       container.addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
    private int getRealCount() {
        return mSummarymodelist.size();
    }

}

但我无法实现这一点,就像我想要如何做到这一点,在此先感谢!!

1 个答案:

答案 0 :(得分:-1)

我没有获得getCount()方法,您是否尝试过调试并查看返回的内容? 你也可以试试这样的东西

@Override
public int getCount() {
    return (int) Math.ceil(mSummarymodelist.size() / 3.f);
}