我正面臨水平recyclerview適配器中的問題。我的適配器在位置0包含imageview,其餘位置爲空。 當我從左向右滾動時,我的圖像從位置1重新出現到4和7.如果我從左向右滾動,我的圖像視圖出現在所有位置,這裏是我的適配器類。我已將此水平RecyclerView適配器放入一個垂直RecyclerView適配器。RecyclerView水平滾動數據隨機更改
HomeBannerAdapter.Java
公共類HomeBannerAdapter擴展RecyclerView.Adapter {
private List<HomePageList> mHomepageList;
private Context mContext;
public HomeBannerAdapter(Context context, List<HomePageList> mHomepageList) {
this.mHomepageList = mHomepageList;
this.mContext = context;
}
@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, viewGroup, false);
SingleItemRowHolder mh = new SingleItemRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(final SingleItemRowHolder holder, final int position) {
final HomePageList singleItem = mHomepageList.get(position);
holder.mTitle.setText(singleItem.getTitle());
if (!singleItem.getBannerUrl().isEmpty()) {
Glide.with(mContext).load(singleItem.getBannerUrl().get(0)).placeholder(R.drawable.placeholder).dontAnimate().into(holder.itemImage);
Log.e("Homebanner not empty", "url" + singleItem.getBannerUrl().toString() + "position" + position);
} else {
Log.e("Homebanner url empty", "url" + singleItem.getBannerUrl().toString() + "position" + position);
}
}
@Override
public int getItemCount() {
return (null != mHomepageList ? mHomepageList.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
public ImageView itemImage;
//int position = getAdapterPosition();
public TextView mTitle;
public HomePageList mHomePageList;
public SingleItemRowHolder(View view) {
super(view);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
this.mTitle = (TextView) view.findViewById(R.id.tvTitle);
// mHomePageList = albumList.get(getPosition());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), mTitle.getText().toString() + " position " + getPosition() + "getAdapterPosition" + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
這裏是我的垂直Recyclerview適配器
HomeDataAdapter.Java
公共類HomeDataAdapt呃擴展RecyclerView.Adapter {
private List<FormList> formList;
private Context mContext;
private List<HomePage> homePageList;
private List<ResourceType> resourceList;
final int VIEW_TYPE_HOMEPAGE = 0;
final int VIEW_TYPE_FORMPAGE = 1;
final int VIEW_TYPE_RESOURCEPAGE = 2;
public HomeDataAdapter(Context context, List<HomePage> homePageList) {
this.homePageList = homePageList;
this.mContext = context;
}
public HomeDataAdapter(Context context, List<HomePage> homePageList, List<FormList> formList) {
this.formList = formList;
this.homePageList = homePageList;
this.mContext = context;
}
public HomeDataAdapter(Context context, List<HomePage> homePageList, List<FormList> formList, List<ResourceType> resourceList) {
this.resourceList = resourceList;
this.formList = formList;
this.homePageList = homePageList;
this.mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
switch (position) {
case VIEW_TYPE_HOMEPAGE:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
ViewHolder homeView = new ViewHolder(v);
return homeView;
case VIEW_TYPE_FORMPAGE:
View v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
ViewHolder formView = new ViewHolder(v1);
return formView;
case VIEW_TYPE_RESOURCEPAGE:
View v2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
ViewHolder resourceView = new ViewHolder(v2);
return resourceView;
}
return null;
}
@Override
public void onBindViewHolder(final ViewHolder itemRowHolder, int position) {
if (position == VIEW_TYPE_HOMEPAGE) {
HomePage mHomePage = homePageList.get(position);
Log.e("albumList", "albumList" + homePageList.get(position).toString() + "position" + position);
itemRowHolder.itemTitle.setText(mHomePage.getHeaderTitle());
HomeBannerAdapter itemListDataAdapter = new HomeBannerAdapter(mContext, mHomePage.getResults());
itemRowHolder.mRecyclerview.setHasFixedSize(true);
itemRowHolder.mRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
itemRowHolder.mRecyclerview.getLayoutManager().scrollToPosition(position);
itemRowHolder.mRecyclerview.getHorizontalOffset();
//itemRowHolder.mRecyclerview.scrollToPositionWithOffset(2, 20);
// itemRowHolder.mRecyclerview.getLayoutManager().smoothScrollToPosition(itemRowHolder.mRecyclerview, null, recyclerAdapter.getItemCount() - 1);
itemRowHolder.mRecyclerview.setAdapter(itemListDataAdapter);
} else if (position == VIEW_TYPE_FORMPAGE) {
position = position - 1;
FormList mFormPage = formList.get(position);
itemRowHolder.itemTitle.setText(formList.get(position).getHeaderTitle());
JobBannerAdapter itemListDataAdapter = new JobBannerAdapter(mContext, mFormPage.getResults());
itemRowHolder.mRecyclerview.setHasFixedSize(true);
itemRowHolder.mRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
itemRowHolder.mRecyclerview.setAdapter(itemListDataAdapter);
} else if (position == VIEW_TYPE_RESOURCEPAGE) {
position = position - 2;
ResourceType mResourceType = resourceList.get(position);
itemRowHolder.itemTitle.setText(mResourceType.getHeaderTitle());
ResourceBannerAdapter itemListDataAdapter = new ResourceBannerAdapter(mContext, resourceList.get(position).getResults());
itemRowHolder.mRecyclerview.setHasFixedSize(true);
itemRowHolder.mRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
itemRowHolder.mRecyclerview.setAdapter(itemListDataAdapter);
}
}
@Override
public int getItemCount() {
int homePageListSize = 0;
int formPageListSize = 0;
int resourcePageListSize = 0;
if (homePageList == null && formList == null && resourceList == null) return 0;
if (resourceList != null)
resourcePageListSize = resourceList.size();
if (formList != null)
formPageListSize = formList.size();
if (homePageList != null)
homePageListSize = homePageList.size();
if (resourcePageListSize > 0 && formPageListSize > 0 && homePageListSize > 0)
return homePageListSize + formPageListSize + resourcePageListSize; // albumlist+formlist+resourceList
if (formPageListSize > 0 && homePageListSize > 0)
return homePageListSize + formPageListSize; // albumlist+formlist
else if (resourcePageListSize > 0 && homePageListSize == 0 && formPageListSize == 0)
return resourcePageListSize; // resourceList
else if (formPageListSize > 0 && homePageListSize == 0)
return formPageListSize; // formlist
else if (formPageListSize == 0 && homePageListSize > 0)
return homePageListSize; // albumlist
else return 0;
// return (null != albumList ? albumList.size()+mFormList.size() : 0);
}
@Override
public int getItemViewType(int position) {
int homePageListSize = 0;
int formPageListSize = 0;
int resourcePageListSize = 0;
if (homePageList == null && formList == null && resourceList == null)
return super.getItemViewType(position);
if (homePageList != null)
homePageListSize = homePageList.size();
if (formList != null)
formPageListSize = formList.size();
if (resourceList != null)
resourcePageListSize = resourceList.size();
if (formPageListSize > 0 && homePageListSize > 0) {
if (position == 0) return VIEW_TYPE_HOMEPAGE;
else if (position == formPageListSize)
return VIEW_TYPE_FORMPAGE;
} else if (formPageListSize == 0 && homePageListSize > 0) {
if (position == 0) return VIEW_TYPE_HOMEPAGE;
else return VIEW_TYPE_FORMPAGE;
} else if (resourcePageListSize > 0 && formPageListSize > 0 && homePageListSize > 0) {
if (position == 0) return VIEW_TYPE_HOMEPAGE;
else if (position == 1)
return VIEW_TYPE_FORMPAGE;
else if (position == 2)
return VIEW_TYPE_RESOURCEPAGE;
}
return super.getItemViewType(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView itemTitle;
public CustomRecyclerView mRecyclerview;
public ViewHolder(View view) {
super(view);
this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
this.mRecyclerview = (CustomRecyclerView) view.findViewById(R.id.recycler_view_list);
}
}
}
Thankyou。我會試試看,並告訴你它是否正常工作。:) – pradeep
非常感謝Savepopulation.Glide似乎沒有爲空的Images分配佔位符。我已經手動實現了空字符串的佔位符。 holder.itemImage.setImageResource(R.drawable.your_place_holder_for_empty_url); – pradeep
沒有它。我會編輯我的答案。 – savepopulation