简体   繁体   中英

ViewPager position is not working properly

Trying to implement view pager , by default it should take 0th position only but here it is taking 0th position and 1st position . I'm expecting only 0th position for the first time

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 );
        }
    }

Trying to implement view pager , by default it should take 0th position only but here it is taking 0th position and 1st position . I'm expecting only 0th position for the first time

当你想这样设置时手动设置位置:

viewPager.setCurrentItem(0); 

Viewpager making the 0th position and cache next one. Use this method to change it's default behavior.

mViewPager.setOffscreenPageLimit(int);

you can check this Answer for more detail.

使用 postdelayed 设置视图分页器中的当前项

viewPager.postDelayed(() -> viewPager.setCurrentItem(position, true), 100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM