Can't add views or inflate within View Pager inside an Array Adapter

拟墨画扇 提交于 2019-12-02 21:59:53

问题


I used an external library called "Swipe Cards by Diolor" and I attached it to an Array Adapter. Within each card I also need to make a View Pager with bars showing which image the user is currently looking at. The issue I am having is that when I set an OnPageChangeListeneron the View Pager it calls methodonPageSelected and executed all the code within it. This involves a call to createBars (included below). However, everything in createBars is executed including the barHolder.removeAllView except for the li.inflate statements. I also tried using addView but a similar problem occurs. Please advise a method to work around this and if possible the resoan why it's happening. I have included code from the Array Adapter code for the createBars and getView methods. Let me know if some other piece of code would be helpful.

 private void createBars(int position, int numItems, LinearLayout barHolder) {

    barHolder.removeAllViews();
    Log.i("View pager listner", "method called");


    for (int i = 0; i < numItems; i++) {
        if (i == position) {
            li.inflate(R.layout.white_rectangle, barHolder);
        } else {
            li.inflate(R.layout.transparent_white_rectangle, barHolder);
        }
    }
}

public View getView(int position, View itemView, ViewGroup parent) {

    // all views required for a card
    final ProgressBar homeProgressBar;
    final TextView homeTimeLeft;
    TextView homeTitle;
    CardView homeItemHolder;

    HomeItem item = getItem(position);
    final int numItems = item.getItemImages().length;

    if (itemView == null) {
        itemView = li.inflate(R.layout.home_item, parent, false);
    }


    homeProgressBar = itemView.findViewById(R.id.home_progressbar);
    homeTimeLeft = itemView.findViewById(R.id.home_time_text);
    homeTitle = itemView.findViewById(R.id.home_card_title);
    homeViewPager = itemView.findViewById(R.id.home_view_pager);

    homeViewPager.setOffscreenPageLimit(6);

    timeLeftItem = item.getTimeLeftMiliseconds();
    homeTitle.setText(item.getTitle());

    HomeImageHolder viewPagerAdapter = new HomeImageHolder(context, item.getItemImages());
    homeViewPager.setAdapter(viewPagerAdapter);
    final LinearLayout barHolder = itemView.findViewById(R.id.home_bars);
    createBars(0, item.getItemImages().length, barHolder);

    homeViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Log.i("view pager listner", "it listned");
            createBars(position, numItems, barHolder);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    countDownTimer = new CountDownTimer(timeLeftItem, milisecondsInHours) {

        @Override
        public void onTick(long millisUntilFinished) {
            homeTimeLeft.setText(HelperMethods.milisecondsToDays(millisUntilFinished) + " Days "
                    + HelperMethods.milisecondsToHours(millisUntilFinished) + " Hours");
            homeProgressBar.setProgress(updateProgressBar(millisUntilFinished));

        }

        @Override
        public void onFinish() {
            homeTimeLeft.setText("0 Days 0 Hours");
            homeProgressBar.setProgress(updateProgressBar(0));

        }
    };
    countDownTimer.start();

    return itemView;
}

The inflator works when it's called outside the listener but not when it is called inside it. What would be the reasoning being this?

EDIT: I figured out that the class to which the array adapter is added, (Diolor's swipe cards) calls getView 4 times on the future objects in the array. Could this be a potential issue. I know that the reference to bar holder within the listener does not change for a given card. I used Logs and toString methods to confirm that the LinearLayout passed into the listner coincided with the barHolder that corresponds to the current card.

来源:https://stackoverflow.com/questions/51691464/cant-add-views-or-inflate-within-view-pager-inside-an-array-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!