RecyclerView don't work - android

I have created RecyclerView inside RecyclerView, but for some reason my pictures are not displayed in the internal. Help please!
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case TYPE_SUBSKRIBE_LIKE_AS:
SubscribeLikeAsHolder subscribeLikeAs = (SubscribeLikeAsHolder) holder;
subscribeLikeAs.recyclerViewImagines.setLayoutManager(new LinearLayoutManager(ctx, LinearLayoutManager.HORIZONTAL, false));
subscribeLikeAs.recyclerViewImagines.setAdapter(imageRecyclerViewAdapter);
break;
}
}
public class ImageRecyclerViewAdapter extends RecyclerView.Adapter<ImageRecyclerViewAdapter.ImagesViewHolder>{
private Context ctx;
private LayoutInflater lInflater;
private ArrayList<ItemsImage> images;
public void InForEntries (Context context, ArrayList<ItemsImage> items) {
ctx = context;
images = items;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ImagesViewHolder extends RecyclerView.ViewHolder {
ImageView image;
ImagesViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.imageViewSmall);
}
}
#Override
public ImagesViewHolder onCreateViewHolder(ViewGroup viewGroup, int typeView) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_for_images, viewGroup, false);
return new ImagesViewHolder(view);
}
#Override
public void onBindViewHolder(ImageRecyclerViewAdapter.ImagesViewHolder holder, int position) {
// holder.image.setBackground(images.get(position).getDrawable());
holder.image.setBackground(ctx.getResources().getDrawable(R.drawable.image_1));
}
#Override
public int getItemCount() {
return images.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}

A common mistake I make is forgetting to set a LinearLayoutManager to the RecyclerView.
private LinearLayoutManager mLayoutManager;
RecyclerView mRecyclerView;
mLinearLayoutManager = new LinearLayoutManager(context);
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

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

Recyclerview onItemClick not working (no log)

I wanted to implement an OnItemClick on the elements of my RecyclerView and i built an interface on my adapter and I am calling it on my activity, however the click isn't working and there is no errors in the log, so I'm not sure what I'm messing up. It is a Grid Recyclerview, if that affects anything.. The toast doesn't show up either.
my adapter
public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.ImageViewHolder> {
private Context mContext;
private List<Images> mUploads;
private OnItemClickListener mListener;
public ImagesAdapter(Context context, List<Images> uploads) {
mContext = context;
mUploads = uploads;
}
#Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_view_post, parent, false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Picasso.get().load(mUploads.get(position).getImage_url2()).fit().centerCrop().into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView imageView;
LinearLayout view_container;
public ImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.thumbnail2);
view_container = itemView.findViewById(R.id.container2);
}
#Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
}
My activity
public class ProfileActivity2 extends AppCompatActivity implements OnMapReadyCallback, ImagesAdapter.OnItemClickListener {
mRecyclerview = findViewById(R.id.recyclerview_insideprofile);
mRecyclerview.setLayoutManager(new GridLayoutManager(this, 2));
mRecyclerview.setHasFixedSize(true);
mAdapter = new ImagesAdapter(ProfileActivity2.this, mImages);
mRecyclerview.setAdapter(mAdapter);
mStorage = FirebaseStorage.getInstance();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("Building_Images");
mAdapter.setOnItemClickListener(ProfileActivity2.this);
#Override
public void onItemClick(int position) {
Toast.makeText(ProfileActivity2.this, "TestMessage", Toast.LENGTH_SHORT).show();
Images selectedItem = mImages.get(position);
final String image_url = selectedItem.getImage_url2();
Intent mainIntent = new Intent(ProfileActivity2.this, FullScreenImageActivity.class);
mainIntent.putExtra("b_image_url", image_url);
startActivity(mainIntent);
}
You forgot to add itemView.setOnClickListener in your viewholder, you have to add:
public ImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.layout);
itemView.setOnClickListener(this);

recyclerview in view pager won't load

I am developing an android app with multiple tabs using viewpager. However, one of the tabs which contain recyclerview just keep on loading non stop as shown in the attached screenshot. The recyclerview contains a list of images. I am able to load imageview and textview in other tabs successfully, only this tab which contain recyclerview has issue.
Adapter:
public class MasterListAdapter extends RecyclerView.Adapter<MasterListAdapter.Holder> {
private Context mContext;
private List<Integer> mImageIds;
private LayoutInflater inflater;
#BindView(R.id.images_list_view) ImageView imageView;
public MasterListAdapter(Context context, List<Integer> imageIds) {
mContext = context;
mImageIds = imageIds;
inflater = LayoutInflater.from(context);
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(inflater.inflate(R.layout.list_view, parent, false));
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext).load(mImageIds.get(position)).apply(RequestOptions.fitCenterTransform()).into(imageView);
}
#Override
public int getItemCount() {
return mImageIds.size();
}
public class Holder extends ViewHolder {
public Holder(View itemView) {
super(itemView);
}
}
}
Fragment:
public class ListFragment extends BaseFragment {
#BindView(R.id.recyclerView)
RecyclerView recyclerView;
#Override
int getLayoutId() {
return R.layout.fragment_list;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
ImageAssets:
public class ImageAssets {
private static final List<Integer> images = new ArrayList<Integer>() {{
add(R.drawable.user);
//add(R.drawable.img2);
//add(R.drawable.img3);
//add(R.drawable.img4);
//add(R.drawable.img5);
//add(R.drawable.img6);
//add(R.drawable.img7);
//add(R.drawable.img8);
//add(R.drawable.img9);
//add(R.drawable.img10);
//add(R.drawable.img11);
//add(R.drawable.img12);
//add(R.drawable.img13);
}};
public static List<Integer> getImages() {
return images;
}
}
Viewpager:
static class ViewPagerAdapter extends FragmentPagerAdapter {
ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
return Page.values()[position].getFragment();
}
#Override
public CharSequence getPageTitle(int position) {
return Page.values()[position].getTitle();
}
#Override
public int getCount() {
return Page.values().length;
}
}
enum Page {
First("View") {
#Override
Fragment getFragment() {return new ListFragment();}
};
private String title;
Page(String title) {
this.title = title;
}
String getTitle() {
return title;
}
abstract Fragment getFragment();
}
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:foreground="?attr/selectableItemBackground"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/images_list_view"
android:clipToPadding="false"
android:src="#drawable/user">
</ImageView>
</android.support.v7.widget.CardView>
Update:
Hi all, thank you for all your value responses to my post. I have figured out the cause of the infinite loading issue. It was because I imported ListFragment in MainActivity accidentally which has the exact same name as my fragment class. Removing the import solved the issue.
Instead of
#BindView(R.id.images_list_view) ImageView imageView;
try this :-
public class Holder extends ViewHolder {
public ImageView imageView;
public Holder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.images_list_view);
}
and in onBind
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext)
.load(mImageIds.get(position))
.apply(RequestOptions.fitCenterTransform())
.into(holder.imageView);
}
As you are calling a static reference your ImageAssets constructor is never getting called.. so get the correct list of images try this in your fragment :-
private void init() {
ImageAssets imageAssets = new ImageAssets(); // add this line to get the images
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
Make constructor in ImageAssets class and initialize images there !!
Found your problem. You forgot to bind your view inside Holder class.
public class MasterListAdapter extends RecyclerView.Adapter<MasterListAdapter.Holder> {
private Context mContext;
private List<Integer> mImageIds;
private LayoutInflater inflater;
public MasterListAdapter(Context context, List<Integer> imageIds) {
mContext = context;
mImageIds = imageIds;
inflater = LayoutInflater.from(context);
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(inflater.inflate(R.layout.list_view, parent, false));
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext).load(mImageIds.get(position)).apply(RequestOptions.fitCenterTransform()).into(holder.imageView);
}
#Override
public int getItemCount() {
return mImageIds.size();
}
public class Holder extends ViewHolder {
#BindView(R.id.images_list_view) ImageView imageView;
public Holder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
Fragment
public class ListFragment extends BaseFragment {
#BindView(R.id.recyclerView)
RecyclerView recyclerView;
#Override
int getLayoutId() {
return R.layout.fragment_list;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
init();
}
private void init() {
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}

RecyclerView-: No adapter attached; skipping layout

I have been reading the different answers here on stackoverflow post and tried to implement their solutions but I am still getting the error
public class Appetizer extends AppCompatActivity {
private RecyclerView recyclerView;
private AdapterFood mAdapter;
recyclerView = (RecyclerView)findViewById(R.id.lvaptzr);
mAdapter = new AdapterFood(Appetizer.this,data);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(Appetizer.this));
this is my Adapter class:
public class AdapterFood extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataFood> data = Collections.emptyList();
DataFood current;
int currentPos=0;
public AdapterFood(Context context, List<DataFood> data){
this.context=context;
inflater=LayoutInflater.from(context);
this.data=data;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.list_item,parent,false);
Myholder holder=new Myholder(view);
return holder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Myholder myholder=(Myholder) holder;
DataFood current=data.get(position);
myholder.textFoodname.setText(current.foodName);
myholder.textPrice.setText("Rp. " + current.price);
myholder.textId.setText(current.foodId);
Glide.with(context).load("http://kelompokdua.hol.es/pic/" + current.foodImage)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(myholder.ivFood);
}
#Override
public int getItemCount() {
return data.size();
}
class Myholder extends RecyclerView.ViewHolder{
TextView textFoodname,textPrice,textId;
ImageView ivFood;
public Myholder(View itemView) {
super(itemView);
textFoodname=(TextView) itemView.findViewById(R.id.textFoodname);
ivFood=(ImageView)itemView.findViewById(R.id.ivFood);
textId=(TextView)itemView.findViewById(R.id.textid);
textPrice=(TextView) itemView.findViewById(R.id.textPrice);
}
}
}
i try any method, but i can't solve the probems. what i am doing wrong?
You should call setLayoutManager before setAdapter .
recyclerView = (RecyclerView)findViewById(R.id.lvaptzr);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(Appetizer.this);
recyclerView.setLayoutManager(mLayoutManager); // 1st
mAdapter= new AdapterFood(Appetizer.this,data);
recyclerView.setAdapter(mAdapter); // 2nd

RecyclerView, slow scrollToPosition, setSelected

Prompt, faced with scrollToPosition speed. I RecyclerView 900 positions and need to quickly establish a position as scrollToPosition very slow, c ListView such problems were not setSelection working out perfectly. It is possible to accelerate the scrollToPosition?
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.Holder> {
final static public String TAG = "EventAdapter";
Context context;
List<Event> items;
public EventAdapter(List<Event> items) {
this.items = items;
}
#Override
public EventAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_event, parent, false);
context = parent.getContext();
return new EventAdapter.Holder(v);
}
#Override
public void onBindViewHolder(EventAdapter.Holder holder, int position) {
holder.setModel(context, items.get(position), position);
}
#Override
public int getItemCount() {
return items.size();
}
public class Holder extends RecyclerView.ViewHolder {
public View block;
Holder(View view) {
super(view);
block = (View) view.findViewById(R.id.cv);
}
public void setModel(Context context, Event model, int position) {
}
}
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
rv.setLayoutManager(linearLayoutManager);
rv.setHasFixedSize(true);
linearLayoutManager.scrollToPosition(index);
There are couple of ways to do it:
1) Try smoothScrollToPosition as : [will certainly work]
Toast.makeText(this, "Scroll...", Toast.LENGTH_SHORT).show();
myList.smoothScrollToPosition(0);
2) Also read about scrollToPositionWithOffset as this may help too

Categories

Resources