首页 > 解决方案 > 如何在片段中显示 ListItem

问题描述

我想用 ListView 创建 Fragment,所以,我的 Fragment 活动中有一个带有 listitem 的 ListView,但是当我打开我的 Fragment 时我的 listitem 没有显示,我需要帮助,如何在 Fragment 中显示项目?当我在 Activity 中制作这个 ListView 时,一切正常,如何解决这个问题?我的代码在这里:

FollowFragment.java

public class FollowFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getActivity().setTitle("Подписки");
        return inflater.inflate(R.layout.fragment_follow,container,false);
    }
}

follow_fragment.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"
    android:padding="10dp">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@layout/item_follow"/>

</FrameLayout>

item_follow.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/favourite_match_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp">


        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/team_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fontFamily="@font/roboto_medium"
            android:text="FC Chelsea"
            android:textColor="@color/black"
            tools:ignore="MissingConstraints" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/team_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:fontFamily="@font/roboto_medium"
            android:text="Manchester City"
            app:layout_constraintTop_toBottomOf="@+id/team_1"
            android:textColor="@color/black"
            tools:ignore="MissingConstraints" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/match_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fontFamily="@font/roboto"
            android:text="25 сентября 2021 года"
            android:textColor="@color/colorGrey"
            android:textSize="12sp"
            app:layout_constraintTop_toBottomOf="@id/team_2"
            tools:ignore="MissingConstraints" />


        <ImageButton
            android:layout_width="48dp"
            android:layout_height="50dp"
            android:layout_marginEnd="24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:src="@drawable/ic_love"
            android:background="#00000000"
            app:layout_constraintLeft_toRightOf="@id/team_2"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

标签: javaandroidxml

解决方案


那是因为您尚未为其分配具有正确数据的适配器。

  • 您的适配器应该扩展ArrayAdapter并覆盖getView您应该根据您的数据使用正确的内容设置您的视图。

适配器可能是什么样子:

public class MyAdapter extends ArrayAdapter<YourDataCustomClassHere> {
    ArrayList<YourDataClassHere> data;
    Context context ;
    public MyAdapter(@NonNull Context context, int resource, @NonNull ArrayList<YourDataCustomClassHere> objects) {
        super(context, resource, objects);
        this.data = objects;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_follow, parent, false);
        }
        //example on setting one of your fields 
        ((AppCompatTextView) convertView.findViewById(android.R.id.team_1))
                .setText(data.get(position).getText());
        //set Other Fields here
        return convertView;
    }
}

您的数据类应该是一个自定义类,其中包含列表中每个项目的正确信息,并且getTextdata.get(position).getText()一个任意方法,我认为它将在您的自定义类中获取某种文本,但您可以随意将其替换为你想要什么。

  • 然后在您的 onCreateView 中,您应该像这样将适配器设置为您的 ListView:
public class FollowFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getActivity().setTitle("Подписки");
        View rootView= inflater.inflate(R.layout.fragment_follow,container,false);
        ListView list = rootView.findViewById(R.id.listview);
        MyAdapter myadapter = new MyAdapter(getContext(),R.layout.item_follow,YourDataArrayListHere);
        list.setAdapter(myadapter);
        return rootView; 
    }
}

我相信您会找到自己的方式并将YourDataArrayListHere替换为您的数据自定义类的正确 ArrayList。

无论如何,我希望您重新考虑您对ListView的选择,因为它在运行时消耗大量 Ram(内存)并改用RecyclerView


推荐阅读