app不在recyclerview上显示项目

xyhw6mcr  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(388)

我是新来安卓工作室的。我试图在recycleview中显示项目,但它没有显示任何内容。
android studio如是说:

E/RecyclerView: No layout manager attached; skipping layout

我的片段:

public class Articles extends Fragment {
    View root;
    RecyclerView recyclerView;
    NewsList[] myListData = new NewsList[] {
            new NewsList("Article 1", "Some Author"),
            new NewsList("Article2", "Some Author"),
            new NewsList("Article3", "Some Author"),

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_articles, container, false);
        recyclerView = root.findViewById(R.id.recyclerView);
        NewsAdapter adapter = new NewsAdapter(myListData);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);

        return root;
    }

下面是我的适配器类的代码:

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    private NewsList[] listdata;

    // RecyclerView recyclerView;
    public NewsAdapter(NewsList[] listdata) {

        this.listdata = listdata;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final NewsList myListData = listdata[position];
        holder.textView1.setText(listdata[position].getTitle());
        holder.textView2.setText(listdata[position].getAuthor());
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(),"click on item: "+myListData.getTitle(),Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return listdata.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView1;
        public TextView textView2;
        public RelativeLayout relativeLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            this.textView1 = (TextView) itemView.findViewById(R.id.textView1);
            this.textView2 = (TextView) itemView.findViewById(R.id.textView2);
            relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);
        }
    }

以前我有一个错误,应用程序在打开片段时崩溃,但现在它跳过了recyclerview。如果你能帮我解决这个问题,我将不胜感激。

mpgws1up

mpgws1up1#

必须为您的recyclerview设置布局管理器。例如,可以通过在膨胀视图后添加以下内容来使用线性视图:

recyclerView.setLayoutManager(new LinearLayoutManager( context ));

上下文就是你的上下文

gpfsuwkq

gpfsuwkq2#

你需要添加一个 layoutManager 您的回收者视图:

public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_articles, container, false);
        recyclerView = root.findViewById(R.id.recyclerView);
        NewsAdapter adapter = new NewsAdapter(myListData);
        recyclerView.setHasFixedSize(true);
        // Add this line
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()))
        recyclerView.setAdapter(adapter);

        return root;
    }

相关问题