日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ViewPager 实现页面左右滑动的简单案例1

發(fā)布時(shí)間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ViewPager 实现页面左右滑动的简单案例1 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ViewPager 實(shí)現(xiàn)頁(yè)面左右滑動(dòng)的簡(jiǎn)單案例

主要Activity:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

? ? xmlns:tools="http://schemas.android.com/tools"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:paddingBottom="@dimen/activity_vertical_margin"

? ? android:paddingLeft="@dimen/activity_horizontal_margin"

? ? android:paddingRight="@dimen/activity_horizontal_margin"

? ? android:paddingTop="@dimen/activity_vertical_margin"

? ? tools:context=".MainActivity">


? ? <android.support.v4.view.ViewPagerandroid:id="@+id/view_pager"

? ? ? ? android:layout_width="fill_parent"

? ? ? ? android:layout_height="fill_parent">? ? ? ? ? ? ? ?

? ? </android.support.v4.view.ViewPager>

</RelativeLayout>

主類:

public class MainActivity extends Activity {

ViewPager pager;

List<View> views;


@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

pager = (ViewPager)findViewById(R.id.view_pager);

views =new ArrayList<View>();

LayoutInflater mLi = LayoutInflater.from(this); ?

View view1 = mLi.inflate(R.layout.view1,null); ?

View view2 = mLi.inflate(R.layout.view2,null); ?

View view3 = mLi.inflate(R.layout.view3,null); ?

views.add(view3); ?

views.add(view2);? ?

views.add(view1);

MyPagerAdapter adapter =new MyPagerAdapter();

pager.setAdapter(adapter);

}

class MyPagerAdapterextends PagerAdapter{


@Override

publicint getCount() {

//TODO Auto-generated method stub

returnviews.size();

}


@Override

publicboolean isViewFromObject(View arg0, Object arg1) {

//TODO Auto-generated method stub

return arg0 == arg1;? ?//這行代碼很重要,它用于判斷你當(dāng)前要顯示的頁(yè)面

}

@Override ?

public Object instantiateItem(ViewGroup container,int position) { ?

? ? container.addView(views.get(position)); ?

? ? return views.get(position); ?

} ?

@Override ?

publicvoid destroyItem(ViewGroup container,int position, Object object) { ?

? ? container.removeView(views.get(position)); ?

} ?

}

}


三個(gè)View頁(yè)面:

1

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

? ? xmlns:tools="http://schemas.android.com/tools"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:paddingBottom="@dimen/activity_vertical_margin"

? ? android:paddingLeft="@dimen/activity_horizontal_margin"

? ? android:paddingRight="@dimen/activity_horizontal_margin"

? ? android:paddingTop="@dimen/activity_vertical_margin"

? ? tools:context=".MainActivity"

? ? android:background="#f00">


? ? <TextViewandroid:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="1"/>


</RelativeLayout>

2

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

? ? xmlns:tools="http://schemas.android.com/tools"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:paddingBottom="@dimen/activity_vertical_margin"

? ? android:paddingLeft="@dimen/activity_horizontal_margin"

? ? android:paddingRight="@dimen/activity_horizontal_margin"

? ? android:paddingTop="@dimen/activity_vertical_margin"

? ? tools:context=".MainActivity"

? ? android:background="#ff0">


? ? <TextViewandroid:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="2"/>


</RelativeLayout>

3

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

? ? xmlns:tools="http://schemas.android.com/tools"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:paddingBottom="@dimen/activity_vertical_margin"

? ? android:paddingLeft="@dimen/activity_horizontal_margin"

? ? android:paddingRight="@dimen/activity_horizontal_margin"

? ? android:paddingTop="@dimen/activity_vertical_margin"

? ? tools:context=".MainActivity"

?? android:background="#0ff">


? ? <TextViewandroid:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="3"/>


</RelativeLayout>

運(yùn)行結(jié)果:

demo下載:http://download.csdn.net/detail/lyhdream/5170645





總結(jié)

以上是生活随笔為你收集整理的ViewPager 实现页面左右滑动的简单案例1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。