zoukankan      html  css  js  c++  java
  • 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!

  • 相关阅读:
    视频实例分割 | Target-Aware Adaptive Tracking for Unsupervised Video Object Segmentation
    目标检测算法:Selective Search(选择性搜索)简述
    [2020BUAA软工助教]期末总结
    WPF,数据验证方案,验证通过才能继续下一步
    WPF:解决数据验证ValidationRule与按钮Canexcute联动的问题
    解决mininet运行报错“ImportError: No module named mininet.log”
    交易所对接ERC20钱包端口要多长时间?
    尺子的刻度
    学习java的第四周
    学习java的第三周
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3608920.html
Copyright © 2011-2022 走看看