<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
如何自动移动 ViewPager2 页面?
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
如何自动移动 ViewPager2 页面?
好的,这是示例...希望您觉得它有帮助
下面我为您提供了片段如何使用 viewpager2 自动更改的示例......
依赖项:
implementation 'com.google.android.material:material:1.1.0-alpha08'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta02'
主要活动
public class MainActivity extends AppCompatActivity {
int ci=0;
java.util.Timer timer;
ViewPager2 myViewPager2;
ViewPagerAdapter myAdapter;
private ArrayList<Fragment> arrayList = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewPager2 = findViewById(R.id.view_pager);
// add Fragments in your ViewPagerFragmentAdapter class
arrayList.add(new FirstFragment());
arrayList.add(new SecondFragment());
arrayList.add(new ThirdFragment());
myAdapter = new ViewPagerAdapter(getSupportFragmentManager(), getLifecycle());
// set Orientation in your ViewPager2
myViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
myViewPager2.setAdapter(myAdapter);
myViewPager2.setPageTransformer(new MarginPageTransformer(1500));
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
myViewPager2.post(new Runnable() {
@Override
public void run() {
Log.d("viewPager",""+ci);
myViewPager2.setCurrentItem(ci%7);
ci++;
}
});
}
},1000,3000);//changes 3 second you can change as per requirement
}}
活动主体
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这是适配器
public class ViewPagerAdapter extends FragmentStateAdapter {
private ArrayList<Fragment> arrayList = new ArrayList<>();
public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
case 2:
return new ThirdFragment();
}
return null;
}
@Override
public int getItemCount() {
return 3;
}}
最后但并非最不重要的一点是,您必须添加诸如 firstFragment、second 等片段...
微小的扩展可以帮助你
fun ViewPager2.enableAutoScroll(totalPages: Int): Timer {
val autoTimerTask = Timer()
var currentPageIndex = currentItem
autoTimerTask.schedule(object : TimerTask() {
override fun run() {
currentItem = currentPageIndex++
if (currentPageIndex == totalPages) {
autoTimerTask.cancel()
}
}
}, 0, DELAY_FOUR_SECONDS)
return autoTimerTask
}