ViewPager位置无法正常工作

问题描述 投票:0回答:1

[尝试实现视图分页器,默认情况下它应该仅排在第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

android onclick android-viewpager position
1个回答
0
投票

要进行这样的设置时手动设置位置:

viewPager.setCurrentItem(0); 
© www.soinside.com 2019 - 2024. All rights reserved.