zoukankan      html  css  js  c++  java
  • ListFragment的使用

    ListFragment继承了Fragment,顾名思义,ListFragment是一种特殊的Fragment,它包含了一个ListView,在ListView里面显示数据。

    1. MainActivity

    Java类文件:

    package com.example.hzhi.fragmentdemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    
    
    public class MainActivity extends Activity {
        private FragmentManager manager;
        private FragmentTransaction transaction;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            manager = getFragmentManager();
            transaction = manager.beginTransaction();
            ListFragmentImpl frgImpl = new ListFragmentImpl();
            ListFragmentSelf frgSelf = new ListFragmentSelf();
            transaction.add(R.id.fragment1, frgImpl, "frgImpl");
            transaction.add(R.id.fragment2, frgSelf, "frgSelf");
            transaction.commit();
        }
    
    }

    xml布局文件:

    <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" >
    
        <LinearLayout
            android:id="@+id/fragment1"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"/>
    
        <LinearLayout
            android:id="@+id/fragment2"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"/>
    
    </LinearLayout>

    可见MainActivity是比较简单的,在布局里面放了左右两个ListFragment。

    2. ListFragment

    2.1 左边的ListFragment

    Java类文件:

    package com.example.hzhi.fragmentdemo;
    
    import android.app.ListFragment;
    import android.widget.ListView;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.util.Log;
    import android.widget.Toast;
    import android.widget.SimpleAdapter;
    
    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListFragmentImpl extends ListFragment{
        private static final String TAG = "ListFragmentImpl";
    
        private ListView selfList;
    
        String[] classes = {
                "计算机网络",
                "操作系统",
                "C语言",
                "Java",
                "数据库原理",
                "移动开发",
        };
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.d(TAG, "onCreateView");
            return inflater.inflate(R.layout.list_fragment_impl, container, false);
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            Log.d(TAG, "onCreate");
            super.onCreate(savedInstanceState);
            // 设置ListFragment默认的ListView,即@id/android:list
            this.setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, classes));
    
        }
    
        public void onListItemClick(ListView parent, View v,
                                    int position, long id) {
            Log.d(TAG, "onListItemClick");
            // 找到ListFragmentSelf
            ListFragmentSelf listFragmentSelf = (ListFragmentSelf) getFragmentManager().
                    findFragmentByTag("frgSelf");
            listFragmentSelf.flushData(position);
    
        }
        
    }

    布局文件:

    <?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" >
    
        <!-- ListFragment对应的android:id值固定为"@id/android:list" -->
        <ListView
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"
            />
    
    </LinearLayout>

    2.2 右边的ListFragment

    Java类文件:

    package com.example.hzhi.fragmentdemo;
    
    import android.app.ListFragment;
    import android.widget.ListView;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.util.Log;
    import android.widget.SimpleAdapter;
    
    import java.util.Map;
    import java.util.HashMap;
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListFragmentSelf extends ListFragment{
        private static final String TAG = "ListFragmentImpl";
    
        private ListView selfList;
    
        final String[] from = new String[] {"name", "title", "info", "picture"};
        final int[] to = new int[] {R.id.text0, R.id.text1, R.id.text2, R.id.picture};
        private String[] tname = new String[]{"计算机网络", "操作系统", "C语言", "Java", "数据库原理", "移动开发"};
        private String[] ttitle = new String[]{"张三", "李四", "王五", "Tom", "Mike", "Peter"};
        private String[] ttime = new String[]{"160", "50", "40", "200", "180", "150"};
        private int[] pic = new int[]{R.drawable.jsjwl, R.drawable.czxt, R.drawable.cyy,
                R.drawable.java, R.drawable.sjkyl, R.drawable.ydkf};
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.d(TAG, "onCreateView");
            return inflater.inflate(R.layout.list_fragment_self, container, false);
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            Log.d(TAG, "onCreate");
            super.onCreate(savedInstanceState);
            flushData(0);
    
        }
    
        public void onListItemClick(ListView parent, View v,
                                    int position, long id) {
            Log.d(TAG, "onListItemClick");
    
        }
    
        public void flushData(int p){
            // 建立SimpleAdapter,将from和to对应起来
            SimpleAdapter adapter = new SimpleAdapter(
                    this.getActivity(), getSimpleData(p),
                    R.layout.list_item, from, to);
            this.setListAdapter(adapter);
        }
    
        private List<Map<String, Object>> getSimpleData(int p) {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "课程名称");
            map.put("info", tname[p]);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "教师姓名");
            map.put("info", ttitle[p]);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "学时");
            map.put("info", ttime[p]);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "图片");
            map.put("picture", pic[p]);
            list.add(map);
    
            return list;
        }
    }

    布局文件:

    <?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" >
    
        <!-- ListFragment对应的android:id值固定为"@id/android:list" -->
        <ListView
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"
            />
    
    </LinearLayout>

    行布局文件:

    <?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" >
    
        <TextView android:id="@+id/text0"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <TextView android:id="@+id/text1"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <TextView android:id="@+id/text2"
            android:textSize="24sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <ImageView
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp" />
    
    </LinearLayout>

    最重要的方法是,当点击左边ListFragment的某一行时,取得改行的position,然后根据Tag找到右边的ListFragment,并调用flushData()方法,使右边的ListFragment刷新数据。

    3. 运行结果

  • 相关阅读:
    去掉python的警告
    LeetCode--687. 最长同值路径
    Python中获取字典中最值对应的键
    python -- 解决If using all scalar values, you must pass an index问题
    keras自定义padding大小
    评价指标的局限性、ROC曲线、余弦距离、A/B测试、模型评估的方法、超参数调优、过拟合与欠拟合
    一言难尽的js变量提升
    vue-cli 脚手架 安装
    十分钟入门 Less
    Echarts的资源文件
  • 原文地址:https://www.cnblogs.com/mstk/p/5789265.html
Copyright © 2011-2022 走看看