Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍、Android Fragment使用、Android FragmentManage FragmentTransaction介绍中做了关于Fragment的详细介绍。这一片主要通过一个实例了解Fragment的使用。

先看下实例效果图:

效果图的左边是一个列表,右边是列表item的详情。

先看一下布局文件(layout):

<?xml version=“1.0″ encoding=“utf-8″?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”    android:orientation=“horizontal” android:layout_width=“match_parent”    android:layout_height=“match_parent”>    <fragment        class=“com.fragment.main.TitlesFragment”        android:id=“@+id/titles” android:layout_weight=“1″        android:layout_width=“0px” android:layout_height=“match_parent” />    <FrameLayout android:id=“@+id/details” android:layout_weight=“1″        android:layout_width=“0px” android:layout_height=“match_parent”        android:background=“?android:attr/detailsElementBackground” /></LinearLayout>

布局文件中使用了fragment标签和FrameLayout标签。Android Fragment使用中介绍了2中嵌入Fragment的方法,这个实例中都用到,从布局文件看到有了fragment标签,这是一种使用方法,FrameLayout标签将会成为第二种加载fragment的载体view。

看一下程序实现(com.fragment.main.TitlesFragment):

public class TitlesFragment extends ListFragment {     int mCurCheckPosition = 0;    int mShownCheckPosition = -1;     @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);                                                                 setListAdapter(new ArrayAdapter<String>(getActivity(),                android.R.layout.simple_list_item_activated_1,                Shakespeare.TITLES)); //使用静态数组填充列表        if (savedInstanceState != null) {             mCurCheckPosition = savedInstanceState.getInt(“curChoice”, 0);            mShownCheckPosition = savedInstanceState.getInt(“shownChoice”, -1);        }             getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);             showDetails(mCurCheckPosition);    }     @Override    public void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);         outState.putInt(“curChoice”, mCurCheckPosition);        outState.putInt(“shownChoice”, mShownCheckPosition);    }     @Override    public void onListItemClick(ListView l, View v, int position, long id) {        showDetails(position);    }     /**     *显示listview item 详情     */    void showDetails(int index) {        mCurCheckPosition = index;            getListView().setItemChecked(index, true);             if (mShownCheckPosition != mCurCheckPosition) {                 DetailsFragment df = DetailsFragment.newInstance(index);                  FragmentTransaction ft = getFragmentManager()                        .beginTransaction();                ft.replace(R.id.details, df);                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);                ft.commit();                mShownCheckPosition = index;            }        } }

TitlesFragment

TitlesFragment继承自Fragment的子类ListFragment,使用了一个静态数组填充列表,重写了onListItemClick方法,showDetails方法展示ListView item的详情。

DetailsFragment df = DetailsFragment.newInstance(index);//获取详情Fragment的实例FragmentTransaction ft = getFragmentManager().beginTransaction();//获取FragmentTransaction 实例ft.replace(R.id.details, df);  //使用DetailsFragment 的实例ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);ft.commit();//提交

这里就使用到了Android Fragment使用中介绍的第二种加载fragment的方法。看一下DetailsFragment :

public class DetailsFragment extends Fragment {     /**     * Create a new instance of DetailsFragment, initialized to     * show the text at ’index’.     */    public static DetailsFragment newInstance(int index) {        DetailsFragment f = new DetailsFragment();        // Supply index input as an argument.                Bundle args = new Bundle();        args.putInt(“index”, index);        f.setArguments(args);        return f;    }     @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        if (container == null) {                        return null;        }        ScrollView scroller = new ScrollView(getActivity());        TextView text = new TextView(getActivity());         int padding = (int) TypedValue.applyDimension(                TypedValue.COMPLEX_UNIT_DIP, 4, getActivity().getResources()                        .getDisplayMetrics());        text.setPadding(padding, padding, padding, padding);        scroller.addView(text);        text.setText(Shakespeare.DIALOGUE[getArguments().getInt("index", 0)]);        return scroller;    }}

DetailsFragment 中使用newInstance(int index)方法产生DetailsFragment 实例并接受整型参数,重载了onCreateView方法创建view。

这个例子基本完成了,主要介绍的是在3.0以后的使用方法,其实Fragment在SDK1.6之后就可以使用了,在1.6上使用需要借助 android-support-v4.jar包实现。android-support-v4.jar在:SDK根目录\extras\android \compatibility\v4下可以找到,如果想了解Fragment在SDK1.6上怎么实现的请参考Fragment 在Android SDK1.6上实现。

更多相关文章

  1. android 自带的主题 theme 的使用
  2. 在android的Browser中设置User Agent
  3. Android(安卓)自定义View及其在布局文件中的使用示例(三):结合An
  4. Dagger2使用
  5. TextView和EidtText使用技巧
  6. Android(安卓)CardView使用和导入出错问题
  7. Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
  8. Android(安卓)UI LinearLayout权限级别与TableLayout混合使用,
  9. Android(安卓)缩放、移动、旋转View相关方法

随机推荐

  1. Android,UI主线程与子线程
  2. android mContainer.setPersistentDrawin
  3. Android伸手党系列之五:Android UI相关知
  4. 解析Android中的main线程与子线程
  5. ADB 工具
  6. Android的RelativeLayout的layout_height
  7. Android应用程序内存分析-Memory Analysi
  8. -如何将Eclipse中的项目迁移到Android St
  9. Android(安卓)实例教程
  10. 高焕堂