Android笔记之ListFragment使用

ListFragment继承于Fragment:

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();
    }
}

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

 View view = inflater.inflate(R.layout.left, null);
这里用到
<?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>

 

Done!

Android笔记之ListFragment使用,,5-wow.com

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