当前位置: 首页 > 知识库问答 >
问题:

Recycleview Recycleview:未附加适配器;跳过布局

司空叶五
2023-03-14

我有一个回收演示,但当我运行该演示时,总是显示错误消息“recycleview recycerview:No adapter attached;skipping layout”

这是我主要的activity课

public class MainActivity extends BaseActivity{

//  private ListView mListView;
private ArrayList<Note> noteList;
private RecycleViewNoteListAdatper noteListAdapter;
private SwipeRefreshLayout mSwipeRefreshLayout;
private DrawerLayout drawerLayout;
private View drawerView;
private Toolbar mToolbar;
private RecyclerView mRecycleView;

@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
//      requestWindowFeature(Window.);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initData();
    initListener();
//      colorChange();
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();
        // 很明显,这两货是新API才有的。
//          window.setStatusBarColor(colorBurn(R.color.colorPrimary));
//          window.setNavigationBarColor(colorBurn(R.color.colorPrimary));
    }

}

@SuppressLint("NewApi") 
private void initView() {
    mRecycleView = (RecyclerView) findViewById(R.id.recycler_view);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.setTitle("全部内容");
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
    setSupportActionBar(mToolbar);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(
            this, drawerLayout, mToolbar, R.string.open_string,
            R.string.close_string);
    actionBarDrawerToggle.syncState();
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    mSwipeRefreshLayout.setColorSchemeColors(R.color.colorPrimary);
    mSwipeRefreshLayout.setEnabled(true);
    mSwipeRefreshLayout
            .setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    // 执行刷新操作
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            mSwipeRefreshLayout.setRefreshing(false);
                        }
                    }, 3000);
                }
            });

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);



//          fab.attachToListView(mRecycleView, );
    ScrollDirectionListener listener=  new ScrollDirectionListener() {
          @Override
          public void onScrollDown() {
              LogUtils.d("ListViewFragment", "onScrollDown()");
          }

          @Override
          public void onScrollUp() {
              LogUtils.d("ListViewFragment", "onScrollUp()");
          }
      };/*, new AbsListView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState) {
              LogUtils.d("ListViewFragment", "onScrollStateChanged()");
          }

          @Override
          public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
              LogUtils.d("ListViewFragment", "onScroll()");
          }
      }*/
    fab.attachToRecyclerView(mRecycleView, listener);
}

private void initData() {
    noteList = new ArrayList<Note>();
    Note note = null;
    for (int i = 0; i < 50; i++) {
        note = new Note();
        String md5Encode = MD5Utils.MD5Encode(String.valueOf(System
                .currentTimeMillis()) + i + "note");
        note.setNoteName(md5Encode);
        note.setNoteMd5(md5Encode);
        noteList.add(note);
    }
//      noteListAdapter = new NoteListAdatper(this, noteList);

    //设置 RecycleView的显示方式
    LinearLayoutManager llm= new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    mRecycleView.setLayoutManager(llm);
    RecycleViewNoteListAdatper noteAdapter=new RecycleViewNoteListAdatper(this, noteList);
    mRecycleView.setAdapter(noteListAdapter);

}

private void initListener() {
    drawerLayout.requestDisallowInterceptTouchEvent(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);

}

}

这是我的RecyclerViewAdapter代码

public class RecycleViewNoteListAdatper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    Context mContext;
    private LayoutInflater layoutInflater;
    private ArrayList<Note> list;
    public RecycleViewNoteListAdatper(Context context,ArrayList<Note> note) {
        this.mContext=context;
        this.list=note;
        layoutInflater = LayoutInflater.from(context);  
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
        return new RecyclerViewHolder(layoutInflater.inflate(R.layout.item_notelist_2, paramViewGroup),this,mContext) ;
    }

    @Override
    public void onBindViewHolder(ViewHolder paramVH, int paramInt) {
        if(!ListUtils.isEmpty(list)){
            Note note = list.get(paramInt);
            ((RecyclerViewHolder)paramVH).tv_introduce.setText(note.getNoteName());
        }
    }

    @Override
    public int getItemCount() {
        if(!ListUtils.isEmpty(list)){
            return list.size();
        }
        return 0;
    }
    public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements OnClickListener{
        RecyclerView.Adapter<ViewHolder> mAdapter;
        Context mContext;
        RelativeLayout ll_container;
        LinearLayout ll_action;
        Button action1,action2,action3;
        TextView tv_introduce;
        public RecyclerViewHolder(View view,RecyclerView.Adapter<ViewHolder> adapter,Context context) {
            super(view);
            this.mAdapter=adapter;
            this.mContext=context;
            tv_introduce = (TextView) view.findViewById(R.id.item_introduce);
            action1=(Button) view.findViewById(R.id.button1);
            action2=(Button) view.findViewById(R.id.button2);
            action3=(Button) view.findViewById(R.id.button3);
            ll_container=(RelativeLayout) view.findViewById(R.id.ll_container);
            ll_action=(LinearLayout) view.findViewById(R.id.ll_action);


            action1.setOnClickListener(this);
            action2.setOnClickListener(this);
            action3.setOnClickListener(this);

            ll_container.setOnClickListener(this);

        }
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
            case R.id.button1:
                ToastUtils.showToast(mContext, "onClick1");
                break;
            case R.id.button2:
                ToastUtils.showToast(mContext, "onClick2");
                break;
            case R.id.button3:
                ToastUtils.showToast(mContext, "onClick3");
                break;
            case R.id.ll_container:
                Intent intent=new Intent(mContext,SimpleNoteEditActivity.class);
                mContext.startActivity(intent);
                break;
            default:
                break;
            }

        }       


    }
}

共有1个答案

闾丘京
2023-03-14

这是我们过去经常遇到的问题。您已经在initdata方法中编写了这些代码,这将需要一个很长的过程来获取正确的数据,但我们需要首先初始化它,因此现在您可以做的是将这些代码复制到oncreate()

LinearLayoutManager llm= new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecycleView.setLayoutManager(llm);
RecycleViewNoteListAdatper noteAdapter=new RecycleViewNoteListAdatper(this, noteList);
mRecycleView.setAdapter(noteAdapter);

注意:使LLMNoteAdapterNoteAdapter成为类变量。使notelist为空数组。然后首先设置它,在加载数据后再次重复相同的过程,但不要初始化两次。我过去总是这样做。

 类似资料:
  • 我一直在stackoverflow和这篇博文上阅读不同的答案,并试图实现它们的解决方案,但我仍然得到错误: 片段中的布局: 和项目的布局: 我做错了什么? 编辑:以下是适配器:

  • 我正在使用一个片段来查看一个RecyclerView。但我总是犯这个错误 E/RecyclerView:未附加适配器;跳过布局 我已经为我的recyclerview创建了一个适配器,但布局没有显示。php后端运行良好。谁能帮帮我。下面是我的片段和适配器代码 Tips.java TipAdapter.java

  • 我已经提到了相当少的帖子关于这个错误,但没有一个能够解决我的问题。我有一个片断,在这里我使用修改获得了我的值,我已经在下面显示了: 下面是我的RecyclerView适配器类:

  • 我收到以下错误:“RecyclerView:未附加适配器;跳过布局”但我已附加适配器...我试过很多不同的方法,但都解决不了这个错误。 我正在使用Volley库获取数据。当我启动activity时,直到结束只有进度条可见,并且收到上面的Logcat消息。我已经在中添加了带有适配器的。你能帮帮我吗? 下面是我的代码: 主要活动 地震适配器 mainactivity.xml EquestakerAw.

  • 我需要帮助,我有一个错误,那就是这个:E/recyclerView:没有连接适配器;跳过布局 代码: 公共视图onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){View View=inflater.inflate(r.layout.fragment_chats,container,

  • 按照本指南,我尝试将实现为片段。我得到错误回收视图不显示任何内容 我已尝试 RecycleView未附加适配器;跳过布局 片段中的RecyclerView:没有连接适配器,跳过布局 和更多。 我的代码: