Android ViewPager使用方法

來源:互聯網
上載者:User


<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+yOfNvKOsYW5kcm9pZNbQyrnTw1ZpZXdQYWdlcr/J0tTKtc/WtuC49tKzw+a7rLav0Ke5+6GiPC9wPgo8cD48YnI+CjwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">public class TvControlActivity extends Activity {private TvControlActivity tvControlActivity = this;// 需要滑動的頁卡private View view1;private View view2;private View view3;private ArrayList views;// 多頁面滑動切換private ViewPager viewPager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tv_control);viewPager = (ViewPager) findViewById(R.id.main_viewpager);viewPager.setOnPageChangeListener(null);initView();/** * 這是重寫PagerAdapter的一個方法,我們還可以這樣做 */PagerAdapter paAdapter = new PagerAdapter() {@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 == arg1;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn views.size();}//銷毀@Overridepublic void destroyItem(ViewGroup container, int position,Object object) {// TODO Auto-generated method stubcontainer.removeView(views.get(position));}//@Override//public void destroyItem(View container, int position, Object object) {//// TODO Auto-generated method stub//((ViewPager) container).removeView(views.get(position));////}@Overridepublic int getItemPosition(Object object) {// TODO Auto-generated method stubreturn super.getItemPosition(object);}//執行個體化@Overridepublic Object instantiateItem(ViewGroup container, int position) {// TODO Auto-generated method stubcontainer.addView(views.get(position));return views.get(position);}//@Override//public Object instantiateItem(View container, int position) {//// TODO Auto-generated method stub//((ViewPager) container).addView(views.get(position));//return views.get(position);//}};viewPager.setAdapter(paAdapter);}/** * 載入要顯示的頁卡 */private void initView() {// TODO Auto-generated method stubLayoutInflater inflater = getLayoutInflater().from(this);view1 = inflater.inflate(R.layout.tv_control, null);view2 = inflater.inflate(R.layout.tv_control_number, null);// 將要分頁顯示的View裝入數組中views = new ArrayList();views.add(view1);views.add(view2);}}在實現adapter的時候一定要重寫以下四個方法,其他方法的話也可以重寫

instantiateItem(ViewGroup container, int position)getItemPosition(Object object)destroyItem(ViewGroup container, int position,Object object)isViewFromObject(View arg0, Object arg1)


需要注意的是布局檔案中需要這樣寫
組件是用來顯示左右滑動的介面的,如果不載入xml布局檔案,他是不會顯示內容的        android:id="@+id/main_viewpager"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" >    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.