Android 中怎么利用ViewPager實現(xiàn)左右滑動翻頁效果,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

代碼如下:
package com.example.demo;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager mViewPager=(ViewPager) findViewById(R.id.viewPager);
PagerAdapter mPagerAdapter=new MyPagerAdapter(this);
mViewPager.setAdapter(mPagerAdapter);
}
private class MyPagerAdapter extends PagerAdapter{
private Context context;
private ArrayList<TextView> views;
public MyPagerAdapter(Context context){
this.context=context;
views=new ArrayList<TextView>();
for(int i=0;i<30;i++){
TextView tv=new TextView(context);
tv.setText(""+i);
views.add(tv);
}
}
@Override
public int getCount() {
return views.size();
}
@Override
public void destroyItem(ViewGroup container, int position,Object object) {
container.removeView(views.get(position));
}
@Override
public View instantiateItem(ViewGroup container, int position){
((ViewGroup) container).addView(views.get(position));
return views.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
}
}布局文件:
<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="match_parent" tools:context="com.example.demo.MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
看完上述內(nèi)容,你們掌握Android 中怎么利用ViewPager實現(xiàn)左右滑動翻頁效果的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文題目:Android中怎么利用ViewPager實現(xiàn)左右滑動翻頁效果-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.chinadenli.net/article4/deihoe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、域名注冊、ChatGPT、App開發(fā)、網(wǎng)站設計公司、服務器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容