首先创建一个新的empty activity
然后右键com.example.text,点击new、java class创建一个新的class命名为adapter来做viewpaper2的适配器
再在com.example.text里创建几个fragment.
准备工作结束。
下面按喜好修改布局以及重写适配器(adapter)。
布局参考
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager"
android:layout_weight="1">
</androidx.viewpager2.widget.ViewPager2>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/l1">
<ImageView
android:id="@+id/image1" //这里命名是为了把他当做按钮,大家也可以把别的东西
android:layout_weight="1" 当做按钮
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
>
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/l2">
<ImageView
android:id="@+id/image2" //同上
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher">
</ImageView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
fragment布局随便写一下就行,滑动能区分即可
MainActivity.java
package com.example.text;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imag1,imag2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a();
init();
imag1.setOnClickListener(this::onClick);
imag2.setOnClickListener(this::onClick);
}
private void init() {
imag1 = findViewById(R.id.image1);
imag2 = findViewById(R.id.image2);
}
public void a() {
List<Fragment> list=new ArrayList<>();
list.add(0,new BlankFragment()); //把之前创建的fragment添加进去
list.add(1,new BlankFragment2());
ViewPager2 viewPager2 =findViewById(R.id.viewpager);
viewPager2.setAdapter(new adapter(getSupportFragmentManager(),getLifecycle(),list));
}
@Override
public void onClick(View view) {
ViewPager2 viewPager2 =findViewById(R.id.viewpager);
switch (view.getId()){
case R.id.image1:
viewPager2.setCurrentItem(0,true); //true滑动跳转,false不滑动跳转
break;
case R.id.image2:
viewPager2.setCurrentItem(1,true);
}
}
}
adapter.java
package com.example.text;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.ArrayList;
import java.util.List;
public class adapter extends FragmentStateAdapter {
List<Fragment> list = new ArrayList<>();
public adapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> list){
super(fragmentManager,lifecycle);
this.list = list;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return list.get(position);
}
@Override
public int getItemCount() {
return list.size();
}
}
展示