Android FragmentActivity+viewpager的使用

來源:互聯網
上載者:User

標籤:

使用情境,打算設計一個“底部功能表列+其餘可滑動的頁面”的簡單的功能。

package com.lanyuweng.mibaby;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.CalendarView;import android.widget.TextView;import com.lanyuweng.mibaby.fragment.FragmentAdapter;public class MainActivity extends FragmentActivity implements OnClickListener{        private static final String TAG = "MainActivity-----";        public static final int TAB_CALENDAR = 0;    public static final int TAB_REMINDER = 1;    public static final int TAB_NOTE    = 2;    public static final int TAB_MORE    = 3;    private CalendarView calendarView;        private ViewPager viewPager;    private TextView tvCalendar,tvReminder,tvNote,tvMore;        //定義適配器    private FragmentAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {                Log.i(TAG,"initView----");        viewPager  = (ViewPager)findViewById(R.id.viewpager);        tvCalendar = (TextView) findViewById(R.id.tvCalendar);        tvReminder = (TextView) findViewById(R.id.tvReminder);        tvNote = (TextView) findViewById(R.id.tvNote);        tvMore = (TextView) findViewById(R.id.tvMore);        tvCalendar.setOnClickListener(this);        tvReminder.setOnClickListener(this);        tvNote.setOnClickListener(this);        tvMore.setOnClickListener(this);                adapter = new FragmentAdapter(getSupportFragmentManager());        viewPager.setAdapter(adapter);    }    @Override    public void onClick(View arg0) {        switch (arg0.getId()) {        case R.id.tvCalendar:            viewPager.setCurrentItem(TAB_CALENDAR);            findViewById(R.id.tvConfig);            break;        case R.id.tvMore:            viewPager.setCurrentItem(TAB_MORE);            break;        case R.id.tvNote:            viewPager.setCurrentItem(TAB_NOTE);            break;        case R.id.tvReminder:            viewPager.setCurrentItem(TAB_REMINDER);            break;        default:            break;        }    }        @Override    protected void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();    }    }

通過兩種方式來切換頁面,左右滑動以及點擊底部功能表列。第二種方式是使用onClick()方法,第二種方式則是以繼承FragmentPagerAdapter來控制Fragment的顯示。

package com.lanyuweng.mibaby.fragment; import com.lanyuweng.mibaby.MainActivity;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;public class FragmentAdapter extends FragmentPagerAdapter {    public final static int TAB_COUNT = 4;    public FragmentAdapter(FragmentManager fm) {        super(fm);    }    @Override    public Fragment getItem(int arg0) {        switch (arg0) {        case MainActivity.TAB_CALENDAR:            CalendarFragment calendarFragment = new CalendarFragment();            return calendarFragment;                    case MainActivity.TAB_REMINDER:            ReminderFragment reminderFragment = new ReminderFragment();            return reminderFragment;                    case MainActivity.TAB_NOTE:            NoteFragment noteFragment = new NoteFragment();            return noteFragment;        case MainActivity.TAB_MORE:            MoreFragment moreFragment = new MoreFragment();            return moreFragment;        }        return null;    }    @Override    public int getCount() {        // TODO Auto-generated method stub        return TAB_COUNT;    }} 

同時,XML為:

<LinearLayout 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"    android:background="@drawable/background"    android:orientation="vertical" >    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="fill_parent"        android:layout_height="match_parent"        android:layout_weight="9" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="55sp"        android:layout_weight="1"        android:background="@color/burlywood"        android:orientation="horizontal" >        <TextView            android:id="@+id/tvCalendar"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:layout_weight="1"            android:drawableTop="@drawable/calendar"                        android:gravity="center"            android:text="@string/calendar"            android:textSize="12sp"            tools:ignore="NestedWeights" >        </TextView>        <TextView            android:id="@+id/tvReminder"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:layout_weight="1"            android:drawableTop="@drawable/reminder"            android:gravity="center"            android:text="@string/reminder"            android:textSize="12sp" >        </TextView>        <TextView            android:id="@+id/tvNote"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:layout_weight="1"            android:drawableTop="@drawable/note"            android:gravity="center"            android:text="@string/note"            android:textSize="12sp" >        </TextView>        <TextView            android:id="@+id/tvMore"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:layout_weight="1"            android:drawableTop="@drawable/more"            android:gravity="center"            android:text="@string/more"            android:textSize="12sp" >        </TextView>    </LinearLayout></LinearLayout>

 

記錄一下fragmentactivity+viewpager+adapter的使用,以備後用

 

Android FragmentActivity+viewpager的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.