【问题标题】:How to display ListItem in Fragment如何在片段中显示 ListItem
【发布时间】:2021-09-20 15:28:12
【问题描述】:

我想用 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>

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    那是因为您还没有为适配器分配正确的数据。

    • 您的适配器应扩展 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;
        }
    }
    
    

    您的数据类应该是一个自定义类,该类包含列表中每个项目的正确信息,data.get(position).getText() 中的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

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 2016-11-06
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多