Android FragmentStatePageAdapter的使用Demo

來源:互聯網
上載者:User

標籤:android   style   blog   http   os   io   使用   java   ar   

上一篇寫過FragmentPagerAdapter,這篇來介紹FragmentStatePagerAdapter,那麼兩者之間有何差別呢:

FragmentPagerAdapter很多其它的用於少量介面的ViewPager,比方Tab。划過的fragment會儲存在記憶體中,雖然已經划過。而FragmentStatePagerAdapter和ListView有點類似,會儲存當前介面,以及下一個介面和上一個介面(假設有),最多儲存3個,其它會被銷毀掉。

假設想要更具體的瞭解,能夠查看官網API,以下給出依照官網上寫出的Demo:

實現:

源碼:


布局檔案:

fragment_pager.xml(布局了ViewPager和兩個button):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical"    android:padding="4dip" >    <android.support.v4.view.ViewPager        android:id="@+id/pager"        android:layout_width="match_parent"        android:layout_height="0px"        android:layout_weight="1" >    </android.support.v4.view.ViewPager>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="0"        android:gravity="center"        android:measureWithLargestChild="true"        android:orientation="horizontal" >        <Button            android:id="@+id/goto_first"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="調到首頁" >        </Button>        <Button            android:id="@+id/goto_last"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="調到尾頁" >        </Button>    </LinearLayout></LinearLayout>

fragment_pager_list.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:drawable/gallery_thumb"    android:orientation="vertical" >    <!-- 該Textview用來顯示Fragment的頁數 -->    <TextView        android:id="@+id/text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical|center_horizontal"        android:text="@string/hello_world"        android:textAppearance="?android:attr/textAppearanceMedium" />    <!-- 不為空白用來顯示ListView,假設為空白,則顯示TextView(資料為空白) -->    <FrameLayout        android:layout_width="match_parent"        android:layout_height="0dip"        android:layout_weight="1" >        <ListView            android:id="@android:id/list"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:drawSelectorOnTop="false" />        <TextView            android:id="@android:id/empty"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="資料為空白"            android:textAppearance="?android:attr/textAppearanceMedium" />    </FrameLayout></LinearLayout>

代碼檔案:

MainActivity:

package com.fragmentdemo13_fragmentstatepageradapter;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * 這裡我們調用的是support.v4的包,所以MainActivity繼承的是FragmentActivity,而不是Activity。 */public class MainActivity extends FragmentActivity {public static final int NUM_ITEMS = 10;private MyAdapter mAdapter;private ViewPager mPager;private Button button_first, button_last;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_pager);/** * 相同,因為調用的是support.v4的包,這裡是getSupportFragmentManager(),而不是getFragmentManager(); */mAdapter = new MyAdapter(getSupportFragmentManager());mPager = (ViewPager) findViewById(R.id.pager);mPager.setAdapter(mAdapter);/** * 點擊返回首頁 */button_first = (Button) findViewById(R.id.goto_first);button_first.setOnClickListener(new OnClickListener() {public void onClick(View v) {mPager.setCurrentItem(0);}});/** * 點擊返回尾頁 */button_last = (Button) findViewById(R.id.goto_last);button_last.setOnClickListener(new OnClickListener() {public void onClick(View v) {mPager.setCurrentItem(NUM_ITEMS - 1);}});}}

MyAdapter.java:

package com.fragmentdemo13_fragmentstatepageradapter;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;/** * 這裡繼承的是FragmentStatePagerAdapter, 依據官方API的介紹,當項目中遇到使用大量的列表頁面時,使用該適配器是個更好的選擇。 * (This version of the pager is more useful when there are a large number of * pages, working more like a list view.) */public class MyAdapter extends FragmentStatePagerAdapter {public MyAdapter(FragmentManager fm) {super(fm);}/** * 僅僅須要實現以下兩個方法就可以。 */@Overridepublic Fragment getItem(int position) {return ArrayListFragment.newInstance(position);}@Overridepublic int getCount() {return MainActivity.NUM_ITEMS;}}

ArrayListFragment.java:

package com.fragmentdemo13_fragmentstatepageradapter;import java.util.ArrayList;import android.os.Bundle;import android.support.v4.app.ListFragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class ArrayListFragment extends ListFragment {private int mNum;public ArrayList<String> list = new ArrayList<String>();/** * 建立一個計算Fragment頁面的執行個體,將怒num作為一個參數。 * (Create a new instance of * CountingFragment, providing "num" as an argument.) */public static ArrayListFragment newInstance(int num) {ArrayListFragment f = new ArrayListFragment();Bundle args = new Bundle();args.putInt("num", num);f.setArguments(args);return f;}/** * 當調用該方法時,檢索此執行個體的數量的參數。 * (When creating, retrieve this instance's number from * its arguments.) */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mNum = getArguments() != null ? getArguments().getInt("num") : 1;}/** * Fragment的UI僅僅顯示它所在的頁碼。 * (The Fragment's UI is just a simple text view * showing its instance number.) */@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_pager_list, container,false);TextView tv = (TextView) view.findViewById(R.id.text);tv.setText("Fragment #" + mNum);return view;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, getData()));}/** * 在每個Fragment列表中展示的資料。 */private ArrayList<String> getData() {for (int i = 0; i < 20; i++) {list.add("nihao" + i);}return list;}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {Toast.makeText(getActivity(), "您點擊了"+position, 0).show();}}

源碼下載:

點擊下載原始碼


Android FragmentStatePageAdapter的使用Demo

聯繫我們

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