RecyclerView Not Showing up



我有一些文本视图和一个要显示在RecyclerView顶部的按钮。我似乎做的每件事都很好,但不知怎么的,什么都没有表现出来。我还检查了日志,RecyclerView应该有数据!

这不是第一次显示RecyclerViews。然而,这是我第一次在RecyclerView上显示一些元素

  1. activity_home.xml-这是的主主页

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_RL"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
    android:id="@+id/top_LL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">
    <EditText
    android:id="@+id/first_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_weight="2"
    android:hint="FirstName" />
    <EditText
    android:id="@+id/last_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_weight="2"
    android:hint="LastName" />
    <ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />
    <Button
    android:id="@+id/submit"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_weight="1"
    android:onClick="goButton"
    android:text="GO" />
    <Button
    android:id="@+id/load"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_marginEnd="4dp"
    android:layout_weight="1"
    android:onClick="loadButton"
    android:text="L" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
    android:id="@+id/nameList"
    android:layout_below="@+id/top_LL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">    
    </android.support.v7.widget.RecyclerView></RelativeLayout>
    
  2. name_list_item.xml

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
    android:id="@+id/list_first_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    tools:text="first name"/>
    <TextView
    android:id="@+id/list_last_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    tools:text="second name"/>
    </LinearLayout>
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray" />
    

  3. 名称适配器

    公共类NameAdapter扩展了RecyclerView.Adapter{

    private List<Name> mNameList;
    private Context mContext;
    public NameAdapter(Context context) {
    mContext = context;
    }
    @Override
    public NameViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
    View view = inflater.inflate(R.layout.name_list_item, viewGroup, false);
    return new NameViewHolder(view);
    }
    @Override
    public void onBindViewHolder(final NameViewHolder nameViewHolder, int position) {
    String firstName = mNameList.get(position).getFirst();
    String lastName = mNameList.get(position).getLast();
    nameViewHolder.mListItemFirstName.setText(firstName);
    nameViewHolder.mListItemLastName.setText(lastName);
    }
    @Override
    public int getItemCount() {
    if (mNameList == null) {
    return 0;
    }
    return mNameList.size();
    }
    
    class NameViewHolder extends RecyclerView.ViewHolder {
    TextView mListItemFirstName;
    TextView mListItemLastName;
    private NameViewHolder(View itemView) {
    super(itemView);
    mListItemFirstName = itemView.findViewById(R.id.list_first_name);
    mListItemLastName = itemView.findViewById(R.id.list_last_name);
    }
    }
    
    public void swapNameList(List<Name> nameList) {
    if (nameList != null) {
    nameList.clear();
    }
    mNameList = nameList;
    this.notifyDataSetChanged();
    }    
    

    }

更新:问题已解决新代码应该是:

public void swapNameList(List<Name> nameList) {
if (mNameList != null) {
mNameList.clear();
}
mNameList = nameList;
this.notifyDataSetChanged();
}    

问题在这里

if (nameList != null) {
nameList.clear();
}

您正在清除作为参数传递的列表。这个

public void swapNameList(List<Name> nameList) {
mNameList = nameList;
this.notifyDataSetChanged();
}

应该修复它。(您不需要清除任何内容,因为每次调用swapNameList时都会覆盖列表(

解决方案:这里的问题是,在分配给适配器之前,您要将列表转发并清除,因此:

代替:

nameList.clear();

写入:

mNameList.clear();

仅此而已。

最新更新