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

时间:2017-01-23 11:08:11

标签: android android-recyclerview android-cardview recycler-adapter

我提到了很少有关此错误的帖子,但没有一个能够解决我的问题。我有一个片段,我使用改造获得我的价值观,我已在下面显示:

public class Tab extends Fragment
{
public String nlink;
public RecyclerView recycler;
public List<Initialise> item;
public Adapter adapter;
public Context mContext;

public Tab(Context mContext)
{
    this.mContext=mContext;
}


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    View v= inflater.inflate(R.layout.tab_layout,container,false);
    item = new ArrayList<>();
    recycler=(RecyclerView)v.findViewById(R.id.recycler);
    recycler.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getContext());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recycler.setLayoutManager(llm);
    recycler.setItemAnimator(new DefaultItemAnimator());
    adapter = new Adapter(item,mContext);
    recycler.setAdapter(adapter);
    return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);
    Bundle bundle=getArguments();
    nlink=bundle.getString("link");
    Retrofit adapter = new Retrofit.Builder()
            .baseUrl(ItemAdapter.ROOT_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Service Serviceapi = adapter.create(Service.class);
    Serviceapi.getitem(nlink).enqueue(new Callback<Item>()
    {
        @Override
        public void onResponse(Call<Item> call, Response<Item> response)
        {
            for(int i=0;i<response.body().getitems().size();i++)
            {
                item.add(new Initialise(response.body().getitems().get(i).getTitle(),response.body().getitems().get(i).getDate(),response.body().getitems().get(i).getcount(),response.body().getitems().get(i).getImage()));
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<Item> call, Throwable t)
        {

        }
    });
}
}

以下是我的RecyclerView Adapter类:

public class Adapter extends RecyclerView.Adapter<Adapter.Holder>
{
static class Holder extends RecyclerView.ViewHolder
{
    CardView cv;
    ImageView thum_img;
    TextView thum_time;
    TextView thum_count;
    TextView thum_text;

    Holder(View itemView)
    {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        thum_img = (ImageView)itemView.findViewById(R.id.thum_img);
        thum_time = (TextView)itemView.findViewById(R.id.thum_time);
        thum_count = (TextView)itemView.findViewById(R.id.thum_scount);
        thum_text = (TextView)itemView.findViewById(R.id.thum_text);
    }
}

private List<Initialise> item;
private Context mContext;

public Adapter(List<Initialise> item, Context mContext)
{
    this.item=item;
    this.mContext=mContext;
}

@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_layout, parent, false);
    return new Holder(v);
}

@Override
public void onBindViewHolder(Holder holder, int position)
{      
    holder.thum_text.setText(item.get(position).gettitle());
    holder.thum_time.setText(item.get(position).gettime());
    holder.thum_count.setText(item.get(position).getcount());
    Picasso.with(mContext).load(item.get(position).getstoryImage()).resize(120, 60).into(holder.thum_img);
}

@Override
public int getItemCount()
{
    return item.size();
}
}

我收到以下错误:

  

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

我的cardview未显示且viewpager为空。我哪里错了?

1 个答案:

答案 0 :(得分:0)

尝试修改onCreateViewHolder()方法

View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_layout,parent,false);
        Holder rcv = new Holder(layoutView,mContext);
        return rcv;