我想将我的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 String TAG = ViewPagerAdapter.class.getSimpleName(); private int count; private int[] pageIDsArray; private TextToSpeech btnTTS; private TtsButton tTSBtn; String 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, ListarrayAtomicNum, List arrayName, List arraySymbol, List arrayFamily, List arrayPhase, List arrayColor, List arrayGroup, List arrayPeriod, List arrayBlock, List arrayProtons, List arrayNeutrons, List arrayElectrons, List arrayWeight, List arrayMelt, List arrayBoil, List 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, 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()"); } }); } @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, container, false); } else if (layoutColorId == 3) { itemView = inflater.inflate(R.layout.frame_learn_c, container, false); } else if (layoutColorId == 4) { itemView = inflater.inflate(R.layout.frame_learn_d, container, 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); // name textView 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.metamelt); // 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(String.valueOf(ElementStructure.arrayAtomicNum.get(position))); tvSymbol.setText(ElementStructure.arraySymbol.get(position)); tvWeight.setText(String.valueOf(ElementStructure.arrayWeight.get(position))); tvName.setText(ElementStructure.arrayName.get(position)); tvPeriod.setText(String.valueOf(ElementStructure.arrayPeriod.get(position))); tvBlock.setText(String.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(String.valueOf(ElementStructure.arrayNeutrons.get(position))); tvProtons.setText(String.valueOf(ElementStructure.arrayProtons.get(position))); tvElectrons.setText(String.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(String.valueOf(ElementStructure.arrayGroup.get(position))); } if (ElementStructure.arrayMelt.get(position) == 0) { tvMelt.setText("n/a"); } else { tvMelt.setText(String.valueOf(ElementStructure.arrayMelt.get(position))); } if (ElementStructure.arrayBoil.get(position) == 0) { tvBoil.setText("n/a"); } else { tvBoil.setText(String.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, String inputTxt) { super(); tTSCheck(inputTxt); } private void tTSCheck (String 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容器,最终的int位置)",如果条件是正确的.但我不确定这是不是最好的.