码迷,mamicode.com
首页 > 移动开发 > 详细

Android ListFragment实例Demo

时间:2014-06-19 11:33:21      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:android listfragment

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

实现效果图:

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

bubuko.com,布布扣

源代码:

bubuko.com,布布扣

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,布布扣,bubuko.com

Android ListFragment实例Demo

标签:android listfragment

原文地址:http://blog.csdn.net/u012440207/article/details/30085279

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!