55 Android Fragment_ViewPager 适配器 FragmentStatePagerAdapter

来源:互联网 发布:python运行原理 编辑:程序博客网 时间:2024/05/21 06:56


fragment_pager_list.xml  (加载到 LsitFragment 上)

<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        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" />    <!--    The frame layout is here since we will be showing either    the empty view or the list view.    -->    <FrameLayout        android:layout_width="match_parent"        android:layout_height="0dip"        android:layout_weight="1" >        <!--             Here is the list. Since we are using a ListActivity, we             have to call it "@android:id/list" so ListActivity will             find it        -->        <ListView            android:id="@android:id/list"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:drawSelectorOnTop="false" />        <!-- Here is the view to show if the list is emtpy -->        <TextView            android:id="@android:id/empty"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="No items."            android:textAppearance="?android:attr/textAppearanceMedium" />    </FrameLayout></LinearLayout>


activity_main.xml (主布局文件)

<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>

MainActivity.java

package com.example.android_fragment_fragmentstatepageradapter;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;import android.support.v4.app.ListFragment;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends FragmentActivity {private ViewPager viewPager;static final int NUM_ITEMS=10;//定义一共有10页private Button button1;private Button button2;private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState)  {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewPager=(ViewPager)this.findViewById(R.id.pager);adapter=new MyAdapter(getSupportFragmentManager());button1=(Button)findViewById(R.id.goto_first);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubviewPager.setCurrentItem(0);}});button2=(Button)findViewById(R.id.goto_last);button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubviewPager.setCurrentItem(NUM_ITEMS-1);}});viewPager.setAdapter(adapter);}/** * 填充适配器数据 * @author Administrator * */public static class MyAdapter extends FragmentStatePagerAdapter{public MyAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int arg0) {// TODO Auto-generated method stubreturn ArrayListFragment.getIntances(arg0);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn NUM_ITEMS;}}public static  class ArrayListFragment extends ListFragment{int num;static ArrayListFragment getIntances(int num){ArrayListFragment arrayListFragment =new ArrayListFragment();Bundle bundle=new Bundle();bundle.putInt("num", num);arrayListFragment.setArguments(bundle);return arrayListFragment;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getData()));}public List<String> getData(){ List<String> list=new ArrayList<String>(); for(int i=0;i<20;i++) { list.add("Item"+i); } return list;}@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);num=getArguments() !=null?getArguments().getInt("num"):1;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.fragment_pager_list, null);TextView tv=(TextView)view.findViewById(R.id.text);    tv.setText("Frament #"+num );return view;}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubToast.makeText(getActivity(), "clicked", 1).show();}} @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}







0 0
原创粉丝点击