简体   繁体   English

RecyclerView不显示项目

[英]RecyclerView not showing items

I have a main activity and a class called RecyclerView_Config where i have configured my Adapter and ViewHolder classes. 我有一个主要活动,还有一个名为RecyclerView_Config的类,在其中配置了Adapter和ViewHolder类。 I am passing an arraylist to the RecyclerView_Config constructor and trying to display the items , but nothing is shown when my app is run. 我正在将一个arraylist传递给RecyclerView_Config构造函数,并尝试显示各项,但是运行我的应用程序时什么也没显示。 But my getItemCount() returns 11, still no items are displayed. 但是我的getItemCount()返回11,仍然没有显示任何项目。

I have checked my code , but as Iam a newbie to Recycler View I cant understand what's happening. 我已经检查了我的代码,但是由于我是Recycler View的新手,所以我无法理解正在发生的事情。 One thing for now is that getItemCount in my RecyclerView_Config class is returning a value >0 which means that my arrayList data is being passed succesfully , but still no data is showing 现在的一件事是RecyclerView_Config类中的getItemCount返回值> 0,这意味着我的arrayList数据已成功传递,但仍然没有数据显示

Main Activity: 主要活动:

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> arrayList;
    private RecyclerView recyclerView;

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

        arrayList=new ArrayList<>();

        recyclerView=findViewById(R.id.recyclerView);
        for(int i=0;i<=10;i++)
            arrayList.add("i = "+i);
        RecyclerView_Config obj=new RecyclerView_Config();
        obj.setConfig(recyclerView,MainActivity.this,arrayList);

    }
}

RecyclerView_Config below: 下面是RecyclerView_Config:

public class RecyclerView_Config {

    private Context mContext;
    private UserAdapter userAdapter;
    public void setConfig(RecyclerView recyclerView,Context context,List<String> arrayList)
    {
        mContext=context;
        userAdapter = new UserAdapter(arrayList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(userAdapter);
    }
    class UserItemView extends RecyclerView.ViewHolder
    {
        private TextView list_item;

        public UserItemView(ViewGroup parent)
        {
            super(LayoutInflater.from(mContext).
                    inflate(R.layout.recycler_view_row,parent,false));

            list_item= (TextView)itemView.findViewById(R.id.textView);


        }

        public void bind(String username)
        {
            Toast.makeText(mContext,"Bind Username: "+username, Toast.LENGTH_SHORT).show();
            list_item.setText(""+username);
        }
    }//eclassBookItemView

    class UserAdapter extends RecyclerView.Adapter<UserItemView>
    {
        private List<String> arrayList;

        public UserAdapter(List<String> arrayList) {
            this.arrayList = arrayList;
        }


        @Override
        public UserItemView onCreateViewHolder(ViewGroup parent, int i) {
            return new UserItemView(parent);
        }

        @Override
        public void onBindViewHolder(UserItemView holder, int position) {
            //Toast.makeText(mContext,"Adapter Username: "+arrayList.get(position)+"\nPos = "+position, Toast.LENGTH_SHORT).show();
            holder.bind(arrayList.get(position));
        }

        @Override
        public int getItemCount() {

            Toast.makeText(mContext,"Size = "+arrayList.size(), Toast.LENGTH_SHORT).show();
            return arrayList.size();
        }
    }
}

activity_main.xml: activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="395dp"
        android:layout_height="715dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

recycler_view_row.xml: recycler_view_row.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="395dp"
        android:layout_height="715dp"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="75sp"
            android:text="Hello World"
            android:textSize="50sp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

I expect all my arrayList data which is nothing but nos. 我期望我所有的arrayList数据都是空。 from 0 to 10 to be displayed 从0到10显示

But the actual result is nothing is showing up. 但是实际结果没有显示出来。

typecast incoming holder to UserItemView in onBindViewHolder method so that desired method can be invoked. 将onBindViewHolder方法中的UserItemView输入持有者转换为类型转换,以便可以调用所需的方法。

Hope this should help. 希望这会有所帮助。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM