Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何在ViewPager上进行循环滚动?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想设置我的ViewPager做圆形滚动.我希望第一页能够滚动到第2页和最后一页.而且我想我的最后一页滚动到[最后一页-1]和第一页.我已经尝试了,然我不知道什么时候调用我创建的方法.在ViewPager中似乎没有处理这种事情的方法,所以我创建了以下.
public ViewPagerAdapter(final ViewPager pager,int... pagEIDs) {
        super();
        int actualNoOfIDs = pagEIDs.length;
        count = actualNoOfIDs + 2;
        Log.d(tag,"actualNoOfIDs: " + actualNoOfIDs +
                "count: " + count);

        pagEIDsArray = new int[count];
        for (int i = 0; i < actualNoOfIDs; i++) {
            pagEIDsArraY[ i + 1] = pagEIDs[i];
        }
        pagEIDsArray[0] = pagEIDs[actualNoOfIDs - 1];
        pagEIDsArraY[count - 1] = pagEIDs[0];

        Log.d(tag,"actualNoOfIDs#2: " + actualNoOfIDs +
                "count#2: " + count);
        pager.setOnPagechangelistener(new OnPagechangelistener() {
            @Override
            public void onPageSELEcted(int position) {
                int PageCount = getCount();
                if (position == 0) {
                    pager.setCurrentItem(PageCount - 2,falsE);
                } else if (position == PageCount - 1) {
                    pager.setCurrentItem(1,falsE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int statE) {
                // TODO Auto-generated method stub
                Log.d(tag,"onPageScrollStateChanged()");
//              if (state == ViewPager.SCROLL_STATE_IDLE) { 
//                  int PageCount = getCount(); 
//                  int currentItem = pager.getCurrentItem(); 
//                  if (currentItem == 0) { 
//                      pager.setCurrentItem(PageCount - 2,falsE); 
//                  } else if (currentItem == PageCount - 1) { 
//                      pager.setCurrentItem(1,falsE); 
//                  } 
//              } 

            }

            @Override
            public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {
                // TODO Auto-generated method stub
                Log.d(tag,"onPageScrolled()");

            }
        });
    }

我的整个代码很冗长,但如果它有助于我可以发布.

public class ViewPagerAdapter extends PagerAdapter {
    public static @R_618_10495@ng TAG = ViewPagerAdapter.class.getSimplename();

    private int count;
    privatE int[] pagEIDsArray;

    private TextToSpeech btnTTS;
    private TtsButton tTSBtn;

    @R_618_10495@ng inputTxt;
    Context context;
    View itemView;

    TextView tvnumber; // container for atomic number
    TextView tvSymbol; // container for symbol
    TextView tvWeight; // container for weight
    TextView tvName; // container for name
    TextView tvGroup; // container for group
    TextView tvPeriod; // container for period
    TextView tvBlock; // container for block
    TextView tvFamily; // container for family
    TextView tvColor; // container for color
    TextView tvPhase; // container for phase
    TextView tvMelt; // container for melTing point
    TextView tvBoil; // container for boiling point
    TextView tvNeutrons; // container for neutrons
    TextView tvProtons; // container for protons
    TextView tvElectrons; // container for electrons
    TextView tvUrl; // container for electrons

    public ViewPagerAdapter(Context context,List<Integer> arrayAtomicNum,List<@R_618_10495@ng> arrayName,List<@R_618_10495@ng> arraySymbol,List<@R_618_10495@ng> arrayFamily,List<@R_618_10495@ng> arrayPhase,List<@R_618_10495@ng> arrayColor,List<Integer> arrayGroup,List<Integer> arrayPeriod,List<@R_618_10495@ng> arrayBlock,List<Integer> arrayProtons,List<Integer> arrayNeutrons,List<Integer> arrayElectrons,List<Double> arrayWeight,List<Double> arraymelt,List<Double> arrayBoil,List<@R_618_10495@ng> arrayUrl) {    
        this.context = context;
        ElementStructure.arrayAtomicNum = arrayAtomicNum;
        ElementStructure.arrayName = arrayName;
        ElementStructure.arraySymbol = arraySymbol;
        ElementStructure.arrayFamily = arrayFamily;
        ElementStructure.arrayPhase = arrayPhase;
        ElementStructure.arrayColor = arrayColor;
        ElementStructure.arrayGroup = arrayGroup;
        ElementStructure.arrayPeriod = arrayPeriod;
        ElementStructure.arrayBlock = arrayBlock;
        ElementStructure.arrayProtons = arrayProtons;
        ElementStructure.arrayNeutrons = arrayNeutrons;
        ElementStructure.arrayElectrons = arrayElectrons;
        ElementStructure.arrayWeight = arrayWeight;
        ElementStructure.arraymelt = arraymelt;
        ElementStructure.arrayBoil = arrayBoil;
        ElementStructure.arrayUrl = arrayUrl;
    }


    public ViewPagerAdapter(final ViewPager pager,"onPageScrolled()");

            }
        });
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return ElementStructure.arrayAtomicNum.size();

    }

    @Override
    public Boolean isViewFromObject(View view,Object object) {
        // TODO Auto-generated method stub
        return view == ((RelativeLayout) object);
    }

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

        LayoutInflater inflater = (LayoutInflater) context
             .getSystemservice(Context.LAYOUT_INFLATER_serviCE);

        int layoutColorId = ElementStructure.arrayLayoutColor.get(position);

        if (layoutColorId == 1) {
            itemView = inflater.inflate(R.layout.frame_learn_a,container,falsE);
        } else if (layoutColorId == 2) {
            itemView = inflater.inflate(R.layout.frame_learn_b,falsE);
        } else if (layoutColorId == 3) {
            itemView = inflater.inflate(R.layout.frame_learn_c,falsE);
        } else if (layoutColorId == 4) {
            itemView = inflater.inflate(R.layout.frame_learn_d,falsE);    
        }

        Button btnSpeak = (Button)itemView.findViewById(R.id.btnaudio);

        btnSpeak.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                inputTxt = ElementStructure.arrayName.get(position);
                tTSBtn = new TtsButton(this,inputTxt); 
            }

        });

        // atomic number textView
        tvnumber = (TextView)itemView.findViewById(R.id.Metanumber);

        // symbol textView
        tvSymbol = (TextView)itemView.findViewById(R.id.Metasymbol);

        // weight textView
        tvWeight = (TextView)itemView.findViewById(R.id.Metaweight);

        // @R_278_4687@tView
        tvName = (TextView)itemView.findViewById(R.id.MetaName);

        // group textView
        tvGroup = (TextView)itemView.findViewById(R.id.Metagroup);

        // period textView
        tvPeriod = (TextView)itemView.findViewById(R.id.Metaperiod);

        // block textView
        tvBlock = (TextView)itemView.findViewById(R.id.Metablock);

        // family textView
        tvFamily = (TextView)itemView.findViewById(R.id.Metafamily);

        // color textView
        tvColor = (TextView)itemView.findViewById(R.id.Metacolor);

        // phase textView
        tvPhase = (TextView)itemView.findViewById(R.id.MetaphasE);

        // melTing point textView
        tvMelt = (TextView)itemView.findViewById(R.id.@L_251_9@melt);

        // boiling point textView
        tvBoil = (TextView)itemView.findViewById(R.id.Metaboil);

        // neutrons textView
        tvNeutrons = (TextView)itemView.findViewById(R.id.Metaneutrons);

        // protons textView
        tvProtons = (TextView)itemView.findViewById(R.id.Metaprotons);

        // electrons textView
        tvElectrons = (TextView)itemView.findViewById(R.id.Metaelectrons);

        // url textView
        tvUrl = (TextView)itemView.findViewById(R.id.Metaurl);

        // capture position and set to the TextViews
        tvnumber.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayAtomicNum.get(position)));
        tvSymbol.setText(ElementStructure.arraySymbol.get(position));
        tvWeight.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayWeight.get(position)));
        tvName.setText(ElementStructure.arrayName.get(position));
        tvPeriod.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayPeriod.get(position)));
        tvBlock.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayBlock.get(position)));
        tvFamily.setText(ElementStructure.arrayFamily.get(position));
        tvColor.setText(ElementStructure.arrayColor.get(position));
        tvPhase.setText(ElementStructure.arrayPhase.get(position));
        tvNeutrons.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayNeutrons.get(position)));
        tvProtons.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayProtons.get(position)));
        tvElectrons.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayElectrons.get(position)));
        tvUrl.setText(ElementStructure.arrayUrl.get(position));

        // capture position,adjust for 0 value cases
        if (ElementStructure.arrayGroup.get(position) == 0) {
            tvGroup.setText("n/a");
        } else {
            tvGroup.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayGroup.get(position)));
        }

        if (ElementStructure.arraymelt.get(position) == 0) {
            tvMelt.setText("n/a");
        } else {
            tvMelt.setText(@R_618_10495@ng.valueOf(ElementStructure.arraymelt.get(position)));
        }

        if (ElementStructure.arrayBoil.get(position) == 0) {
            tvBoil.setText("n/a");
        } else {
            tvBoil.setText(@R_618_10495@ng.valueOf(ElementStructure.arrayBoil.get(position)));
        }

        // add fragments to container (ViewPager)
        ((ViewPager) container).addView(itemView);
        return itemView;

    }

    @Override
    public void destroyItem(ViewGroup container,int position,Object object) {
        Log.d(tag,"destroyItem()");
        // remove fragments from container (ViewPager)
        ((ViewPager) container).removeView((RelativeLayout) object);
    }


    @Override
    public void finishupdate(View container) {
        // TODO Auto-generated method stub
        Log.d(tag,"finishupdate()");
    }

    @Override
    public void restoreState(Parcelable state,ClassLoader loader) {
        // TODO Auto-generated method stub
        Log.d(tag,"restoreState()");
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        Log.d(tag,"saveState()");
        return null;
    }

    @Override
    public void startupdate(View container) {
        // TODO Auto-generated method stub
        Log.d(tag,"startupdate()");
    }




    public class TtsButton extends Activity implements OnInitListener {

        public TtsButton(OnClickListener onClickListener,@R_618_10495@ng inputTxt) {
            super();
            tTscheck(inputTxt);
        }

        private void tTscheck (@R_618_10495@ng inputTxt) {

            int resultCodecheck = TextToSpeech.ENGIne.checK_VOICE_DATA_PASS;
            if (resultCodecheck == 1) {
                btnTTS = new TextToSpeech(context,this);
            } else {
                Intent installTTSFiles = new Intent(); //missing data,install it
                installTTSFiles.setAction(TextToSpeech.ENGIne.ACTION_INSTall_TTS_DATA);
                startActivity(installTTSFiles);
            }
        }

        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status == TextToSpeech.succesS)
            {
                Log.i(tag,"TTS INIT: succesS");
                btnTTs.setLanguage(Locale.US);
                btnTTs.speak(inputTxt,TextToSpeech.QUEUE_FLUSH,null);
            }
            else if(status == TextToSpeech.ERROR)
            {
                Log.e(tag,"TTS INIT: ERROR");
            }
        }

        @Override
        public void onPause() {
            if (btnTTS != null) {
                btnTTs.stop();
                btnTTs.shutdown();
            }
            super.onPause();
        }

    } //end embedded class

} //end ViewPagerAdapter

提前致谢.我正在虑尝试使用动作手势,但是,我真的不明白为什么我的方法不会工作很长时间,我可以把它称为正确的位置.我最初的想法是在每次位置发生变化时,在“Object instantiateItem(ViewGroup container,final int position)”中调用它,如果条件是正确的.但我不知道这是否最好.

解决方法

@H_696_13@ 这是一个没有假页面的@L_874_31@案,它的作用就像一个魅力:
public class CircularViewPagerHandler implements ViewPager.onPagechangelistener {
    private ViewPager   mViewPager;
    privatE int         mCurrentPosition;
    privatE int         mScrollState;

    public CircularViewPagerHandler(final ViewPager viewPager) {
        mViewPager = viewPager;
    }

    @Override
    public void onPageSELEcted(final int position) {
        mCurrentPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(final int statE) {
        handleScrollState(statE);
        mScrollState = state;
    }

    private void handleScrollState(final int statE) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            setNextItemIfNeeded();
        }
    }

    private void setNextItemIfNeeded() {
        if (!isScrollStateSettling()) {
            handleSetNextItem();
        }
    }

    private Boolean isScrollStateSettling() {
        return mScrollState == ViewPager.SCROLL_STATE_SETTLING;
    }

    private void handleSetNextItem() {
        final int lastPosition = mViewPager.getAdapter().getCount() - 1;
        if(mCurrentPosition == 0) {
            mViewPager.setCurrentItem(lastPosition,falsE);
        } else if(mCurrentPosition == lastPosition) {
            mViewPager.setCurrentItem(0,falsE);
        }
    }

    @Override
    public void onPageScrolled(final int position,final float positionOffset,final int positionOffsetPixels) {
    }
}

您只需将其设置为您的ViewPager作为onPagechangelistener就可以了:

viewPager.setOnPagechangelistener(new CircularViewPagerHandler(viewPager));

为了避免在您的ViewPager的“结束”上发出蓝色的光芒,您应该将此行应用于您放置ViewPager的xml:

android:overScrollmode="never"

大佬总结

以上是大佬教程为你收集整理的android – 如何在ViewPager上进行循环滚动?全部内容,希望文章能够帮你解决android – 如何在ViewPager上进行循环滚动?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。