嗨,我是Android Studio的新手,我正在尝试制作一个简单的回收站视图,其中包含卡片视图列表。代码运行得很好,当我运行它时,每个视图都可以正常显示,但是后来我决定将背景色添加到回收器视图中,而不是一切都出错了。我唯一更改的是设置回收者视图的背景颜色。顺便说一下,我的回收者视图生活在一个碎片中。
更改之前的回收者视图的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/recycler_view_item" />
</android.support.constraint.ConstraintLayout>
这是新的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
tools:listitem="@layout/recycler_view_item" />
</android.support.constraint.ConstraintLayout>
我的猜测是背景颜色以某种方式覆盖了我的视线? 顺便说一句,我确实注意到logcat中发生了错误:
2018-12-21 22:02:05.515 20644-20644/com.steven97102gmail.todoassistant E/RecyclerView: No adapter attached; skipping layout
但是,即使我没有设置背景,也会发生该错误,并且工作正常。
这是我的Frament课程:
public class RecycleFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter viewAdapter;
private RecyclerView.LayoutManager viewLayouManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.recyler_view, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// get and configure the recycle view
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
// layout manager for recyler view
viewLayouManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(viewLayouManager);
// creates the initial cards
ArrayList<CardItem> card_lists = new ArrayList<>();
for (int i = 0;i<=10;i++){
CardItem card = new CardItem("TODO"+i,"12/" + i);
card_lists.add(card);
}
// adapter for recycler view, initialize the recycler view
viewAdapter = new RecyclerAdapter(card_lists);
recyclerView.setAdapter(viewAdapter);
// test add a new item
card_lists.add(0,new CardItem("Test Insert","12/16"));
viewAdapter.notifyItemInserted(0);
}
}
这是我的“回收者视图”适配器类:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private ArrayList<CardItem> card_item_list;
// view holder is created from the recycler_card_item template
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView title_tv, date_tv;
public RecyclerViewHolder(LinearLayout layout) {
super(layout);
title_tv = layout.findViewById(R.id.card_item_title);
date_tv = layout.findViewById(R.id.card_item_date);
}
}
// take in a list of card items to initialize with
public RecyclerAdapter(ArrayList<CardItem> card_list) {
card_item_list = card_list;
}
@NonNull
@Override
// inflate and creates a viewholder objects, which is from the recycler care item template;
public RecyclerAdapter.RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
// create a new card item
LinearLayout cardLayout = (LinearLayout) LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recycler_card_item, viewGroup, false);
RecyclerViewHolder vh = new RecyclerViewHolder(cardLayout);
return vh;
}
@Override
// bind the CardItem class with the viewholder to complete the card
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
CardItem target_card = card_item_list.get(position);
holder.title_tv.setText(target_card.title);
holder.date_tv.setText(target_card.date);
}
@Override
public int getItemCount() {
return card_item_list.size();
}
}
答案 0 :(得分:2)
在您的代码中,您尝试在“活动”级别检索recyclerview:
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
但是recyclerview在您的片段中。奇怪的是您没有其他错误,我期望该应用程序崩溃。无论如何,应该以这种方式解决:像这样更改您的onCreateView
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.recyler_view, container, false);
recyclerView = root.findViewById(R.id.recycler_view);
return root;
}
不再需要上一行,即从活动中检索recyclerview的那一行。