Android ViewPager Customize Carousel and Resolve Play Conflicts
- 2021-12-21 05:03:58
- OfStack
In this paper, we share the Android ViewPager custom carousel map and solve the playback conflict for your reference
First of all, introduce this small code under 1:
Notes comprehensive, easy to learn, suitable for beginners, self-made pictures! ! !
1 Be sure to set the ArrayList collection & Handler mechanism to the adapter, otherwise can not complete the display, also can not solve the sliding conflict, the code is a bit too much, but it is easy to understand ah
layout layout writing:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager01"
android:layout_width="match_parent"
android:layout_height="300dp" />
<RadioGroup
android:id="@+id/radioGroup"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</RadioGroup>
</LinearLayout>
Activity internal writing:
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);
// Looking for Viewpager Control
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
pager = (ViewPager) view.findViewById(R.id.pager01);
// Self-made picture data -->drawable Under the folder
int arr[] = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4, R.drawable.a5 };
final ArrayList<ImageView> list = new ArrayList<ImageView>();
for (int i = 0; i < arr.length; i++) {
ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(arr[i]);
list.add(imageView);
// Generate small dots
RadioButton radioButton = new RadioButton(getActivity());
radioGroup.addView(radioButton);
}
// The default is selected 1 A
radioGroup.check(radioGroup.getChildAt(0).getId());
// Adapter
pager.setAdapter(new MyPagerAdapter(list, handler));
// Page switching listening
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// Control dot switching according to page index
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 Send a message
handler.sendEmptyMessageDelayed(0, 3000);
return view;
}
//Handler Mechanism -----------------------------------------
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
// Get to the following 1 Entries for the page ---> Assign a value to the current entry
int currentItem = pager.getCurrentItem() + 1;
pager.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(0, 3000);
break;
default:
break;
}
};
};
}
How PagerAdapter adapter is written: (Includes sliding collision resolution)
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<ImageView> list;
private Handler handler;
// Structure
public MyPagerAdapter(ArrayList<ImageView> list, Handler handler) {
super();
this.list = list;
this.handler = handler;
}
@Override
public int getCount() {
// Set the maximum value
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// Current view and newly loaded view judgment --> Only when it is true can it be destroyed
return arg0 == arg1;
}
// Add View
// ---- Sliding collision is included in this method ----
@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 Events
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:// Slip
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_DOWN:// Press
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL:// Non-human operation
handler.sendEmptyMessageDelayed(0, 3000);
break;
case MotionEvent.ACTION_UP:// Lift
handler.sendEmptyMessageDelayed(0, 3000);
break;
}
return true;
}
});
// The obtained picture is 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);
}
}