代码不多,直接上代码吧,一共1个Activity,2个Adapter,3个他们对应的布局。
TextActivity:
public class TextActivity extends AppCompatActivity {
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text2);
ViewPager viewPager = findViewById(R.id.viewpager);
//定义一个视图集合(用来装左右滑动的页面视图)
final List viewList =new ArrayList<>();
//定义数据的集合
List list_string =new ArrayList<>();
View view = new View(TextActivity.this);
//Listview集合
for (int i =0; i <20; i++) {
viewList.add(view);
}
//每个Listview中的数据集合
for (int i =0; i <100; i++) {
list_string.add(i+"p");
}
HomeViewPagerAdapter adapter =new HomeViewPagerAdapter(viewList, list_string,TextActivity.this);
viewPager.setAdapter(adapter);
}
TextActivity对应的布局:activity_text,很简单,只有一个viewpager
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"
tools:context=".base.Text2Activity">
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
HomeViewPagerAdapter:viewpager的适配器
public class HomeViewPagerAdapterextends PagerAdapter {
private Listlist_views;
private Listlist_data;
private LinkedListmViewCache;
private ContextmContext;
private LayoutInflatermLayoutInflater;
HomeViewPagerAdapter(List list_views, List list_string, Context context) {
super();
this.list_views = list_views;
list_data = list_string;
this.mContext = context;
this.mLayoutInflater = LayoutInflater.from(mContext);
this.mViewCache =new LinkedList<>();
}
@Override
public int getCount() {
return list_views.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@Override
public ObjectinstantiateItem(ViewGroup container, int position) {
ViewHolder viewHolder;
View convertView;
if (mViewCache.size() ==0) {
viewHolder =new ViewHolder();
convertView =this.mLayoutInflater.inflate(R.layout.list_list, null, false);
viewHolder.listview = convertView.findViewById(R.id.listview);
convertView.setTag(viewHolder);
}else {
convertView =mViewCache.removeFirst();
viewHolder = (ViewHolder) convertView.getTag();
}
TextAdapter adapter =new TextAdapter(mContext, list_data);
viewHolder.listview.setAdapter(adapter);
container.addView(convertView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return convertView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View contentView = (View) object;
container.removeView(contentView);
this.mViewCache.add(contentView);
}
public final class ViewHolder {
public ListViewlistview;
}
HomeViewPagerAdapter对应的布局list_list,很简单,只有一个ListView
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
TextAdapter:viewpager中的Listview适配器
public class TextAdapter extends BaseAdapter {
private LayoutInflatermInflater;
private Listlist;
private ContextmContext;
public TextAdapter(Context context, List list) {
this.list = list;
this.mContext = context;
if (mInflater ==null) {
mInflater = LayoutInflater.from(context);
}
}
@Override
public int getCount() {
return list !=null && !list.isEmpty() ?list.size() :0;
}
@Override
public ObjectgetItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("SetTextI18n")
@Override
public ViewgetView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final String obj =list.get(position);
if (obj ==null) {
return convertView;
}
if (convertView ==null) {
holder =new ViewHolder();
convertView =mInflater.inflate(R.layout.item_adapter_text, null);
holder.tv_adapter = convertView.findViewById(R.id.tv_adapter);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_adapter.setText(list.get(position));
return convertView;
}
class ViewHolder {
private TextViewtv_adapter;
}
TextAdapter的布局:item_adapter_text ,很简单,只有一个textview
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white">