设置背景颜色时回收站视图不显示视图

Posted

技术标签:

【中文标题】设置背景颜色时回收站视图不显示视图【英文标题】:Recycler view not showing the views when a background color is setted 【发布时间】:2019-05-22 00:17:42 【问题描述】:

您好,我是 android Studio 的新手,我正在尝试制作一个包含卡片视图列表的简单回收器视图。代码运行得很好,当我运行它时每个视图都渲染得很好,但后来我决定向回收器视图添加背景颜色,而不是一切都出错了。我唯一改变的是设置回收站视图的背景颜色。顺便说一句,我的回收站视图存在于片段中。

这是我设置背景颜色之前的样子:

recycler视图修改前的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_
    android:layout_>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_
        android:layout_
        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_
    android:layout_>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_
        android:layout_
        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();
    

【问题讨论】:

为父布局设置android:background,而不是RecyclerView 我尝试在包含回收站视图的约束布局中设置背景颜色,但我的视图仍然没有显示。无论如何感谢您的帮助! 【参考方案1】:

在您的代码中,您尝试在 Activity 级别检索 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的那一行,不再需要了。

【讨论】:

以上是关于设置背景颜色时回收站视图不显示视图的主要内容,如果未能解决你的问题,请参考以下文章

按下按钮时更改列表视图背景颜色

设置子视图背景颜色不影响到父视图的背景颜色方法

设置列表视图项模板的背景颜色和列表背景颜色时,Xamarin Forms Listview 选定项背景颜色丢失

如何将图像视图颜色设置为文本字段背景颜色

为啥在 SwiftUI 中显示视图时 List 的背景颜色不同?

如何在滚动时突出显示/更改表格视图部分标题的背景颜色