Java 未使用recyclerview和cardview显示底部导航数据

Java 未使用recyclerview和cardview显示底部导航数据,java,android,android-recyclerview,Java,Android,Android Recyclerview,您好,在下面的代码中,我正在使用底部导航,它包含不同的选项,其中一个选项是account。如果单击account底部选项卡,希望在recyclerviews中显示cardview 但数据并没有带来任何东西。它显示为空 使用下面的xml以listview格式显示内容 accounts.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com

您好,在下面的代码中,我正在使用底部导航,它包含不同的选项,其中一个选项是account。如果单击account底部选项卡,希望在recyclerviews中显示cardview

但数据并没有带来任何东西。它显示为空

使用下面的xml以listview格式显示内容

accounts.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>
Fragment_account.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="20dp"
            android:background="@color/White">
            <android.support.v7.widget.CardView
                android:id="@+id/cardView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                app:cardBackgroundColor="@color/slivergray"
                app:cardCornerRadius="8dp"
                app:cardElevation="4dp"
                app:cardUseCompatPadding="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:background="@color/White">

                    <ImageView
                        android:id="@+id/appImage"
                        android:layout_width="72dp"
                        android:layout_height="72dp"
                        android:layout_marginLeft="16dp"
                        android:background="@drawable/ic_account_circle_black_24dp"
                        android:backgroundTint="@color/gray"
                        android:scaleType="centerCrop"
                        tools:ignore="ContentDescription"/>

                    <TextView
                        android:id="@+id/headingText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toRightOf="@+id/appImage"
                        android:paddingLeft="16sp"
                        android:paddingRight="16dp"
                        android:text="Apollo Hospital"
                        android:textColor="@color/black"
                        android:textSize="18sp"
                        tools:ignore="RtlHardcoded"/>

                    <TextView
                        android:id="@+id/subHeaderText"
                        style="@style/Base.TextAppearance.AppCompat.Subhead"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/headingText"
                        android:layout_toRightOf="@+id/appImage"
                        android:paddingLeft="16dp"
                        android:text="Hospital"
                        android:paddingRight="16dp"
                        android:textColor="@color/gray"
                        android:textSize="15sp"/>

                    <TextView
                        android:id="@+id/subHeadingText"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/subHeaderText"
                        android:layout_toRightOf="@+id/appImage"
                        android:gravity="left"
                        android:lines="5"
                        android:maxLines="5"
                        android:paddingBottom="16dp"
                        android:paddingLeft="16dp"
                        android:paddingTop="16dp"
                        android:paddingRight="16dp"
                        android:text="stories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detail"
                        android:textColor="@color/gray"
                        android:textSize="14sp"/>
                    <TextView
                        android:id="@+id/textViewOptions"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true"
                        android:paddingLeft="@dimen/activity_horizontal_margin"
                        android:text="&#8942;"
                        android:textColor="@color/gray"
                        android:layout_marginRight="10dp"
                        android:textAppearance="?android:textAppearanceLarge"
                        />
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:layout_below="@id/subHeadingText">

                        <LinearLayout
                            android:layout_width="209dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left"
                            android:layout_marginLeft="30dp"
                            android:orientation="horizontal">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:backgroundTint="@color/gray"
                                android:src="@drawable/ic_account_circle_black_24dp"></ImageView>

                            <Button
                                android:id="@+id/action1"
                                style="@style/Base.Widget.AppCompat.Button.Borderless"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Action 1"
                                android:textColor="@color/gray"
                                android:textSize="14sp"
                                android:textStyle="bold" />

                        </LinearLayout>
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:layout_gravity="right">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:src="@drawable/ic_account_circle_black_24dp"
                                android:layout_marginLeft="50dp"></ImageView>

                            <Button
                                android:id="@+id/action2"
                                style="@style/Base.Widget.AppCompat.Button.Borderless"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Action 2"
                                android:textColor="@color/gray"
                                android:textSize="14sp"
                                android:textStyle="bold" />

                        </LinearLayout>

                    </LinearLayout>
                </RelativeLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>

    </RelativeLayout>
AccountFragment.java:

    public class AccountFragement extends Fragment {

        public AccountFragement(){

        }

       RecyclerView rv;
        private List<List_Data> list_data;

        private MyAdapter adapter;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment


            View rootView = inflater.inflate (R.layout.account, container, false);


            rv=(RecyclerView) rootView.findViewById(R.id.recyclerview);
            //rv.setHasFixedSize(true);
            rv.setLayoutManager(new LinearLayoutManager(getContext()));
            list_data=new ArrayList<>();
            adapter=new MyAdapter(list_data,getContext());
            rv.setAdapter(adapter);

            return rootView;

        }

        private void setupData(List<List_Data> list_data) {

            adapter=new MyAdapter(list_data,getContext());
            rv.setAdapter(adapter);

        }
    }

List_Data.java:

    public class List_Data {



         String title;



        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }




    }
MyAdapter.java:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<List_Data>list_data;
    private Context context;
    private String title1;

    public MyAdapter(List<List_Data> list_data, Context context) {
        this.list_data = list_data;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_account,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        List_Data listData=list_data.get(position);
       // title1=list_data.get(0).getTitle();
        holder.title.setText(listData.getTitle());


    }

    @Override
    public int getItemCount() {
        return list_data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView title,subtile;
        public ViewHolder(View itemView) {
            super(itemView);

            title=(TextView)itemView.findViewById(R.id.headingText);
            subtile=(TextView)itemView.findViewById(R.id.subHeaderText);
            title.setText(title1);
        }
    }
}

您编写的代码有点不完整,首先您无法查看任何数据,因为AccountFragment中的List_数据中没有数据,因此适配器不知道从何处获取数据

当列表中有数据时,可以填充到适配器

我建议您遵循下面链接中的最佳方法,如果您有任何问题,请务必让我们知道

并尽最大努力开始在项目中使用AndroidX