Recycler View creating duplicate items - android

I am using Recycler View for creating a list of items and I am getting a duplicate items in the list. I have passed a list of 30 size into the Recycler View Adapter. The created list has 30 items but there are only 3 unique items, all other are repetition of 3 unique items. I am not able to find the bug.
public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.CollectionViewHolder> {
private List<CollectionDataTypeModel> mDataSet = new ArrayList<CollectionDataTypeModel>();
private Activity mActivity;
private String mType;
public CollectionAdapter(List<CollectionDataTypeModel> mDataSet, Activity activity, String type) {
this.mActivity = activity;
this.mDataSet = mDataSet;
this.mType = type;
}
#Override
public CollectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection_cardview_layout, parent, false);
CollectionViewHolder vh = new CollectionViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(final CollectionViewHolder holder, int position) {
final CollectionDataTypeModel collectionData = mDataSet.get(position);
Log.d("Colelction Adapter","name : "+collectionData.getCollectionName());
holder.titleText.setText(collectionData.getCollectionName()+"");
holder.secondaryText.setText(collectionData.getPoiCount()+" attractions");
if (mType.equalsIgnoreCase("Collection")) {
Glide.with(mActivity).load(R.drawable.aman).asBitmap().centerCrop().into(new BitmapImageViewTarget(holder.profileImage) {
#Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mActivity.getResources(), resource);
circularBitmapDrawable.setCircular(true);
holder.profileImage.setImageDrawable(circularBitmapDrawable);
}
});
} else if (mType.equalsIgnoreCase("Destination")) {
holder.profileImageRipple.setVisibility(View.GONE);
} else if (mType.equalsIgnoreCase("Download")) {
holder.profileImageRipple.setVisibility(View.GONE);
holder.viewIcon.setImageResource(R.drawable.clear_icon);
holder.viewCount.setVisibility(View.INVISIBLE);
}
// holder.collectionImage.setImageResource(R.drawable.goldentemple);
Glide.with(mActivity).load(collectionData.getCollectionImage()).into(holder.collectionImage);
holder.likeCount.setText(Integer.toString(collectionData.getLikeCount()));
holder.viewCount.setText(Integer.toString(collectionData.getViewCount()));
holder.likeIconRipple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
#Override
public void onComplete(RippleView rippleView) {
// if(holder.likeIcon.getDrawable()==mActivity.getDrawable(R.drawable.like_icon))
{
holder.likeIcon.setImageResource(R.drawable.like_fill_icon);
}
// else
{
// holder.likeIcon.setImageResource(R.drawable.like_icon);
}
}
});
holder.collectionImageRipple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
#Override
public void onComplete(RippleView rippleView) {
EventBus.getDefault().post(new CollectionMessageEvent(collectionData));
}
});
}
#Override
public int getItemCount() {
return mDataSet.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
public static class CollectionViewHolder extends RecyclerView.ViewHolder {
TextView titleText;
ImageView collectionImage;
TextView secondaryText;
TextView likeCount;
TextView viewCount;
ImageView profileImage;
ImageView viewIcon;
RippleView collectionImageRipple;
RippleView profileImageRipple;
RippleView likeIconRipple;
ImageView likeIcon;
public CollectionViewHolder(View itemView) {
super(itemView);
// View bottombar = findById(itemView,R.id.bottomBar);
collectionImage = findById(itemView, R.id.collection_image);
collectionImageRipple = findById(itemView, R.id.collection_image_ripple);
profileImage = findById(itemView, R.id.profileimage);
profileImageRipple = findById(itemView, R.id.profileimage_ripple);
titleText = findById(itemView, R.id.title_text);
secondaryText = findById(itemView, R.id.secondary_text);
likeCount = findById(itemView, R.id.like_count);
likeIcon = findById(itemView, R.id.like_icon);
likeIconRipple = findById(itemView, R.id.like_icon_ripple);
viewCount = findById(itemView, R.id.view_count);
viewIcon = findById(itemView, R.id.view_icon);
}
}
}

Related

How to show all items of list in recyclerview?

I have an adapter that is showing limited Items on Fragment.
#Override
public int getItemCount() {
int limit = 7;
return Math.min(latestProductModelList.size(),limit);
}
and I want to show all the items of list in recyclerview when I click on ViewAll button using the same adapter on another acitivity.
this is my adapter.
`
public class LatestProductAdapter extends RecyclerView.Adapter<LatestProductAdapter.ViewHolder> {
List<LatestProductModel> latestProductModelList = new ArrayList<>();
Context context;
LatestProductClickInterface latestProductClickInterface;
public LatestProductAdapter(Context context, LatestProductClickInterface latestProductClickInterface) {
this.context = context;
this.latestProductClickInterface = latestProductClickInterface;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
LatestProductModel latestProductModel = latestProductModelList.get(position);
Glide.with(context).load(latestProductModel.getImage()).into(holder.itemImage);
holder.itemTitle.setText(latestProductModel.getTitle());
holder.itemView.setOnClickListener(v -> {
latestProductClickInterface.OnLatestProductClicked(latestProductModelList.get(position));
});
}
#Override
public int getItemCount() {
int limit = 7;
return Math.min(latestProductModelList.size(),limit);
}
#SuppressLint("NotifyDataSetChanged")
public void updateList(List<LatestProductModel> latestProductModels){
latestProductModelList.clear();
latestProductModelList.addAll(latestProductModels);
Collections.reverse(latestProductModelList);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView itemImage;
TextView itemTitle;
public ViewHolder(#NonNull View itemView) {
super(itemView);
itemImage = itemView.findViewById(R.id.item_img);
itemTitle = itemView.findViewById(R.id.item_title);
}
}
}
`
How to achieve this?
Or is there another way to achieve this?
kindly help me.
You can use bellow codes for your adapter:
public class LatestProductAdapter extends RecyclerView.Adapter<LatestProductAdapter.ViewHolder> {
List<LatestProductModel> latestProductModelList = new ArrayList<>();
Context context;
LatestProductClickInterface latestProductClickInterface;
private boolean shouldShowAllItems;
public LatestProductAdapter(Context context, LatestProductClickInterface latestProductClickInterface , boolean shouldShowAllItems) {
this.context = context;
this.latestProductClickInterface = latestProductClickInterface;
this.shouldShowAllItems = shouldShowAllItems;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
LatestProductModel latestProductModel = latestProductModelList.get(position);
Glide.with(context).load(latestProductModel.getImage()).into(holder.itemImage);
holder.itemTitle.setText(latestProductModel.getTitle());
holder.itemView.setOnClickListener(v -> {
latestProductClickInterface.OnLatestProductClicked(latestProductModelList.get(position));
});
}
#Override
public int getItemCount() {
if (shouldShowAllItems){
return latestProductModelList.size();
}else {
int limit = 7;
return Math.min(latestProductModelList.size(), limit);
}
}
#SuppressLint("NotifyDataSetChanged")
public void updateList(List<LatestProductModel> latestProductModels){
latestProductModelList.clear();
latestProductModelList.addAll(latestProductModels);
Collections.reverse(latestProductModelList);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView itemImage;
TextView itemTitle;
public ViewHolder(#NonNull View itemView) {
super(itemView);
itemImage = itemView.findViewById(R.id.item_img);
itemTitle = itemView.findViewById(R.id.item_title);
}
}
}
and create adapter object accourding your need:
LatestProductAdapter latestProductAdapter = LatestProductAdapter(context , this ,//true or false);

(android) How to dynamically add items to the current nested RecyclerView

This picture shows the problem.
I am using nested RecyclerView right now.
What I want is that when the ADD button of the outer RecyclerView is pressed, the item of the inner RecyclerView is added.
But what I made is that no matter which button is pressed, the last added inner RecyclerView's item is added.
How can I add items each the proper RecyclerView position?
This is my code.
RoutineModel.java
public class RoutineModel {
String routine;
public RoutineModel(String routine) {
this.routine = routine;
}
public String getRoutine() {
return routine;
}
}
RoutineAdapter.java
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
public RoutineDetailAdapter detailAdapter;
OnRoutineItemClickListener listener;
public void setOnRoutineClickListener(OnRoutineItemClickListener listener) {
this.listener = listener;
}
public void addItem(RoutineModel item) {
routineItems.add(item);
}
public void addDetailItem() {
detailAdapter.addItem(new RoutineDetailModel());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
detailAdapter = new RoutineDetailAdapter();
holder.setRoutineDetailRecyClerView();
holder.routine_detail.setAdapter(detailAdapter);
detailAdapter.addItem(new RoutineDetailModel());
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineModel curRoutineItem = routineItems.get(position);
holder.setItems(curRoutineItem);
}
#Override
public int getItemCount() {
return routineItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView routine;
Button addSet;
RecyclerView routine_detail;
public ViewHolder(#NonNull View itemView) {
super(itemView);
initViews();
addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.OnItemClick();
}
});
}
private void initViews() {
routine = itemView.findViewById(R.id.routine);
routine_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
}
private void setItems(RoutineModel routineItem) {
routine.setText(routineItem.getRoutine());
}
public void setRoutineDetailRecyClerView() {
routine_detail.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
routine_detail.setHasFixedSize(true);
}
}
public interface OnRoutineItemClickListener {
public void OnItemClick();
}
}
RoutineDetailModel
public class RoutineDetailModel {
String set;
String weight;
String reps;
}
RoutineDetailAdapter
public class RoutineDetailAdapter extends RecyclerView.Adapter<RoutineDetailAdapter.ViewHolder>{
ArrayList<RoutineDetailModel> items = new ArrayList<>();
public void addItem(RoutineDetailModel item) {
items.add(item);
notifyDataSetChanged();
}
public ArrayList<RoutineDetailModel> getItem() {
return this.items;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.routine_detail_item, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineDetailModel item = items.get(position);
holder.setItem(item);
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView set;
public ViewHolder(#NonNull View itemView) {
super(itemView);
set = itemView.findViewById(R.id.set);
}
private void setItem(RoutineDetailModel item) {
set.setText("TEST");
}
}
}
MainActivity
public class WriteRoutineActivity extends AppCompatActivity {
RecyclerView routine_recyclerview;
TextView title;
RoutineAdapter routineAdapter;
LinearLayoutManager routineLayoutManger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_routine);
initViews();
setPageTitle(getIntent());
setRoutineRecyclerview();
routineAdapter = new RoutineAdapter();
routine_recyclerview.setAdapter(routineAdapter);
// add detail item
routineAdapter.setOnRoutineClickListener(new RoutineAdapter.OnRoutineItemClickListener() {
#Override
public void OnItemClick() {
routineAdapter.addDetailItem();
}
});
}
private void initViews() {
title = findViewById(R.id.body_part_detail_title);
routine_recyclerview = findViewById(R.id.routine_recyclerview);
add_routine_btn = findViewById(R.id.add_routine);
}
private void setRoutineRecyclerview() {
routineLayoutManger = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);
routine_recyclerview.setLayoutManager(routineLayoutManger);
routine_recyclerview.setHasFixedSize(true);
}
public void addRoutine(String routine) {
routineAdapter.addItem(new RoutineModel(routine));
routineLayoutManger.scrollToPositionWithOffset(0, 0);
}
}
detailAdapter in RoutineAdapter gets re-assigned every time onCreateViewHolder gets called meaning it will always point to the RoutineDetailAdapter of the last item created in the RecyclerView.
A fix for this could be to pass the RoutineDetailAdapter of the clicked item with the click listener interface.
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
OnRoutineItemClickListener listener;
public void setOnRoutineClickListener(OnRoutineItemClickListener listener) {
this.listener = listener;
}
public void addItem(RoutineModel item) {
routineItems.add(item);
}
public void addDetailItem(RoutineDetailAdapter routineDetailAdapter) {
routineDetailAdapter.addItem(new RoutineDetailModel());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
RoutineDetailAdapter detailAdapter = new RoutineDetailAdapter();
holder.setRoutineDetailRecyClerView();
holder.routine_detail.setAdapter(detailAdapter);
detailAdapter.addItem(new RoutineDetailModel());
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineModel curRoutineItem = routineItems.get(position);
holder.setItems(curRoutineItem);
}
#Override
public int getItemCount() {
return routineItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView routine;
Button addSet;
RecyclerView routine_detail;
public ViewHolder(#NonNull View itemView) {
super(itemView);
initViews();
addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RoutineDetailAdapter routineDetailAdapter = (RoutineDetailAdapter) routine_detail.getAdapter();
listener.OnItemClick(routineDetailAdapter);
}
});
}
private void initViews() {
routine = itemView.findViewById(R.id.routine);
routine_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
}
private void setItems(RoutineModel routineItem) {
routine.setText(routineItem.getRoutine());
}
public void setRoutineDetailRecyClerView() {
routine_detail.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
routine_detail.setHasFixedSize(true);
}
}
public interface OnRoutineItemClickListener {
public void OnItemClick(RoutineDetailAdapter adapter);
}
}
public class WriteRoutineActivity extends AppCompatActivity {
RecyclerView routine_recyclerview;
TextView title;
RoutineAdapter routineAdapter;
LinearLayoutManager routineLayoutManger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_routine);
initViews();
setPageTitle(getIntent());
setRoutineRecyclerview();
routineAdapter = new RoutineAdapter();
routine_recyclerview.setAdapter(routineAdapter);
// add detail item
routineAdapter.setOnRoutineClickListener(new RoutineAdapter.OnRoutineItemClickListener() {
#Override
public void OnItemClick(RoutineDetailAdapter routineDetailAdapter) {
routineAdapter.addDetailItem(routineDetailAdapter);
}
});
}
private void initViews() {
title = findViewById(R.id.body_part_detail_title);
routine_recyclerview = findViewById(R.id.routine_recyclerview);
add_routine_btn = findViewById(R.id.add_routine);
}
private void setRoutineRecyclerview() {
routineLayoutManger = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);
routine_recyclerview.setLayoutManager(routineLayoutManger);
routine_recyclerview.setHasFixedSize(true);
}
public void addRoutine(String routine) {
routineAdapter.addItem(new RoutineModel(routine));
routineLayoutManger.scrollToPositionWithOffset(0, 0);
}
}

recyclerview adapter anonymous arraylist error

I have the 2 errors , can you help me to fix them ?
this error in main activity
This is the code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
//first recycler
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(
new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
OkHttpHandler handler = new OkHttpHandler( this, new OkHttpHandler.MyInterface() {
#Override
public void myMethod(ArrayList result) {
mAdapter = new MyAdapter(result,this);
mAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mAdapter);
// GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), R.layout.grid_item_layout, result);
// adapter.notifyDataSetChanged();
// mGridView.setAdapter(adapter);
}
});
and second error here in my adapter
// viewHolder.txtHeader.setText(...)
final Listitem item;
// final String name = mDataset.get(position);
item = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
this is the full code
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<Listitem> mDataset;
Context mContext;
public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
public TextView txtHeader;
public TextView txtFooter;
public ImageViewHolder(View itemView) {
super (itemView);
txtHeader = (TextView) itemView.findViewById(R.id.firstLine);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine);
}
}
public void add(int position, Listitem item) { //changed from string to listitem
mDataset.add(position, item);
notifyItemInserted(position);
}
public void remove(String item) {
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<Listitem> myDataset, Context context) {
mDataset = myDataset;
mContext = context;
}
// Create new views (invoked by the layout manager)
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
}
private static final int TYPE_IMAGE = 1;
private static final int TYPE_GROUP = 2;
#Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {
ImageViewHolder viewHolder = (ImageViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final Listitem item;
// final String name = mDataset.get(position);
item = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
/* viewHolder.txtFooter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
remove(item);
}
});*/
// viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.size();
}
}
listitem
public class Listitem implements Parcelable {
String id;
//String name;
String url;
Listitem (Parcel in){
this.id = in.readString();
// this.name = in.readString();
this.url = in.readString();
}
Listitem( String name,String url) {
this.id = id;
this.url = url;
}
#Override
public int describeContents() {
return 0;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
// dest.writeString(this.name);
dest.writeString(this.url);
}
public static final Parcelable.Creator<Listitem> CREATOR = new Parcelable.Creator<Listitem>() {
public Listitem createFromParcel(Parcel in) {
return new Listitem(in);
}
public Listitem[] newArray(int size) {
return new Listitem[size];
}
};
}
For the first error:
You need to pass a Context in the adapter constructor.
Actually this inside myMethod(ArrayList result) refers to OkHttpHandler.MyInterface not a Context.
To solve this, change mAdapter = new MyAdapter(result,this);
to mAdapter = new MyAdapter(result,YourActivityName.this);
For the second error:
mDataset.get(position) is returning a Listitem object, while you need a CharSequence (or String) object as parameter with setText() method.
You should do
viewHolder.txtHeader.setText(mDataset.get(position).getUrl());
or
viewHolder.txtHeader.setText(mDataset.get(position).getId());

RecyclerView is showing nothing

I have some items in my adapter but nothing is shown in the RecyclerView.
Adapter
public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {
private List<WorkOrder> orders = new LinkedList<>();
public void setData(List<WorkOrder> orders) {
this.orders = orders;
}
#Override
public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
return new WorkOrderViewHolder(view);
}
#Override
public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
WorkOrder order = orders.get(position);
holder.bind(order);
}
#Override
public int getItemCount() {
return orders.size();
}
}
ViewHolder
public class WorkOrderViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView description;
private TextView date;
public WorkOrderViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title_textview);
description = (TextView) view.findViewById(R.id.description_textview);
date = (TextView) view.findViewById(R.id.date_textview);
}
public void bind(WorkOrder order) {
title.setText("Test");
description.setText("Test");
date.setText("Test");
}
}
Activity (Using AndroidAnnotations)
#EActivity(R.layout.activity_workorders)
#OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {
#ViewById(R.id.orders_recyclerview)
RecyclerView ordersList;
List<WorkOrder> orders = new LinkedList<>();
private WorkOrderAdapter adapter;
{
adapter = new WorkOrderAdapter();
orders.add(new WorkOrder());
orders.add(new WorkOrder());
orders.add(new WorkOrder());
adapter.setData(orders);
}
#AfterViews
public void initViews() {
ordersList.setAdapter(adapter);
}
}
Please add the LayoutManager to the RecyclerView and try again
ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);

About WebView in RecyclerView?

I use the WebView in the first item of RecyclerView, webview load the url include image or video, when i click the image or the video, recyclerview will scroll to the top and webview will continue to load, video will play, how i resolve it?
some code:
public class NewsDetailAdapter extends PageRecyclerAdapter<NewsDetailInfo, ViewHolder> {
private static final boolean DEBUG = true;
public static final int VIEW_TYPE_WEB = 1;
public static final int VIEW_TYPE_COMMENT_TITLE = 2;
public static final int VIEW_TYPE_COMMENT = 3;
private XWebView mWebView;
private OnItemClickListener mItemClickListener;
public NewsDetailAdapter(Context context, XWebView webView, List<NewsDetailInfo> data, OnItemClickListener listener, OnErrorClickListener errorListener) {
super(context, data, errorListener);
mWebView = webView;
mItemClickListener = listener;
}
#Override
public int getListItemViewType(int position) {
return getItem(position).itemType;
}
#Override
public ViewHolder onCreateHolder(LayoutInflater inflater, ViewGroup parent,
int viewType) {
switch (viewType) {
case VIEW_TYPE_WEB:
return new WebViewHolder(inflater.inflate(R.layout.layout_webview, parent, false));
case VIEW_TYPE_COMMENT_TITLE:
return new SimpleTextHolder(inflater.inflate(R.layout.layout_comment_title, parent, false));
case VIEW_TYPE_COMMENT:
return new ListItemHolder(inflater.inflate(R.layout.list_item_comment, parent, false));
}
return null;
}
#Override
public void onBindHolder(ViewHolder viewHolder, final int position) {
if (viewHolder instanceof SimpleTextHolder) {
SimpleTextHolder holder = (SimpleTextHolder) viewHolder;
holder.mText.setText(getItem(position).text);
} else if (viewHolder instanceof ListItemHolder) {
ListItemHolder holder = (ListItemHolder) viewHolder;
holder.mUsername.setText(TextUtils.isEmpty(getItem(position).user.username) ? "匿名" : getItem(position).user.username);
holder.mCommentContent.setText(getItem(position).content);
holder.mPublishTime.setText(TimeUtils.parseToHumnanizeTime(getItem(position).publishTime));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(getItem(position));
}
}
});
}
}
class WebViewHolder extends ViewHolder {
public WebViewHolder(View itemView) {
super(itemView);
FrameLayout frameLayout = (FrameLayout) itemView.findViewById(R.id.web_view_container);
frameLayout.addView(mWebView);
**itemView.requestFocus();** // add this code will resolve it;
}
}
class SimpleTextHolder extends ViewHolder {
TextView mText;
public SimpleTextHolder(View itemView) {
super(itemView);
mText = (TextView) itemView.findViewById(R.id.title);
}
}
class ListItemHolder extends ViewHolder {
ImageView mImageView;
TextView mUsername;
TextView mCommentContent;
TextView mPublishTime;
public ListItemHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.portrait);
mUsername = (TextView) itemView.findViewById(R.id.username);
mCommentContent = (TextView) itemView.findViewById(R.id.comment_content);
mPublishTime = (TextView) itemView.findViewById(R.id.publish_time);
}
}
public interface OnItemClickListener {
void onItemClick(NewsDetailInfo info);
}
}

Categories

Resources