简体   繁体   English

RecyclerView没有显示

[英]RecyclerView Not Showing up

I have some text views and a button to be displayed on top of a RecyclerView. 我有一些文本视图和一个要显示在RecyclerView顶部的按钮。 I seem to be doing everything right, but somehow nothing is being displayed. 我似乎做对了所有事情,但不知何故没有显示。 I also checked thru logs and the RecyclerView should have data! 我还检查了日志,RecyclerView应该有数据!

This is not the very first time to display RecyclerViews. 这不是第一次显示RecyclerViews。 However, this is the first time I am displaying a RecyclerView with some elements on top of it. 但是,这是我第一次显示带有一些元素的RecyclerView。

  1. activity_home.xml - this is the main home page 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 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. Name Adapter 名称适配器

    public class NameAdapter extends RecyclerView.Adapter { 公共类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(); } 

    } }

UPDATE: ISSUE RESOLVED new code should be: 更新:已解决问题的新代码应为:

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

The problem is here 问题在这里

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

you are clearing the list you pass as parameter. 您正在清除作为参数传递的列表。 This 这个

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

should fix it. 应该修复它。 (You don't need to clear anything since you are overriding the list every time you call swapNameList ) (您不需要清除任何内容,因为每次调用swapNameList时都将覆盖列表)

Solution: The problem here is you are carrying the list forward and clearing it off before assigning to the adapter, hence: 解决方案:这里的问题是您要转发该列表,并在分配给适配器之前将其清除,因此:

Instead of: 代替:

nameList.clear();

Write: 写:

mNameList.clear();

That's all. 就这样。

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

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