Sử dụng ListFragment trong Android

ListFragment là một lớp con của Fragment, được sử dụng để hiển thị dữ liệu trong một ListView. Dưới đây là cách sử dụng ListFragment trong ứng dụng Android.

  1. MainActivity ===============

Java class:

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 fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        CustomListFragment customFrag = new CustomListFragment();
        AnotherCustomListFragment anotherFrag = new AnotherCustomListFragment();
        fragmentTransaction.add(R.id.fragment_container1, customFrag, "customFrag");
        fragmentTransaction.add(R.id.fragment_container2, anotherFrag, "anotherFrag");
        fragmentTransaction.commit();
    }
}

XML layout:

<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/fragment_container1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

    <LinearLayout
        android:id="@+id/fragment_container2"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

</LinearLayout>
  1. ListFragment ===============

2.1 ListFragment bên trái

Java class:

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;

public class CustomListFragment extends ListFragment {
    private static final String TAG = "CustomListFragment";

    private ListView listView;

    String[] subjects = {
            "Mạng máy tính",
            "Hệ điều hành",
            "Ngôn ngữ C",
            "Java",
            "Cơ sở dữ liệu",
            "Phát triển di động"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        return inflater.inflate(R.layout.custom_list_fragment, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_list_item_1, subjects));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.d(TAG, "onListItemClick");
        AnotherCustomListFragment anotherFrag = (AnotherCustomListFragment) getFragmentManager().
                findFragmentByTag("anotherFrag");
        anotherFrag.updateData(position);
    }
}

XML layout:

<?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">

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"/>
</LinearLayout>

2.2 ListFragment bên phải

Java class:

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 AnotherCustomListFragment extends ListFragment {
    private static final String TAG = "AnotherCustomListFragment";

    private ListView listView;

    final String[] from = {"name", "title", "info", "picture"};
    final int[] to = {R.id.text0, R.id.text1, R.id.text2, R.id.picture};
    private String[] subjectNames = {"Mạng máy tính", "Hệ điều hành", "Ngôn ngữ C", "Java", "Cơ sở dữ liệu", "Phát triển di động"};
    private String[] teachers = {"Trần A", "Nguyễn B", "Lê C", "Tom", "Mike", "Peter"};
    private String[] hours = {"160", "50", "40", "200", "180", "150"};
    private int[] images = {R.drawable.network, R.drawable.os, R.drawable.c, R.drawable.java, R.drawable.db, R.drawable.mobile};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        return inflater.inflate(R.layout.another_custom_list_fragment, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        updateData(0);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.d(TAG, "onListItemClick");
    }

    public void updateData(int position) {
        SimpleAdapter adapter = new SimpleAdapter(
                getActivity(), getDataForPosition(position),
                R.layout.list_item, from, to);
        setListAdapter(adapter);
    }

    private List<Map<String, Object>> getDataForPosition(int position) {
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("title", "Tên môn học");
        map.put("info", subjectNames[position]);
        list.add(map);

        map = new HashMap<>();
        map.put("title", "Giáo viên");
        map.put("info", teachers[position]);
        list.add(map);

        map = new HashMap<>();
        map.put("title", "Số giờ học");
        map.put("info", hours[position]);
        list.add(map);

        map = new HashMap<>();
        map.put("title", "Hình ảnh");
        map.put("picture", images[position]);
        list.add(map);

        return list;
    }
}

XML layout:

<?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">

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"/>
</LinearLayout>

Layout cho mỗi mục:

<?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>

Khi click vào một mục trong ListFragment bên trái, phương thức updateData sẽ được gọi để cập nhật dữ liệu cho ListFragment bên phải.

Thẻ: Android ListFragment ListView Fragment ArrayAdapter

Đăng vào ngày 16 tháng 6 lúc 23:26