ViewPager位置无法正常工作

时间:2020-01-07 05:21:30

标签: android onclick android-viewpager position

尝试实现视图分页器,默认情况下它应该仅排在第0位,但是在这里它排在第0位和第1位。 我第一次期望只排名第0位

public class ViewCardAdapter extends PagerAdapter {

    public static String cardNo;
    private Context context;
    private List<CardDetails> cardDetailsList;
    private ViewCardBinding viewCardBinding;

    public ViewCardAdapter (Context context, List<CardDetails> cardDetailsList, ViewCardBinding viewCardBinding) {
        this.context = context;
        this.cardDetailsList = cardDetailsList;
        this.viewCardBinding = viewCardBinding;
    }

    @Override
    public int getCount () {
        return cardDetailsList.size ();
    }

    @Override
    public boolean isViewFromObject (@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem (@NonNull ViewGroup container, int position) {

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate (R.layout.mycard_adapter_layout, null);


        container.addView (view);
        bind (cardDetailsList.get (position), view, position);
        return view;
    }

    @Override
    public void destroyItem (ViewGroup container, int position, @NonNull Object object) {
        container.removeView ((LinearLayout) object);
    }

    private void bind (CardDetails item, View view, int position) {
        TextView tv_cardNo = view.findViewById (R.id.tv_card_no);
        TextView tv_expiryDate = view.findViewById (R.id.tv_expire_date);
        EditText edt_card_status = view.findViewById (R.id.et_card_status);
        EditText edt_card_no = view.findViewById (R.id.et_card_no);

        try {
            item = cardDetailsList.get (position);
            String cardStatus = item.getCardStatus ();
            if (cardStatus.equals (ConstantFields.SUSPENDED) ||cardStatus.equals (ConstantFields.INACTIVE) ) {
                viewCardBinding.btnSuspended.setText (context.getResources ().getString (R.string.btn_activie));
            } else if (cardStatus.equals (ConstantFields.ACTIVE)) {
                viewCardBinding.btnSuspended.setText (context.getResources ().getString (R.string.suspend));
            }
            cardNo = String.valueOf (item.getCardNumber ());
            cardStatus = item.getCardStatus ();
            tv_cardNo.setText (cardNo);
            tv_expiryDate.setText (item.getCardExpiryDate ());
            edt_card_status.setText (cardStatus);
            edt_card_no.setText (cardNo);

        } catch (Exception e) {
            Log.e (TAG, "bind: ", e );
        }
    }

尝试实现视图分页器,默认情况下它应该仅排在第0位,但是在这里它排在第0位和第1位。 我第一次期望只排名第0位

2 个答案:

答案 0 :(得分:0)

要进行如下设置时手动设置位置:

viewPager.setCurrentItem(0); 

答案 1 :(得分:0)

Viewpager排在第0位并缓存下一个。 使用此方法可以更改其默认行为。

mViewPager.setOffscreenPageLimit(int);

您可以检查此DataFrame.pivot以获得更多详细信息。