In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to solve the conflict of Android ViewPager custom broadcast picture". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the conflict of Android ViewPager custom broadcast picture".
Be sure to transfer the ArrayList collection & Handler mechanism to the adapter, otherwise you can't complete the presentation or solve the sliding conflict. There's a lot of code, but it's easy to understand.
Layout layout inscription:
Activity is written inside:
Public class Frag_01 extends Fragment {private RadioGroup radioGroup; private ViewPager pager; @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate (R.layout.frag_01, container, false); / / find Viewpager control radioGroup = (RadioGroup) view.findViewById (R.id.radioGroup); pager = (ViewPager) view.findViewById (R.id.pager01) / / Image data customization-- > int arr [] = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5} under the drawable folder; final ArrayList list = new ArrayList (); for (int I = 0; I)
< arr.length; i++) { ImageView imageView = new ImageView(getActivity()); imageView.setImageResource(arr[i]); list.add(imageView); // 生成小圆点 RadioButton radioButton = new RadioButton(getActivity()); radioGroup.addView(radioButton); } // 默认选中第一个 radioGroup.check(radioGroup.getChildAt(0).getId()); // 适配器 pager.setAdapter(new MyPagerAdapter(list, handler)); // 页面切换监听 pager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { //根据页面索引控制小圆点切换 arg0 %= list.size(); radioGroup.check(radioGroup.getChildAt(arg0).getId()); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); //handler发送消息 handler.sendEmptyMessageDelayed(0, 3000); return view; } //Handler机制----------------------------------------- Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: //获取到下一页的条目--->Assign to the current entry int currentItem = pager.getCurrentItem () + 1; pager.setCurrentItem (currentItem); handler.sendEmptyMessageDelayed (0, 3000); break; default: break;}};};}
How to write the PagerAdapter adapter: (includes sliding conflict resolution)
Public class MyPagerAdapter extends PagerAdapter {private ArrayList list; private Handler handler; / / construct public MyPagerAdapter (ArrayList list, Handler handler) {super (); this.list = list; this.handler = handler;} @ Override public int getCount () {/ / set maximum return Integer.MAX_VALUE;} @ Override public boolean isViewFromObject (View arg0, Object arg1) {/ / current view and newly loaded view judge-- > true to destroy return arg0 = = arg1 } / / add view / /-this method contains sliding conflicts-@ Override public Object instantiateItem (ViewGroup container, int position) {position% = list.size (); ImageView imageView = list.get (position); imageView.setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {/ / get event int action = event.getAction () Switch (action) {case MotionEvent.ACTION_MOVE:// slide handler.removeCallbacksAndMessages (null); break; case MotionEvent.ACTION_DOWN:// press handler.removeCallbacksAndMessages (null); break; case MotionEvent.ACTION_CANCEL:// non-artificial operation handler.sendEmptyMessageDelayed (0, 3000); break; case MotionEvent.ACTION_UP:// lift handler.sendEmptyMessageDelayed (0, 3000); break;} return true }}); / / the acquired images are stored in the container container.addView (imageView); return imageView;} / / destroy view @ Override public void destroyItem (ViewGroup container, int position, Object object) {container.removeView ((View) object) }} Thank you for your reading. The above is the content of "how to solve the conflict of Android ViewPager Custom broadcast Picture". After the study of this article, I believe you have a deeper understanding of how to solve the conflict of Android ViewPager Custom broadcast Picture, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.