标签:android界面左右滑动 view左右滑动 左右切换 全屏幕滑动切换
首先要说的是,界面滑动,就需要多个view来进行切换,实际上可以用一个ArrayList<View> pageViews来保存这些view的信息,然后进行切换
LayoutInflater inflater = getLayoutInflater(); pageViews = new ArrayList<View>(); pageViews.add(inflater.inflate(R.layout.item01, null)); pageViews.add(inflater.inflate(R.layout.item02, null)); pageViews.add(inflater.inflate(R.layout.item03, null));
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/feature_guide_0" > </ImageView> </LinearLayout> </LinearLayout>
这里不得不说必须定义一个class GuidPageChangeListener 继承 OnPageChangeListener的接口来实现view之间的切换
public void onPageSelected(int arg0) { // TODO Auto-generated method stub for (int i = 0; i < imageViews.length; i++) { imageViews[arg0] .setBackgroundResource(R.drawable.page_indicator_focused); if (arg0 != i) { imageViews[i] .setBackgroundResource(R.drawable.page_indicator); } } }这里还需要一个适配器PagerAdapter类,我们写一个自己的类来继承它
class GuidPageAdapter extends PagerAdapter
主要引用的方法如下
class GuidPageAdapter extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return pageViews.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { // TODO Auto-generated method stub ((ViewPager) arg0).removeView(pageViews.get(arg1)); } @Override public Object instantiateItem(View arg0, int arg1) { // TODO Auto-generated method stub ((ViewPager) arg0).addView(pageViews.get(arg1)); return pageViews.get(arg1); } }
转载请注明出处
http://blog.csdn.net/pishum/article/details/38024331
标签:android界面左右滑动 view左右滑动 左右切换 全屏幕滑动切换
原文地址:http://blog.csdn.net/pishum/article/details/38024331