原作者地址:http://blog.csdn.net/u011418943/article/details/78493002
这里是自己使用时,写下的记录。为今后所用
注:只能使用Fragment,否则不能滑动,也加载不出页面!
1、3个要展示的pager.xml X3页面
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="3"
android:gravity="center"
android:textSize="40dp"/>
</FrameLayout>
2、给viewpager设置adapter ViewPagerAdapter
public class ViewPagerAdapter extends PagerAdapter {
private String TAG="qwe";
private Context context;
private List<Bean> list=new ArrayList<>();
public ViewPagerAdapter(Context context, List<Bean> list) {
super();
this.context=context;
this.list=list;
}
public ViewPagerAdapter() {
super();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = list.get(position).getView();
Log.d(TAG, "instantiateItem: "+position);
container.addView(view);
// Log.i(TAG, NAME + "--instantiateItem++container:" + container.getChildCount() + "++position:" + position);
return list.get(position).getView();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
}
3、调用
public class huadong extends AppCompatActivity {
private TabIndicator tabIndicator;
private ViewPager viewPager;
private List<String> mTitle=new ArrayList<>();
private ViewPagerAdapter viewPagerAdapter;
private List<Bean> viewList = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.huadong);
initview();
initdate();
}
private void initview() {
tabIndicator=findViewById(R.id.line_indicator);
viewPager=findViewById(R.id.viewpager);
}
private void initdate() {
mTitle.add("qqqq1");
mTitle.add("qqqq2");
mTitle.add("qqqq3");
mTitle.add("qqqq4");
LayoutInflater layoutInflater = getLayoutInflater();
View view1 = layoutInflater.inflate(R.layout.page1, null,false);
View view2 = layoutInflater.inflate(R.layout.page2, null,false);
View view3 = layoutInflater.inflate(R.layout.page3, null,false);
Bean bean=new Bean();
bean.setView(view1);
viewList.add(bean);
Bean bean1=new Bean();
bean1.setView(view2);
viewList.add(bean1);
Bean bean2=new Bean();
bean2.setView(view3);
viewList.add(bean2);
viewPagerAdapter=new ViewPagerAdapter(huadong.this,viewList);
viewPager.setAdapter(viewPagerAdapter);
// 设置 viewpager的切换速度,这样点击的时候比较自然
tabIndicator.setViewPagerSwitchSpeed(viewPager,600);
tabIndicator.setTabData(viewPager,mTitle, new TabIndicator.TabClickListener() {
@Override
public void onClick(int position) {
//顶部点击的方法公布出来
viewPager.setCurrentItem(position);
}
});
}
}
4、主布局文件huadong.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.zhengsr.viewpagerlib.indicator.TabIndicator
android:id="@+id/line_indicator"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/black_ff"
app:tab_color="@color/white"
app:tab_width="25dp"
app:tab_height="5dp"
app:tab_text_default_color="@color/white_ff"
app:tab_text_change_color="@color/white"
app:tab_show="true"
app:tab_text_type="normaltext"
app:tab_textsize="16sp"
app:visiabel_size="3"
app:tap_type="tri">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="heel" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="heel" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="heel" />
</com.zhengsr.viewpagerlib.indicator.TabIndicator>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewpager">
</android.support.v4.view.ViewPager>
</LinearLayout>
5、总结
自己用 LinearLayout写要显示的布局,结果一直显示不出。
不能一直写,写完想一下
导入:
compile 'com.github.LillteZheng:ViewPagerHelper:v0.5'
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}