Recycleview RecyclerView:没有连接适配器;跳过布局

时间:2016-03-08 08:49:26

标签: android android-recyclerview

我有一个回收演示,但是当我运行演示时,总是显示错误消息" Recycleview RecyclerView:没有附加适配器;跳过布局"

这是我的主要活动类

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;
            }

        }       


    }
}

2 个答案:

答案 0 :(得分:1)

mRecycleView.setAdapter(noteListAdapter);

应该是

 mRecycleView.setAdapter(noteAdapter);

答案 1 :(得分:1)

这是我们常常遇到的常见问题。您已经在 initData 方法中编写了这些代码,这需要一个很长的过程来获取正确的数据,但我们需要先将其初始化所以您现在可以做的是将这些代码复制到{ {1}}

onCreate()

注意:LinearLayoutManager llm= new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRecycleView.setLayoutManager(llm); RecycleViewNoteListAdatper noteAdapter=new RecycleViewNoteListAdatper(this, noteList); mRecycleView.setAdapter(noteAdapter); llmnoteAdapter成为一个类变量。将noteAdapter设为空数组。然后在加载数据后再次设置它再次重复相同的过程,但不要初始化两次。我总是习惯这样做。