我已将适配器设置为RecyclerView
,我的列表大小为动态(2),我在适配器中发送。
但是当我调试我的代码时,onCreateViewHolder
和onBindViewHolder
从未调用过。
这是我设置适配器的代码。
mRecycler_Downloaded_List.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true));
mRecycler_Downloaded_List.setHasFixedSize(true);
PurchaseListAdapterDownloded = new Recycler_purchase_list_adapter_downloaded(PurchaseList_Dummy.this, mList_downlaoded, false);
mRecycler_Downloaded_List.setAdapter(PurchaseListAdapterDownloded);
这是我的适配器代码,我得到并设置了值。
package com.structure.adapters;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.structure.R;
import com.structure.beans.Response_GetVideoDetail;
import java.util.ArrayList;
/**
* Created by root on 26/4/17.
*/
public class Recycler_purchase_list_adapter_downloaded extends RecyclerView.Adapter<Recycler_purchase_list_adapter_downloaded.ViewHolder> {
Context context;
Typeface face;
ArrayList<Response_GetVideoDetail.ResponseBean.VideodetailsBean> mList;
View view;
boolean mIsDownloadedList;
private LayoutInflater inflater;
public Recycler_purchase_list_adapter_downloaded(Context context, ArrayList<Response_GetVideoDetail.ResponseBean.VideodetailsBean> mList, boolean mIsDownloadedList) {
this.context = context;
this.mList = mList;
this. inflater = LayoutInflater.from(context);
this.mIsDownloadedList = mIsDownloadedList;
this.notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = LayoutInflater.from(context).inflate(R.layout.item_purchase_list, parent, false);
View view = inflater.inflate(R.layout.item_purchase_list, parent, false);
System.out.println("OnCreateView"+"onCreateViewHolder>>>>>>>>>>>>");
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.mtvName.setText(mList.get(position).getName());
holder.mDesc.setText(mList.get(position).getDescription());
holder.itemTime_tv.setText(mList.get(position).getDuration());
Glide.with(context).load(/*APIs.IMGAE_BASE_URL +*/ mList.get(position).getPic())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.dontTransform().dontAnimate()
.error(R.drawable.demo_song).into(holder.mImg);
System.out.println("OnCreateView"+"onBindViewHolder>>>>>>>>>>>>");
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView mtvName, itemTime_tv,mDesc;
private ContentLoadingProgressBar mProgressBar;
private RelativeLayout mLayout;
// private ItemPurchaseListBinding binding;
private ImageView mImg;
public ViewHolder(View itemView) {
super(itemView);
mtvName = (TextView) itemView.findViewById(R.id.name_item);
mDesc = (TextView) itemView.findViewById(R.id.desc);
itemTime_tv = (TextView) itemView.findViewById(R.id.itemtime);
mProgressBar = (ContentLoadingProgressBar) itemView.findViewById(R.id.seekBar);
mImg = (ImageView) itemView.findViewById(R.id.img_songName);
mLayout = (RelativeLayout) itemView.findViewById(R.id.rel_purchaselist);
// itemTime_tv.setTypeface(face);
// mtvName.setTypeface(face);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// getAdapterPosition();
// registration.getImage(getAdapterPosition());
}
});
}
}
}
请帮我解决这个问题。我搜索了一切。 如果有人有解决方案,请分享。我在设置适配器时调试了代码,列表大小为1。 提前致谢。
答案 0 :(得分:0)
如果您将适配器的创建与列表的setter分开,我认为操作适配器会更容易。
您可以从初始化开始:
mRecycler_Downloaded_List.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true));
mRecycler_Downloaded_List.setHasFixedSize(true);
PurchaseListAdapterDownloded = new Recycler_purchase_list_adapter_downloaded(PurchaseList_Dummy.this,false);
mRecycler_Downloaded_List.setAdapter(PurchaseListAdapterDownloded);
然后,当它已经创建时,使用方法setList:
添加列表private void setList(ArrayList<Response_GetVideoDetail.ResponseBean.VideodetailsBean> mList) {
this.mList = mList;
notifyDataSetChanged();
}
答案 1 :(得分:0)
我认为您在mList_downlaoded列表中添加数据之前设置了适配器。因此,如果您在列表中添加数据之前初始化适配器,则只需调用 PurchaseListAdapterDownloded.notifyDataSetChanged();将数据添加到列表后。