在片段中添加 ListView

Adding a ListView in a Fragment

我想将 ListView 添加到 Fragment。我在 activity_main.xml 中创建了一个 FrameLayout,然后创建了 3 个片段(主页、标签、设置)。我所做的是创建一个底部导航视图并为提到的片段创建 3 个框架布局。

我在整个网络上搜索了如何将 ListView 添加到 Fragment,但无论他们说什么代码都已弃用。我只想将 ListView 添加到设置片段。代码如下:

package com.bhiruva.dashboard.fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.bhiruva.R;

public class FragmentSettings extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private String mParam2;

public FragmentSettings() {
    // Required empty public constructor
}

public static FragmentSettings newInstance(String param1, String param2) {
    FragmentSettings fragment = new FragmentSettings();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_settings, container, false);
}
}

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context="com.bhiruva.dashboard.fragments.FragmentSettings">

</FrameLayout>

任何人都可以帮我更新代码吗?提前致谢!

我假设您已成功将片段显示在屏幕上,而您只想添加一个简单的 ListView。操作方法如下:

1- 将 ListView 添加到您的 fragmnet xml,id 为:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

2- 在片段的 onCreateView 中获取对它的引用:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View contentView = inflater.inflate(R.layout.fragment_settings, container, false);
        ListView listView = contentView.findViewById(R.id.listview);
        return contentView;
    }

3- 为您的 ListView 创建自定义适配器,扩展 BaseAdapter:

    class CustomAdapter extends BaseAdapter {
        List<String> items;

        public CustomAdapter(List<String> items) {
            super();
            this.items = items;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i) {
            return items.get(i);
        }

        @Override
        public long getItemId(int i) {
            return items.get(i).hashCode();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView textView = new TextView(getContext());
            textView.setText(items.get(i));
            return textView;
        }
    }

我在这里创建了一个简单的,它只使主机成为一个字符串列表,并用一个 TextView 显示每个字符串,但是你可以用任何你想要的替换它。

4- 将 ListView 的适配器设置为您的自定义适配器:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View contentView = inflater.inflate(R.layout.fragment_settings, container, false);

        ListView listView = contentView.findViewById(R.id.listview);
        // sample data
        List<String> list = new ArrayList<>();
        for(int i=0;i<100;i++)
            list.add("Item "+i);

        CustomAdapter listAdapter = new CustomAdapter(list);
        listView.setAdapter(listAdapter);
        return contentView;
    }

完成。您在包含所有项目的片段中获得一个 ListView。正如其他人指出的那样,当您有大量项目列表时,ListView 不是首选,在这种情况下最好使用 RecyclerView。