RecyclerView 项目未显示
Posted
技术标签:
【中文标题】RecyclerView 项目未显示【英文标题】:RecyclerView Item not showing 【发布时间】:2021-12-20 10:03:24 【问题描述】: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_
android:layout_
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@+id/addNotificationPanel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.207">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notificationPanelRecView"
android:layout_
android:layout_
android:layout_marginTop="5dp"
android:visibility="visible" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
notification_panel_cardview.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/notificationPanelParent"
android:layout_
android:layout_
android:layout_margin="5dp"
app:cardCornerRadius="7dp"
app:cardElevation="4dp">
<RelativeLayout
android:layout_
android:layout_>
<EditText
android:id="@+id/notificationName"
android:layout_
android:layout_
android:layout_marginLeft="5dp"
android:maxLength="29"
android:text="Notification"
android:textSize="14sp"
tools:ignore="TouchTargetSizeCheck" />
<CheckBox
android:id="@+id/enableNotificationCheckbox"
android:layout_
android:layout_
android:layout_marginRight="4dp"
android:layout_toLeftOf="@+id/deleteNotificationPanel"
android:gravity="fill"
tools:ignore="TouchTargetSizeCheck" />
<ImageButton
android:id="@+id/deleteNotificationPanel"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="6dp"
android:background="@null"
android:src="@android:drawable/ic_delete"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<TextView
android:id="@+id/notifyEvery"
android:layout_
android:layout_
android:layout_below="@+id/notificationName"
android:layout_marginLeft="6dp"
android:layout_marginTop="1dp"
android:text="Notify me every :"
android:textSize="17sp" />
<Spinner
android:id="@+id/hoursSpinner"
android:layout_
android:layout_
android:layout_below="@+id/notificationName"
android:layout_toRightOf="@+id/notifyEvery"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<RelativeLayout
android:layout_
android:layout_
android:layout_below="@+id/notifyEvery"
android:layout_marginTop="5dp">
<CheckBox
android:id="@+id/mondayCheckBox"
android:layout_
android:layout_
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Mon"
android:textSize="13sp" />
<CheckBox
android:id="@+id/tuesdayCheckBox"
android:layout_
android:layout_
android:layout_toRightOf="@+id/mondayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Tue"
android:textSize="13sp" />
<CheckBox
android:id="@+id/wednesdayCheckBox"
android:layout_
android:layout_
android:layout_toRightOf="@+id/tuesdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Wed"
android:textSize="13sp" />
<CheckBox
android:id="@+id/thursdayCheckBox"
android:layout_
android:layout_
android:layout_toRightOf="@id/wednesdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Thu"
android:textSize="13sp" />
<CheckBox
android:id="@+id/fridayCheckBox"
android:layout_
android:layout_
android:layout_toRightOf="@+id/thursdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Fri"
android:textSize="13sp" />
<CheckBox
android:id="@+id/saturdayCheckBox"
android:layout_
android:layout_
android:layout_toRightOf="@+id/fridayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Sat"
android:textSize="13sp" />
<CheckBox
android:id="@+id/sundayCheckBox"
android:layout_
android:layout_
android:layout_toRightOf="@id/saturdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Sun"
android:textSize="13sp"
tools:ignore="TouchTargetSizeCheck" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
NotificationPanel.java
public class NotificationPanel
private int id;
private String notificationName ;
public NotificationPanel(int id, String notificationName)
this.id = id;
this.notificationName = notificationName;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getNotificationName()
return notificationName;
public void setNotificationName(String notificationName)
this.notificationName = notificationName;
@Override
public String toString()
return "NotificationPanel" +
"id=" + id +
", notificationName='" + notificationName + '\'' +
'';
NotificationPanelRecViewAdapter.java
public class NotificationPanelRecViewAdapter extends RecyclerView.Adapter<NotificationPanelRecViewAdapter.ViewHolder>
private String TAG = "NotificationPanelRecViewAdapter";
private ArrayList<NotificationPanel> notificationPanel = new ArrayList<>();
private Context mContext;
public NotificationPanelRecViewAdapter(Context mContext)
this.mContext = mContext;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.notification_panel_cardview,null,false);
ViewHolder holder = new ViewHolder(view);
return holder;
@Override
public void onBindViewHolder(@NonNull ViewHolder holder,final int position)
Log.d(TAG,"onBindViewHolder: Called");
holder.notificationName.setText(notificationPanel.get(position).getNotificationName());
@Override
public int getItemCount()
return 0;
public void setNotificationPanel(ArrayList<NotificationPanel> notificationPanel)
this.notificationPanel = notificationPanel;
notifyDataSetChanged();
public class ViewHolder extends RecyclerView.ViewHolder
private CardView parent;
private EditText notificationName ;
private CheckBox monCheckBox ,tueCheckBox,wedCheckBox,thuCheckBox,friCheckBox,satCheckBox,sunCheckBox,enableNotifCheckbox;
private TextView notifyEvery;
private Spinner hoursSpinner;
private ImageButton deleteNotificationPanel;
public ViewHolder(@NonNull View itemView)
super(itemView);
parent = itemView.findViewById(R.id.notificationPanelParent);
notificationName = itemView.findViewById(R.id.notificationName);
monCheckBox = itemView.findViewById(R.id.mondayCheckBox);
tueCheckBox = itemView.findViewById(R.id.tuesdayCheckBox);
wedCheckBox = itemView.findViewById(R.id.wednesdayCheckBox);
thuCheckBox = itemView.findViewById(R.id.thursdayCheckBox);
friCheckBox = itemView.findViewById(R.id.fridayCheckBox);
satCheckBox = itemView.findViewById(R.id.saturdayCheckBox);
sunCheckBox = itemView.findViewById(R.id.sundayCheckBox);
enableNotifCheckbox = itemView.findViewById(R.id.enableNotificationCheckbox);
notifyEvery = itemView.findViewById(R.id.notifyEvery);
hoursSpinner = (Spinner) itemView.findViewById(R.id.hoursSpinner);
deleteNotificationPanel = itemView.findViewById(R.id.deleteNotificationPanel);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(mContext,R.array.hoursSpinnerValues,R.layout.notification_panel_cardview);
MainActivity.java
公共类 MainActivity 扩展 AppCompatActivity
private RecyclerView notificationPanelRecView;
private NotificationPanelRecViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
notificationPanelRecView = findViewById(R.id.notificationPanelRecView);
ArrayList<NotificationPanel> notificationPanel = new ArrayList<>();
notificationPanel.add(new NotificationPanel(1, "Pills"));
NotificationPanelRecViewAdapter adapter = new NotificationPanelRecViewAdapter(this);
adapter.setNotificationPanel(notificationPanel);
notificationPanelRecView.setLayoutManager(new LinearLayoutManager(this));
notificationPanelRecView.setAdapter(adapter);
我制作了一个 ArrayList,在 MainActivity 中添加了一项,只是为了测试布局,我的问题是它根本没有显示。任何关于为什么会发生这种情况的提示都会有所帮助。---------------------------------------- -------------------------------------------------- ----------------------
【问题讨论】:
【参考方案1】:如果适配器认为没有要绑定的内容,则不会绑定任何内容。
@Override
public int getItemCount()
return 0;
需要
@Override
public int getItemCount()
return notificationPanel.size();
【讨论】:
非常感谢。我完全想念那个,我想知道它哪里出错了以上是关于RecyclerView 项目未显示的主要内容,如果未能解决你的问题,请参考以下文章
Android RecyclerView 绘制流程及Recycler缓存
Android RecyclerView 绘制流程及Recycler缓存