viewpager
看到带view的控件基本都避免不了两个要素:适配器和数据源。
其中适配器是每个安卓开发人员最为头疼的东西。适配器怎么使用呢,一般都是要继承Adapter或其子类。今天来讲讲viewpager的使用。
- 首先布局中加入viewpager控件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
然后在活动中获取控件对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vp=(ViewPager)findViewById(R.id.vp);
}
- 构造适配器,创建个viewpager继承PagerAdatper,实现其方法。
class viewpageadapter extends PagerAdapter {
private List<View> views;//layout集合
public viewpageadapter(List<View> view) {
this.views = view;
}
@Override
public int getCount() {
return views.size();//返回layout数量
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(views.get(position));//直接添加进viewgroup中
return views.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
views.remove(views.get(position));
}
}
- 构造数据源
我们使用三个layout布局作为viewpager数据
在活动中获取三个layout的对象
private List<View> viewList=new ArrayList<>();//传layout集合给适配器
LayoutInflater layout = getLayoutInflater().from(this);
View layout1 = layout.inflate(R.layout.layout1, null);
View layout2 = layout.inflate(R.layout.layout2, null);
View layout3 = layout.inflate(R.layout.layout3, null);
viewList.add(layout1);
viewList.add(layout2);
viewList.add(layout3);
- 将数据放入适配器
viewpageadapter viewpageadapter=new viewpageadapter(viewList);//创建适配器对象
vp.setAdapter(viewpageadapter);//viewpager设置适配器
- 源码
package com.example.viewpage;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.content.Context;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<View> viewList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater layout = getLayoutInflater().from(this);
View layout1 = layout.inflate(R.layout.layout1, null);
View layout2 = layout.inflate(R.layout.layout2, null);
View layout3 = layout.inflate(R.layout.layout3, null);
viewList.add(layout1);
viewList.add(layout2);
viewList.add(layout3);
ViewPager vp=(ViewPager)findViewById(R.id.vp);
viewpageadapter viewpageadapter=new viewpageadapter(viewList);
vp.setAdapter(viewpageadapter);
}
class viewpageadapter extends PagerAdapter {
private List<View> views;//layout集合
public viewpageadapter(List<View> view) {
this.views = view;
}
@Override
public int getCount() {
return views.size();//返回layout数量
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(views.get(position));//直接
return views.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(views.get(position));//移除容器内layout
}
}
}
示例: