我有一个主要活动和一个名为RecyclerView_Config的类,在该类中,我已经配置了Adapter和ViewHolder类。我正在将一个arraylist传递给RecyclerView_Config构造函数,并尝试显示各项,但是运行我的应用程序时什么也没显示。但是我的getItemCount()返回11,仍然没有显示任何项目。
我已经检查了我的代码,但是由于我是Recycler View的新手,所以我无法理解正在发生的事情。 现在有一件事是RecyclerView_Config类中的getItemCount返回值> 0,这意味着我的arrayList数据已成功传递,但仍然没有数据显示
主要活动:
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:
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:
<?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:
<?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>
我希望我所有的arrayList数据都是空。从0到10才能显示
但是实际结果没有显示出来。
答案 0 :(得分:0)
使用onBindViewHolder方法将传入的持有人类型转换为UserItemView,以便可以调用所需的方法。
希望这会有所帮助。
谢谢