Picasso in recyclerview can not auto refresh the display when complete load? - android

i use picasso to display an image in recyclerview, but it won't auto refresh the item when load image complete, i have to manually scroll to make the image be displayed. What have i do to fix it, to make the item of recyclerview auto update the display when completed load?
this is my adapter class :
public class LocationListRecyclerViewAdapter extends RecyclerView
.Adapter<LocationListRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "LocationListRecyclerViewAdapter";
private ArrayList<LocationObject> mDataset;
private static MyClickListener myClickListener;
private Context mContext;
static String imageUrls;
public static String[] thumbnailUrl;
private LayoutInflater inflater;
public LocationListRecyclerViewAdapter(Context context, ArrayList<LocationObject> mDataset) {
this.mContext = context;
this.mDataset = mDataset;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView locName;
TextView locAddress;
TextView locDistance;
RoundedImageView roundedImageView;
public DataObjectHolder(View itemView) {
super(itemView);
this.locName = (TextView) itemView.findViewById(R.id.location_name);
this.locAddress = (TextView) itemView.findViewById(R.id.location_address);
this.locDistance = (TextView) itemView.findViewById(R.id.location_distance);
this.roundedImageView = (RoundedImageView) itemView.findViewById(R.id.thumbnail_location);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public LocationListRecyclerViewAdapter(ArrayList<LocationObject> myDataset) {
mDataset = myDataset;
}
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.location_listcard_item, parent, false);
this.mContext = parent.getContext();
thumbnailUrl = new String[10];
TextView locName = (TextView) view.findViewById(R.id.location_name);
TextView locAddress = (TextView) view.findViewById(R.id.location_address);
TextView locDistance = (TextView) view.findViewById(R.id.location_distance);
Typeface helvetica = Typeface.createFromAsset(parent.getContext().getApplicationContext().getAssets(),"fonts/Helvetica.otf");
locName.setTypeface(helvetica);
locAddress.setTypeface(helvetica);
locDistance.setTypeface(helvetica);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
> #Override
> public void onBindViewHolder(DataObjectHolder holder, int position) {
LocationObject item = mDataset.get(position);
holder.locName.setText(mDataset.get(position).getLocationName());
holder.locAddress.setText(mDataset.get(position).getLocationAddress());
holder.locDistance.setText(String.valueOf(mDataset.get(position).getLocationDistance())+"m");
Log.i(position+" photos"," = "+mDataset.get(position).getLocationPhoto());
//mContext
Picasso.with(holder.roundedImageView.getContext()).cancelRequest(holder.roundedImageView);
Picasso.with(holder.roundedImageView.getContext())
.load(mDataset.get(position).getLocationPhoto())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.resize(200,200)
.into(holder.roundedImageView);
holder.itemView.setTag(item);
}
public void addItem(LocationObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
#Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}

Let Picasso do its job, you do not need to interfere in the download process, the library itself knows when to cancel the request if necessary.
Always helpful before you use the library, carefully explore all of its fine points of using.
http://square.github.io/picasso/
#Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
LocationObject item = mDataset.get(position);
holder.locName.setText(item.getLocationName());
holder.locAddress.setText(item.getLocationAddress());
holder.locDistance.setText(String.valueOf(item.getLocationDistance())+" m");
Picasso.with(mContext)
.load(item.getLocationPhoto())
.placeholder(R.drawable.placeholder)
.resize(200,200)
.centerCrop()
.into(holder.roundedImageView);
}

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);

set onclick for recycleview each view and toast its parameter

Hi I wanna set my recycleview clickable . for example when click on recycleview's each view toast its parameters (like its name) . please don't rank negative I'm a newbie.
this is my mainclass to show recycleview
public class HomeFragment2 extends Fragment {
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
ProgressBar progressBar;
View view ;
int RecyclerViewItemPosition ;
ArrayList<String> ImageTitleNameArrayListForClick;
String ID = "id";
String SECTION = "section";
String TIME = "time";
String NAME = "name";
Button button;
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
public static HomeFragment2 newInstance() {
return new HomeFragment2();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home2, container, false);
GetDataAdapter1 = new ArrayList<>();
recyclerView =v.findViewById(R.id.recyclerView1);
progressBar =v.findViewById(R.id.progressBar1);
button =v.findViewById(R.id.button);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(recyclerViewlayoutManager);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
JSON_DATA_WEB_CALL();
}
});
return v;
}
public void JSON_DATA_WEB_CALL(){
jsonArrayRequest = new JsonArrayRequest(URLS.recycle_fetch,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.GONE);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setId(json.getInt(ID));
GetDataAdapter2.setName(json.getString(SECTION));
GetDataAdapter2.setSubject(json.getString(TIME));
GetDataAdapter2.setPhone_number(json.getString(NAME));
// Adding image title name in array to display on RecyclerView click event.
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, getActivity());
recyclerView.setAdapter(recyclerViewadapter);
}
}
and my adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<GetDataAdapter> getDataAdapter;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
cardView = (CardView) view.findViewById(R.id.mainCard);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
holder.Id.setText(getDataAdapter1.getName());
holder.Section.setText(String.valueOf(getDataAdapter1.getId()));
holder.Time.setText(getDataAdapter1.getPhone_number());
holder.Name.setText(getDataAdapter1.getSubject());
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
}
#Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView Id;
public TextView Section;
public TextView Time;
public TextView Name;
public ViewHolder(View itemView) {
super(itemView);
Id =itemView.findViewById(R.id.textView2);
Section =itemView.findViewById(R.id.textView4);
Time =itemView.findViewById(R.id.textView6);
Name =itemView.findViewById(R.id.textView8);
}
}
}
any body can help me? i wanna get cardview's parameters when click each of them .
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<GetDataAdapter> getDataAdapter;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
//////////////////////////////////here you define your layout you wanna be clicked/////////////////////////
cardView = (CardView) view.findViewById(R.id.mainCard);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
holder.Id.setText(getDataAdapter1.getName());
holder.Section.setText(String.valueOf(getDataAdapter1.getId()));
holder.Time.setText(getDataAdapter1.getPhone_number());
holder.Name.setText(getDataAdapter1.getSubject());
///////////////////////////////// and here you add your on click listener////////////////////////////////
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
}
#Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView Id;
public TextView Section;
public TextView Time;
public TextView Name;
public ViewHolder(View itemView) {
super(itemView);
Id =itemView.findViewById(R.id.textView2);
Section =itemView.findViewById(R.id.textView4);
Time =itemView.findViewById(R.id.textView6);
Name =itemView.findViewById(R.id.textView8);
}
}
}

Recyclerview old item view doesn't remove when click another item?

I have a recyclerview . I click on a item. It is highlighted ( sets background - color) . And then, I click another item , It doesn't remove old highlighted view. My class is very leak . Also could you give me an advice to make more effective ?
public class DrawerMenuShelfListAdapter extends RecyclerView.Adapter<DrawerMenuShelfListAdapter.ViewHolder> implements View.OnClickListener {
private int selectedRow = 0;
public Fragment fragment;
public ViewHolder holder;
public RecyclerView recyclerView;
private ArrayList<ShelfModel> shelfList;
public DrawerMenuShelfListAdapter(Fragment fragment, ArrayList<ShelfModel> shelfList, RecyclerView recyclerView) {
this.shelfList = shelfList;
this.fragment = fragment;
this.recyclerView = recyclerView;
}
public void setItemSelected(int position){
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.transparent));
notifyItemChanged(selectedRow);
selectedRow = position;
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.black));
notifyItemChanged(selectedRow);
}
public void updateList(ArrayList<ShelfModel> list){
shelfList.clear();
shelfList.addAll(list);
notifyDataSetChanged();
notifyItemChanged(selectedRow);
}
#Override
public DrawerMenuShelfListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext())
.inflate(R.layout.navigation_drawer_list_row, parent, false);
holder = new ViewHolder(v);
holder.text = (TextView) v.findViewById(R.id.definitionText);
holder.total = (TextView) v.findViewById(R.id.countText);
holder.editButton = (ImageButton)v.findViewById(R.id.editButton);
holder.text.setTypeface(App.MUSEO_100);
holder.total.setTypeface(App.MUSEO_300);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ShelfModel shelfModel = shelfList.get(position);
holder.text.setText(shelfModel.getName());
holder.total.setText(String.valueOf(shelfModel.getTotal()));
holder.shelfId = shelfModel.getShelfId();
holder.itemView.setOnClickListener(this);
}
#Override
public int getItemCount() {
return shelfList.size();
}
#Override
public void onClick(View v) {
int position = recyclerView.getChildLayoutPosition(v);
((DrawerMenuShelfListListener)fragment).onShelfItemClick(shelfList.get(position));
setItemSelected(position);
}
public interface DrawerMenuShelfListListener extends BaseRecyclerViewListener {
void onShelfItemClick(ShelfModel shelfModel);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public TextView total;
public ImageView editButton;
public int shelfId;
public ViewHolder(View view) {
super(view);
}
}
}
Ok. I found the solution.If I don't call notifyItemChanged , it works

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);
}
}

Toast message onClick NOT SHOWING (RecycleView)

Im trying show a toast msg when clicked on a item from my RecycleView, Ive tried many examples,
but its not giving me anything. Can somebody give me a different example that i can follow, at the end i wanna set the onClick to show a new fragment. If I can get an example on that, it will be great.
Im using this code:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private List<Movie> movies;
private int card_layout;
private Context mContext;
public MovieAdapter(List<Movie> movies, int card_layout, Context context) {
this.movies = movies;
this.card_layout = card_layout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(card_layout, viewGroup, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final Movie movie = movies.get(i);
viewHolder.movieImage.setImageDrawable(mContext.getDrawable(movie.getImageResourceId(mContext)));
viewHolder.movieName.setText(movie.mName);
viewHolder.currentMovie = movie;
}
#Override
public int getItemCount(){
return movies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView movieName;
public ImageView movieImage;
public Movie currentMovie;
public ViewHolder( View itemView) {
super(itemView);
movieName = (TextView) itemView.findViewById(R.id.movieName);
movieImage = (ImageView)itemView.findViewById(R.id.movieImage);
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View itemView){
Toast.makeText(itemView.getContext(),currentMovie.mName,Toast.LENGTH_SHORT ).show();
}
});
}
}
}
Do I have to implement something in my MainActivity as well?
and please dont get mad with me, Im just a starting with all this. All your help will be appriciated. thanks
Layout is not clickable by default. to make clickable add setClickable to true :
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
card_layout, viewGroup, false);
itemView.setClickable(true);
itemView.setFocusableInTouchMode(true);
After wasting an hour of time, I found the most appropriate and simplest solution to this problem:
Try this, it will definitely work.No matters whether cards are used in grids or not.
RecyclerView Adapter:
ProductCardRecyclerViewAdapter.java
public class ProductCardRecyclerViewAdapter extends RecyclerView.Adapter<ProductCardViewHolder> {
public final String TAG=getClass().getSimpleName();
Context context;
private List<ProductEntry> productList;
private Integer[] cardImages;
String[] cardTitle;
String[] cardSubtitle;
public ProductCardRecyclerViewAdapter(Context context, Integer[] imageList, String[] cardTitle, String[] cardSubtitle) {
this.cardImages = imageList;
this.cardTitle = cardTitle;
this.cardSubtitle = cardSubtitle;
this.context = context;
}
#NonNull
#Override
public ProductCardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_card, parent, false);
return new ProductCardViewHolder(layoutView);
}
#Override
public void onBindViewHolder(#NonNull ProductCardViewHolder holder, int position) {
// TODO: Put Recycler ViewHolder Cards binding code here in MDC-102
holder.imgCard.setImageResource(cardImages[position]);
holder.productTitle.setText(cardTitle[position]);
holder.productPrice.setText(cardSubtitle[position]);
holder.productCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Material Card clicked "+cardTitle[position]+" : "+context.getClass());
//TODO: Perform card clicked working
Context c = v.getContext();
Toast.makeText(c, cardTitle[position], Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return cardImages.length;
}
}
RecyclerViewHolder
ProductCardViewHolder.java
public class ProductCardViewHolder extends RecyclerView.ViewHolder {
CardView productCard;
ImageView imgCard;
public TextView productTitle;
public TextView productPrice;
public ProductCardViewHolder(#NonNull View itemView) {
super(itemView);
imgCard = itemView.findViewById(R.id.product_image);
productTitle = itemView.findViewById(R.id.product_title);
productPrice = itemView.findViewById(R.id.product_price);
productCard=itemView.findViewById(R.id.cardofproducts);
// TODO: Find and store views from itemView
}
}
Hope, it will help a lot.

Categories

Resources