Android ListFragment实例Demo

该篇文章是一个ListFragment的一个实例,通过了解该实例,更能了解比较常用的ListFragment的用法,以及各Fragment之间的数据传递。

实现效果图:

该MainActivity中包括1个Button+2个Fragment(右边两个),点击Button,出现中间的list列表,点击列表中的任一项,相应item数值,会传递到右边的Fragment中并显示。


源代码:


activity_main:

<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:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#CCCCCC"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="显示列表" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/center"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#CCDDFF"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#CCFFDD"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

ArticleListFragment本来也应该有一个布局文件,这里是在代码中方便的直接添加了个ListView,也是因为该类继承了ListFragment的缘故。

DetailFragment包含的布局文件:

<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:orientation="horizontal"
    tools:context=".MainActivity" >


    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>


</LinearLayout>

代码文件:

MainActivity:

package com.fragmentdemo8_listfragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
 * ListFragment的一个实例Demo
 */
public class MainActivity extends Activity {
	private Button button;
	private FragmentManager manager;
	private FragmentTransaction transaction;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		manager = getFragmentManager();
		
		button = (Button) findViewById(R.id.button);
		/**
		 * 点击Activity中的该按钮,Activity会在布局中间添加ArticleListFragment,并显示列表数据。
		 */
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				transaction = manager.beginTransaction();
				ArticleListFragment articleListFragment = new ArticleListFragment();
				transaction.add(R.id.center, articleListFragment, "center");
				transaction.commit();
			}
		});
	}

}

中间的Fragment:ArticleListFragment:

package com.fragmentdemo8_listfragment;

import java.util.ArrayList;
import java.util.List;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
 *ArticleListFragment继承ListFragment,进行一些列表数据的显示。
 */
public class ArticleListFragment extends ListFragment {
	private ArrayAdapter<String> adapter;
	private List<String> data;
	private FragmentManager manager;
	private FragmentTransaction transaction;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		data = new ArrayList<String>();
		for (int i = 0; i < 30; i++) {
			data.add("rose" + i);
		}
		manager = getFragmentManager();
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, data);
		setListAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		String str = adapter.getItem(position);
		transaction = manager.beginTransaction();

		DetailFragment detailFragment = new DetailFragment();
		/**
		 * 使用Bundle类存储传递数据
		 */
		Bundle bundle = new Bundle();
		bundle.putString("id", str);
		detailFragment.setArguments(bundle);
		transaction.replace(R.id.right, detailFragment, "detail");
		transaction.commit();
		Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
	}
}

右边的Fragment:DetailFragment:

package com.fragmentdemo8_listfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
 *从ArticleListFragment中的列表item获取数据,然后展示在该Fragment上。 
 *
 */
public class DetailFragment extends Fragment {
	private TextView textView;
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.detail, null);
		textView = (TextView) view.findViewById(R.id.textView);

		Bundle bundle = getArguments();
		String str = bundle.getString("id");

		textView.setText(str);

		return view;
	}

}

源代码下载:

点击下载源码

Android ListFragment实例Demo,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。