RecyclerView 适配器跳过布局问题
最近写了很多RecyclerView的代码,本来一切都好,但是有一次在写一个xml中含有多个recyclerview的时候出现了这样的错误。目前还没有头绪,请各位大神指导一二。
代码如下:
xml布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefreashLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/onlineRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/offlineRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/strangerRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
然后是fragment代码
public class Contacts extends Fragment {
View view;
SwipeRefreshLayout swipeRefreshLayout;
RecyclerView online_recycler, offline_recycler, stranger_recycler;
public Contacts() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_contacts, null);
bindFragmentView();
initFragmentView();
operateView();
return view;
}
@Override
public void bindFragmentView() {
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreashLayout);
online_recycler = (RecyclerView) view.findViewById(R.id.onlineRecyclerView);
online_recycler.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
online_recycler.setItemAnimator(new DefaultItemAnimator());
offline_recycler = (RecyclerView) view.findViewById(R.id.offlineRecyclerView);
offline_recycler.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
offline_recycler.setItemAnimator(new DefaultItemAnimator());
stranger_recycler = (RecyclerView) view.findViewById(R.id.strangerRecyclerView);
stranger_recycler.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
stranger_recycler.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void initFragmentView() {
swipeRefreshLayout.setColorSchemeResources(R.color.swipe_color_1, R.color.swipe_color_2,
R.color.swipe_color_3, R.color.swipe_color_4);
swipeRefreshLayout.setProgressBackgroundColor(R.color.swipe_background_color);
swipeRefreshLayout.setProgressViewEndTarget(true, 200);
// swipeRefreshLayout.setDistanceToTriggerSync(100);
// swipeRefreshLayout.setProgressViewOffset(true, 50, 100);
swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() {
//TODO
refresh();
}
};
swipeRefreshLayout.setOnRefreshListener(listener);
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
}
});
listener.onRefresh();
}
void refresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1500);
presenter.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void operateView() {
}
@Override
public void showContact(UserContactsEntity entity) {
int[] to = {R.id.user_icon, R.id.user_name};
ContactAdapter online_adapter = new ContactAdapter(entity.getOnline(), getActivity().getApplicationContext(),
R.layout.layout_fragment_contacts_item, to);
online_recycler.setAdapter(online_adapter);
ContactAdapter offline_adapter = new ContactAdapter(entity.getOffline(), getActivity().getApplicationContext(),
R.layout.layout_fragment_contacts_item, to);
offline_recycler.setAdapter(offline_adapter);
ContactAdapter stranger_adapter = new ContactAdapter(entity.getOffline(), getActivity().getApplicationContext(),
R.layout.layout_fragment_contacts_item, to);
stranger_recycler.setAdapter(stranger_adapter);
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void answerError() {
Toast.makeText(getActivity(), "网络错误", Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
}
}
项目用mvp架构写的,有些代码我删除了。
showcontact() 是view层的接口实现,数据由presenter层通知model给的回调。亲测数据没有问题。
接下来是adapter代码
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyViewHolder> {
List<UserContactsEntity.ContactBean> list;
Context context;
Integer layout;
int[] to;
protected ImageLoader imageLoader = ImageLoader.getInstance();
public ContactAdapter(List<UserContactsEntity.ContactBean> list,
Context context,
Integer layout,
int[] to) {
this.context = context;
this.list = list;
this.layout = layout;
this.to = to;
}
@Override
public ContactAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(layout, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);
return holder;
}
@Override
public void onBindViewHolder(ContactAdapter.MyViewHolder holder, int position) {
holder.username.setText(list.get(position).getFullname());
String iconurl = list.get(position).getProfileimageurl();
holder.icon.setTag(iconurl);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.dot_free)
.showImageOnFail(R.drawable.dot_free)
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
imageLoader.displayImage(iconurl, holder.icon, options);
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
MyCircleImageView icon;
TextView username;
public MyViewHolder(View itemView) {
super(itemView);
icon = (MyCircleImageView) itemView.findViewById(to[0]);
username = (TextView) itemView.findViewById(to[1]);
}
}
}
总的就这么多,最开始调试之后没有显示数据,我以为是scrollview嵌套的问题,然后我改了好几次,最后看控制台提示的是
04-07 01:58:00.411 23713-23713/? E/RecyclerView: No adapter attached; skipping layout
04-07 01:58:00.411 23713-23713/? E/RecyclerView: No adapter attached; skipping layout
现在有点迷茫,因为这样的代码我写过好多次了。一个界面只有一个recyclerview的时候没有问题。
多个叠加就出错了。求大神指导。