ViewPager
程序员文章站
2022-06-09 20:27:43
...
1.是什么:页面切换组件,可以实现左右滑动,从而切换不同的View。
2.在xml文件中建立。(必须纯手工打全)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_marginTop="30dp"
android:clipChildren="false" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:clipChildren="false" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
3.在Layout文件夹中建立三个视图界面。
4.在MainActivity.java中建立代码
package com.example.asdfa;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
private ViewPager viewpager1;
private List<View> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager1 = (ViewPager) findViewById(R.id.viewpager);
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.a,
null);
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.b,
null);
View view3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.c,
null);
list = new ArrayList<View>();
list.add(view1);
list.add(view2);
list.add(view3);
viewpager1.setAdapter(new MyAdapter());
}
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
((ViewPager) container).removeView(list.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return super.getPageTitle(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).addView(list.get(position));
return list.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
}
}
6
viewpager实现界面中间的间隔空隙如下实现:
viewpager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_margin));
该命令在初始界面打。后面的id在dimen中添加间隔为20dip
viewpager获取当前的页面如下:
Toast.makeText(TestLieKongActivity.this, "选择"+viewpager.getCurrentItem(), 2000).show();
推荐阅读
-
Android利用ViewPager实现滑动广告板实例源码
-
Android编程开发ScrollView中ViewPager无法正常滑动问题解决方法
-
Android Studio使用ViewPager+Fragment实现滑动菜单Tab效果
-
Android中DrawerLayout+ViewPager滑动冲突的解决方法
-
android 中viewpager+fragment仿微信底部TAG完美渐变
-
Android 在viewPager中双指缩放图片双击缩放图片单指拖拽图片的实现思路
-
ViewPager实现带引导小圆点与自动跳转的引导界面
-
Android中Viewpager禁止滑动的实现
-
Android 实现ViewPager边界回弹效果实例代码
-
使用ViewPager实现高仿launcher左右拖动效果