AndroidStudio中RecyclerView不显示问题。
创始人
2024-08-21 02:00:17
0
  1. 检查布局文件中是否有RecyclerView并确认其是否已正确配置。

例子:


  1. 检查RecyclerView是否已在适配器中进行了设置。例如:
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mCustomAdapter = new CustomAdapter(getActivity(), mDataSet);
mRecyclerView.setAdapter(mCustomAdapter);
  1. 确保适配器已正确实现必要的方法。例如:
public class CustomAdapter extends RecyclerView.Adapter {
    //This method will inflate the custom_layout and returning the view holder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_layout, parent, false);
        return new ViewHolder(view);
    }

    //Binding the data into the holder
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText(mDataSet.get(position));
    }

    //Getting the count
    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    //Creating a view holder class for holding the views
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text_view);
        }
    }
}
  1. 检查是否已正确引入支持库。在 build.gradle 文件中添加以下代码:
dependencies {
    compile 'com.android.support:recyclerview-v7:26.1.0'
}

此外,还需确认 compileSdkVersion 和 targetSdkVersion 是否已设置为 26+。

相关内容

热门资讯

2分钟辅助!微乐小程序黑科技,... 2分钟辅助!微乐小程序黑科技,微乐江苏小程序游戏破解下载(开挂)真是真的有挂(2026)1、很好的工...
透视讲解!微乐小程序黑科技,微... 透视讲解!微乐小程序黑科技,微乐小程序有脚本吗(透视)原来是有挂(哔哩哔哩)1、玩家可以在微乐小程序...
五分钟辅助!微乐小程序黑科技,... 五分钟辅助!微乐小程序黑科技,微乐家乡脚本助手(开挂)竟然真的有挂(2026)1、实时微乐小程序黑科...
9分钟安装!微乐小程序免费黑科... 9分钟安装!微乐小程序免费黑科技,微乐小程序免费黑科技下载方法(开挂)好像有挂(2026)微乐小程序...
透视挂!微乐小程序黑科技,微乐... 透视挂!微乐小程序黑科技,微乐小程序黑科技辅助工具2026最新推荐(透视)切实是有挂(哔哩哔哩)1)...
第8分钟辅助!微乐小程序黑科技... 第8分钟辅助!微乐小程序黑科技,陕西微乐挖坑控制器(开挂)果然真的有挂(2026)一、微乐小程序黑科...
辅助透视!微乐小程序免费黑科技... 辅助透视!微乐小程序免费黑科技,陕西微乐小程序攻略(透视)确实真的是有挂(哔哩哔哩)1、微乐小程序免...
第9分钟下载!微乐小程序黑科技... 第9分钟下载!微乐小程序黑科技,微乐小程序黑科技辅助工具2026最新推荐(开挂)一直是有挂(2026...
透视安装!微乐小程序免费黑科技... 透视安装!微乐小程序免费黑科技,微乐智能插件激活码(透视)切实有挂(哔哩哔哩)1)微乐小程序免费黑科...
第九分钟工具!微乐小程序黑科技... 第九分钟工具!微乐小程序黑科技,微信小程序微乐安徽辅助器(开挂)其实是有挂(2026)1、这是跨平台...