Wrap content of items individually in a Recycler View? - java

I'm trying to figure out how to do this in the most simple way possible. Setting the recycler view to wrap the content of course, adjusts the recycler to the biggest item. Setting it to match_parent adjusts all the items to be the maximum at all times.
Setting the maximum width in the Adapter makes all the items be that size no matter what. Dumbfounded at the moment.
My Adapter class
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private ArrayList<Message> messageList;
private Context context;
private MessageSelectListener messageSelectListener;
public MessageAdapter(Context context, ArrayList<Message> messageList){
this.messageList = messageList;
this.context = context;
}
//Create an interface
public interface MessageSelectListener{
void onMessageClick(Message message, int position);
}
public void setMessageClickListener(MessageSelectListener messageSelectListener){
this.messageSelectListener = messageSelectListener;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
//View view = new TextView(context);
//View rootView = LayoutInflater.from(context).inflate(R.layout.order_list, parent, false);
return new MessageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MessageViewHolder holder, final int position) {
final Message message = messageList.get(position);
holder.text.setText(message.getMessageText());
holder.text.setBackgroundResource(R.drawable.rounded_corner_white_noborder);
// passing Order and Position as parameter to interface method,
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
messageSelectListener.onMessageClick(message, position);
}
});
}
#Override
public int getItemCount() {
return messageList == null ? 0 : messageList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView text;
public Message message;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
text = (TextView) itemView;
}
}
}

The Solution was to change the layout parameters in the onCreateViewHolder() method to WRAP_CONTENT
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.**WRAP_CONTENT**, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
//View view = new TextView(context);
//View rootView = LayoutInflater.from(context).inflate(R.layout.order_list, parent, false);
return new MessageViewHolder(view);
}

Related

Why onCreateView on my RecyclerView Adapter is not called?

So I have a RecyclerView inside another RecyclerView. And in my onBind() method in recyclerView1 i am trying to set the data for recyclerView2 , the fact is that recyclerView1 has a FirebaseRecyclerAdapter and recyclerView2 has a custom RecyclerAdapter and the onCreate() method in recyclerView2's adapter is never called , so the data set is good but it doesn't create my recyclerView2 in the interface this recycler is empty.
I put a breakpoint on the OrderViewHolder onCreateViewHolder and it just skips this .
Here is the code from the recyclerView2's adapter onCreate and onBind methods :
adapterReceivedCommands = new FirebaseRecyclerAdapter<Comanda, ReceivedCommandsViewHolder>(optionsReceivedCommands) {
#NonNull
#Override
public ReceivedCommandsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_command,parent,false);
layoutManagerOrder = new GridLayoutManager(parent.getContext(),3);
orderRecycler = (RecyclerView) itemView.findViewById(R.id.order_recycler);
orderRecycler.setLayoutManager(layoutManagerOrder);
fragmentItem = (LinearLayout)itemView.findViewById(R.id.item_layout);
return new ReceivedCommandsViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull final ReceivedCommandsViewHolder receivedCommandsViewHolder, int i, #NonNull final Comanda comanda) {
receivedCommandsViewHolder.tableNmb.setText(comanda.getmasa());
orderModelArrayList = comanda.getcomanda();
mAdaptor = new RecyclerViewAdapter(this, orderModelArrayList);
receivedCommandsViewHolder.recyclerView.setAdapter(mAdaptor);
}
};
adapterReceivedCommands.startListening();
receivedCommandsRecycler.setAdapter(adapterReceivedCommands);
return view;
}
And here is the code from the custom recyclerView2's adapter :
public class RecyclerViewAdapter extends RecyclerView.Adapter<OrderViewHolder>{
private ArrayList<OrderModel> orderModels = new ArrayList<OrderModel>();
private FirebaseRecyclerAdapter<Comanda, ReceivedCommandsViewHolder> context;
public RecyclerViewAdapter(FirebaseRecyclerAdapter<Comanda, ReceivedCommandsViewHolder> context, ArrayList<OrderModel> orderModels) {
this.orderModels = orderModels;
this.context = context;
}
#NonNull
#Override
public OrderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_order,parent,false);
OrderViewHolder holder = new OrderViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull OrderViewHolder holder, int position) {
OrderModel curentModel = orderModels.get(position);
holder.cantitateTxt.setText(curentModel.getCantitate().toString());
holder.comandaTxt.setText(curentModel.getDenumireProdus());
}
#Override
public int getItemCount() {
return 0;
}
Your item count is zero:
#Override
public int getItemCount() {
return 0;
}
So RecyclerView doesn't see any items to inflate. You should fix it to return the size of your list:
#Override
public int getItemCount() {
return orderModels.size();
}

RecyclerView items not visible

RecyclerView items not visible
I am using a string array name item test and pass it to String array list, I used the same idea before and was working correctly
listItemsTest = new ArrayList<>(Arrays.asList(itemsTest));
recyclerView = findViewById(R.id.RecycleViewTest);
recycleViewAdapterTest = new RecycleViewAdapterTest(this,listItemsTest);
recyclerView.setAdapter(recycleViewAdapterTest);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
My Adapter
public class RecycleViewAdapterTest extends
RecyclerView.Adapter<RecycleViewAdapterTest.MyViewHolder> {
private ArrayList<String> ListTest;
Context context;
public RecycleViewAdapterTest(Context context, ArrayList<String> listTest) {
this.ListTest = listTest;
this.context = context;
}
#NonNull
#Override
public RecycleViewAdapterTest.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return ListTest.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
TextView textView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.LLTest);
textView = itemView.findViewById(R.id.textViewTest);
}
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
String value = this.ListTest.get(position);
holder.textView.setText(value);// Try to make the textView public
}
because you need to set list value to viewholder's textview in OnBindViewHolder. :(

How to add a recyclerview item dynamically when click a button in another activity?

I have a recyclerview in ShoppingCartActivity and a button in ProductDetailActivity. I want to send some data to ShoppingCartActivity when a button clicked in ProductDetailActivity and display them in recyclerview item that creates dynamically in ShoppingCartActivity.
I added addData method to recyclerview Adapter but it doesn't work and app crashes.
shop_btn = findViewById(R.id.add_to_shopping_cart);
shop_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent addToCart = new Intent(ProductDetailActivity.this, ShoppingCartActivity.class);
startActivity(addToCart);
}
});
//HERE IS MY RECYCLERVIEW ADAPTER
public class RecyclerViewAdapter_ShoppingCart extends RecyclerView.Adapter<RecyclerViewAdapter_ShoppingCart.MyViewHolder> {
private Context context;
List<CartItems> cartItemsList;
public RecyclerViewAdapter_ShoppingCart(Context context, List<CartItems> cartItemsList) {
this.context = context;
this.cartItemsList = cartItemsList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.card_cart, parent, false);
return new MyViewHolder(view);
}
#NonNull
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.cart_title.setText(cartItemsList.get(position).getTitle());
holder.cart_price.setText(cartItemsList.get(position).getPrice());
Picasso.get().load(cartItemsList.get(position).getImageUrl()).into(holder.cart_img);
}
#Override
public int getItemCount() {
return cartItemsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView cart_title, cart_price;
ImageView cart_img;
public MyViewHolder(View itemView) {
super(itemView);
cart_title = itemView.findViewById(R.id.cart_title);
cart_price = itemView.findViewById(R.id.cart_price);
//cart_delete = itemView.findViewById(R.id.cart_delete);
cart_img = itemView.findViewById(R.id.cart_image);
}
}
public void addItem(List<CartItems> cartItems) {
CartItems newValue = new CartItems();
newValue.setTitle("123");
newValue.setPrice("123");
newValue.setImageUrl("");
cartItems.add(newValue);
notifyDataSetChanged();
}
}
My goal is adding recyclerview items dynamically to ShoppingCartActivity when a button clicked in ProductDetailActivity but app crashes.

Horizontal RecyclerView of images

Which is the code to obtain a Horizontal RecyclerView of images? I already have the code for horizontal RecyclerView of Strings. I need to have the same but of Images.
I have to create a RecyclerView of 10 horizontal scrollable imageView.
This is my actual code of 10 horizontal TextViews:
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
private String[] items;
public HorizontalAdapter(String[] items) {
this.items = items;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.text.setText(items[position]);
}
#Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
TextView text;
public HorizontalViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text_view_top_10_ricette_titolo_ricetta);
} }
}
and I manage it on a Fragment in this way:
RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
list.setAdapter(new HorizontalAdapter(new String[]{"Android","Programming","Java","RecyclerView","Layout"}));
How I have to modify this?
Try this make below changes in your HorizontalAdapter
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
private int[] items;
public HorizontalAdapter(String[] int) {
this.items = items;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.image.setImageResource(items[position]);
}
#Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public HorizontalViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.imageviewid);
} }
}
code for RecyclerView
RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
list.setAdapter(new HorizontalAdapter(new int[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher}));
NOTE : Your layout (item_layout_recycler_view_top_10) needs to contain a ImageView instead of a TextView
Instead of a recyclerView full of textViews (R.layout.item_layout_recycler_view_top_10) you want images?
Just create another layout which contains an ImageView instead of a TextView in it and use it accordingly to your current solution.
String array change to image url array or drawable image array create and send to adapter and set into imageview.

RecyclerView in Fragment

I am setting up a RecyclerView inside a Fragment following the Android docs example here.
I pasted my code below, there is not much. (The only difference is I'm using a Fragment instead of an Activity).
The error I'm getting is:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
All solutions to this problem online are to add false argument in layout inflation for fragment. But I've already done this, and I still get the error.
My code:
MainActivity
...
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new NewsFragment()).commit();
...
NewsFragment
public class NewsFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.news_layout,container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new NewsAdapter(new String[]{"hello","world","qwert","test","greg","peck"});
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
NewsAdapter
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(TextView tv) {
super(tv);
mTextView = tv;
}
}
public NewsAdapter(String[] myDataset) {
mDataset = myDataset;
}
#Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);
TextView tv = (TextView)v.findViewById(R.id.textView2);
ViewHolder vh = new ViewHolder(tv);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
Any help would be hugely appreciated!
The error is on this line:
TextView tv = (TextView)v.findViewById(R.id.textView2);
ViewHolder vh = new ViewHolder(tv);
Change it to:
TextView tv = (TextView)v.findViewById(R.id.textView2);
ViewHolder vh = new ViewHolder(v);
The text view is already having a parent. That's why you are getting this error.
Updated code:
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View view) {
super(tv);
mTextView= (TextView)view.findViewById(R.id.textView2);
}
}
public NewsAdapter(String[] myDataset) {
mDataset = myDataset;
}
#Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}

Categories