Android笔记之ListFragment使用

ListFragment继承于Fragment:

Android笔记之ListFragment使用
package com.example.android_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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LeftFragment extends ListFragment {
    private ArrayAdapter<String> adapter;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, getData());
        manager = getFragmentManager();
    }

    public List<String> getData() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 30; i++) {
            list.add("????" + i);// ??????μ????
        }
        return list;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.left, null);
        setListAdapter(adapter);
        adapter.notifyDataSetChanged();
        return view;
    }

    // ?????????????
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String item = adapter.getItem(position);
        //
        RightFragment fragment = new RightFragment();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.right, fragment, "fragment");
        transaction.addToBackStack("fragment");
        Bundle bundle = new Bundle();
        bundle.putString("item", item);
        fragment.setArguments(bundle);
        transaction.commit();
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
}
Android笔记之ListFragment使用

对应的xml文件,使用的时候只在

 View view = inflater.inflate(R.layout.left, null);
这里用到
Android笔记之ListFragment使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00FF00"
        android:drawSelectorOnTop="false" />

</LinearLayout>
Android笔记之ListFragment使用

 

Done!

Android笔记之ListFragment使用,布布扣,bubuko.com

Android笔记之ListFragment使用

上一篇:升级ADT22.6后,Android模拟器无法创建


下一篇:ios中Core Location跟Map Kit的基本使用