I have set up a GridLayoutManager with a span of 2 and ideally,it should show my elements in 2 columns but the output shows only one element.
Below is the code:
Adapter
public class MemoriesAdapter extends RecyclerView.Adapter<MemoriesAdapter.MemoriesViewHolder> {
private static final String TAG = "MemoriesAdapter";
private final LayoutInflater mInflater;
private final List<List<String>> titleList;
private final List<List<StorageReference>> imageList;
public MemoriesAdapter(Context context, List<List<String>> titleList, List<List<StorageReference>> imageList) {
this.mInflater = LayoutInflater.from(context);
this.titleList = titleList;
this.imageList = imageList;
Log.d(TAG, "Constructor ");
}
#NonNull
#Override
public MemoriesAdapter.MemoriesViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.memories_cards, parent, false);
MemoriesAdapter.MemoriesViewHolder viewHolder = new MemoriesAdapter.MemoriesViewHolder(view);
Log.d(TAG, "onCreateViewHolder");
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull MemoriesAdapter.MemoriesViewHolder holder, int position) {
if (!(titleList.isEmpty() || imageList.isEmpty())) {
Log.d(TAG, "Lists are not empty, proceeding.");
String title = String.valueOf(titleList.get(position));
holder.title.setText(title);
// TODO: get storage ref into the imageview
}
}
#Override
public int getItemCount() {
return titleList.size();
}
public class MemoriesViewHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView title;
private ImageView image2;
private TextView title2;
public MemoriesViewHolder(#NonNull View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder");
image = itemView.findViewById(R.id.memory_image_one);
title = itemView.findViewById(R.id.memory_title);
}
}
}
Fragment
memoriesViewModel.getTitleList().observe(getViewLifecycleOwner(), titList -> {
titleList.add(titList);
memoriesAdapter.notifyDataSetChanged();
Log.d(TAG, "Memories Frag " + titleList);
});
GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
memoryRecyclerView.setHasFixedSize(true);
memoryRecyclerView.setLayoutManager(manager);
memoryRecyclerView.setAdapter(memoriesAdapter);
item_layout
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/memory_dashboard_card"
android:layout_width="180dp"
android:layout_height="170dp"
android:background="#drawable/memories_card_yellow"
>
<ImageView
android:id="#+id/memory_image_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="60dp"
android:src="#drawable/unicorn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="#+id/memory_title"
android:layout_width="108dp"
android:layout_height="21dp"
android:text="My first date was epic"
android:textSize="16sp"
android:textColor="#000"
android:textStyle="bold"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/memory_image_one" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/memories_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:spanCount="2"
tools:listitem="#layout/memories_cards" />
Comment if you want more code or edit if I have given any unnecessary details
Thank You!
EDIT: I have added the entire adapter and I am removing the layout files.
I know this is kinda late but i had the same exact problem and to fix it make sure that the value of layout_width and layout_height is match_parent, i'm talking about the xml layout that you used in your recyclerview adapter, hope you got it fixed.
Related
I have a recyclerview that does not update the text inside the items of the recyclerview.
When initializing the Recycler view, everything is fine. But when you call the filter method. It gives you the correct item as per the catlog. The correct position for the item. The Correct number of items in the ArrayList. But even the image is getting update by picasso. But it does not update the textview that I have on OnBind from the RecyclerView.
public class HospitalAdapter extends RecyclerView.Adapter<HospitalAdapter.HospitalViewHolder> {
private ArrayList<Hospital> hospitalList;
private ArrayList<Hospital> originalList;
private LayoutInflater inflater;
private DatabaseReference db = FirebaseDatabase.getInstance().getReference();
private StorageReference storageRef = FirebaseStorage.getInstance().getReference();
//View FIELDS
private TextView HospitalName;
private TextView Phone;
private RatingBar Rating;
private ImageView Image;
private TextView Location;
private TextView Address;
private LinearLayout cardView;
private Context context;
private Activity activity;
public HospitalAdapter(Context context, Activity activity,String category,String search, ArrayList<Hospital> hospitalArrayList) {
this.context = context;
this.activity = activity;
inflater = LayoutInflater.from(context);
hospitalList = hospitalArrayList;
}
#NonNull
#Override
public HospitalViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.hospital_list_item,parent,false);
HospitalViewHolder holder = new HospitalViewHolder(view);
Log.e("Holder","On CreateViewHolder getting called");
return holder;
}
#Override
public void onBindViewHolder(#NonNull HospitalViewHolder holder, int position) {
Hospital hospital = hospitalList.get(position);
HospitalViewHolder tempHolder = holder;
HospitalName.setText(hospital.getName());
Phone.setText(hospital.getContactNumber());
Rating.setRating(hospital.getRating());
Location.setText(hospital.getLocation());
Address.setText(hospital.getAddress());
final DatabaseReference dbrate = FirebaseDatabase.getInstance().getReference();
}
#Override
public int getItemCount() {
return hospitalList.size();
}
public class HospitalViewHolder extends RecyclerView.ViewHolder {
public HospitalViewHolder(#NonNull View itemView) {
super(itemView);
HospitalName = itemView.findViewById(R.id.hospital_list_item_Name);
Phone = itemView.findViewById(R.id.hospital_list_item_phone);
Rating = itemView.findViewById(R.id.hospital_list_item_rating);
Image = itemView.findViewById(R.id.hospital_list_item_imageview);
Address = itemView.findViewById(R.id.hospital_list_item_address);
Location = itemView.findViewById(R.id.hospital_list_item_city);
cardView = itemView.findViewById(R.id.hospital_list_item_cardview);
}
}
private void sharedElementTransition(View view,Uri uri,Hospital hospital){
Pair[] pair = new Pair[5];
pair[0] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_imageview),"hospital_image");
pair[1] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_rating),"hospital_rating");
pair[2] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_cardview),"hospital_cardview");
pair[3] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_Name),"hospital_name");
pair[4] = new Pair<View,String>(view.findViewById(R.id.hospital_list_item_phone),"hospita_contact");
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(activity,pair);
Intent i = new Intent(context, HospitalTabbed.class);
i.putExtra("THE_URI",uri.toString());
i.putExtra("Key",hospital.getKey());
i.putExtra("Details",hospital.getDetails());
i.putExtra("Rating",hospital.getRating());
i.putExtra("CityName",hospital.getLocation());
i.putExtra("Contact",hospital.getContactNumber());
i.putExtra("Address",hospital.getAddress());
i.putExtra("Name",hospital.getName());
context.startActivity(i,options.toBundle());
}
public void filterList(ArrayList<Hospital> filteredList){
hospitalList = filteredList;
notifyDataSetChanged();
}
}
this is my RecyclerView. Now I am wondering if there could be a problem on the my Layout file. Since the recycler sometimes acts differently on different layouts.
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_marginBottom="8dp">
<Spinner
android:id="#+id/lobby_spinner_category"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/lobby_search_edit_text"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.6"
/>
<Button
android:id="#+id/lobby_search_btn"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.27"
android:text="search"/>
<Button
android:id="#+id/lobby_speech"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.13"
android:drawableLeft="#drawable/ic_mic_black_24dp"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="#+id/lobby_recycler_view"
android:layout_width="match_parent"
android:layout_height="500dp"
/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/lobby_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:fabSize="normal"
android:src="#drawable/ic_assignment_white_24dp"
app:layout_anchor="#id/lobby_bottom_appbar"/>
<android.support.design.bottomappbar.BottomAppBar
android:id="#+id/lobby_bottom_appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/mtrl_bottomappbar_height"
android:layout_gravity="bottom"
android:backgroundTint="#color/colorPrimary"
android:transitionName="lobby_constraint"
app:fabAlignmentMode="end"
app:fabAttached="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lobby_recycler_view"
app:menu="#menu/lobby_bar_menu">
</android.support.design.bottomappbar.BottomAppBar>
Summary of the problem. The list items textview is not getting updated. But the onclick when going to another activity is fine. The image is being updated by picasso is fine and catlog information about the item is also correct. It is just when filtering the Textview does not update.
There are several probabilities :
1. You need to setLayoutManager to the RecyclerView
2. You need to set your adapter to your Recyclerview
3. You need to make sure your getItemCount is not 0
all
private TextView HospitalName;
private TextView Phone;
private TextView Location;
private TextView Address;
not updated?
I forgot that I need to make the changes in the older's fields not on the adapters field.
OnBindViewHolder
holder.HospitalName.setText(hospital.getName());
holder.Phone.setText(hospital.getContactNumber());
holder.Rating.setRating(hospital.getRating());
holder.Location.setText(hospital.getLocation());
holder.Address.setText(hospital.getAddress());
That is when the sender from one device sends message,it should appear on the right hand side and when the other device gets the message it should appear on the left hand side.
My java class: This is the function I am calling
private void chatRowStyling(boolean isItMe, ViewHolder holder){
if (isItMe){
holder.layoutParams.gravity = Gravity.END;
holder.chatBody.setTextColor(Color.BLUE);
holder.senderName.setBackgroundResource(R.drawable.speech_bubble_green);
}else{
holder.layoutParams.gravity = Gravity.START;
holder.chatBody.setTextColor(Color.GREEN);
holder.senderName.setBackgroundResource(R.drawable.speech_bubble_orange);
}
Log.i("TAG","error :" + mySnapShot);
holder.senderName.setLayoutParams(holder.layoutParams);
holder.chatBody.setLayoutParams(holder.layoutParams);
}
In the Screenshot, all the messages appear on the right side, whereas I want one to be on right and another to be on left from different users.
This is my XML code :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/singleMessageContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:text="#string/sender"
android:textColor="#2980b9"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_margin="5dip"
android:padding="12dp"
android:text="#string/author"
android:textColor="#2c3e50" />
</LinearLayout>
The best way to do it would be to implement RecyclerView with separate view types, that way you could adjust layout however you want without some shady workaround. Here is example to do it.
Reference : How to create RecyclerView with multiple view type?
EDIT:
Convinced by comments I decided to add some more explanation.
You could create two separate layouts, one for your messages:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvAuthor"
android:layout_alignParentRight="true"/>
</RelativeLayout>
and the other one for messages from friend:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvAuthor" />
</RelativeLayout>
Then you can create a class for your messages:
public class Message {
private String message;
private String author;
public Message(String message, String author) {
this.message = message;
this.author = author;
}
public String getMessage() {
return message;
}
public String getAuthor() {
return author;
}
}
Next step would be to create adapter for your RecyclerView that will use two seperate ViewHolders, one for your messages and the other for messages from your friend. Inside OnCreateViewHolder you can choose which layout you want to display for each message. Then in OnBindViewHolder, you can fill TextViews with correct message data.
public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<Message> messages;
private final static int TYPE_FROM_FRIEND = 1;
private final static int TYPE_TO_FRIEND = 2;
public MessageAdapter(ArrayList<Message> messages) {
this.messages = messages;
}
public int getItemViewType(int position) {
if (messages.get(position).getAuthor().equals("Me")) {
return TYPE_TO_FRIEND;
} else {
return TYPE_FROM_FRIEND;
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = 0;
RecyclerView.ViewHolder viewHolder;
switch (viewType){
case TYPE_TO_FRIEND:
layout = R.layout.view_message_to_friend;
View toFriendView = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder=new ToFriendViewHolder(toFriendView);
break;
case TYPE_FROM_FRIEND:
layout = R.layout.view_message_from_friend;
View fromFriendView = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder = new FromFriendViewHolder(fromFriendView);
break;
default:
viewHolder = null;
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
int viewType=holder.getItemViewType();
switch (viewType){
case TYPE_TO_FRIEND:
((ToFriendViewHolder)holder).tvMessage.setText(messages.get(position).getMessage());
((ToFriendViewHolder)holder).tvAuthor.setText(messages.get(position).getAuthor());
break;
case TYPE_FROM_FRIEND:
((FromFriendViewHolder)holder).tvMessage.setText(messages.get(position).getMessage());
((FromFriendViewHolder)holder).tvAuthor.setText(messages.get(position).getAuthor());
break;
}
}
private class ToFriendViewHolder extends RecyclerView.ViewHolder {
private TextView tvAuthor;
private TextView tvMessage;
public ToFriendViewHolder(View view) {
super(view);
tvAuthor = (TextView) view.findViewById(R.id.tvAuthor);
tvMessage = (TextView) view.findViewById(R.id.tvMessage);
}
}
private class FromFriendViewHolder extends RecyclerView.ViewHolder {
private TextView tvAuthor;
private TextView tvMessage;
public FromFriendViewHolder(View view) {
super(view);
tvAuthor = (TextView) view.findViewById(R.id.tvAuthor);
tvMessage = (TextView) view.findViewById(R.id.tvMessage);
}
}
#Override
public int getItemCount() {
return messages.size();
}
}
Finally you can set RecyclerView's adapter in your activity:
public class MainActivity extends AppCompatActivity {
private RecyclerView rvMain;
private ArrayList<Message> messages = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvMain = (RecyclerView) findViewById(R.id.rvMain);
rvMain.setLayoutManager(new LinearLayoutManager(this));
messages.add(new Message("Hello", "Me"));
messages.add(new Message("Hi", "Friend"));
messages.add(new Message("How are you?", "Me"));
messages.add(new Message("I'm fine and you?", "Friend"));
MessageAdapter adapter = new MessageAdapter(messages);
rvMain.setAdapter(adapter);
}
}
You can achieve it by setting the view gravity not the LayoutParams with something like this:
if(isIfMe) {
holder.linearLayout.setGravity(Gravity.END);
holder.tvAuthor.setGravity(Gravity.END);
} else {
holder.linearLayout.setGravity(Gravity.END);
holder.tvAuthor.setGravity(Gravity.END);
}
I'm trying to adapt a recyclerview to display a list of objects. But actually I have some kind of wierd bug. My object have ID and Name data to display, but in the list, at first, I only can see the Id, and when I scroll down, only the rows going completely out of the screen are completely displayed...
This is my ListPresentActivity.
public class ListPresentActivity extends AppCompatActivity implements ViewInterface {
private static final String EXTRA_PRESENT_ID = "EXTRA_PRESENT_ID";
private static final String EXTRA_PRESENT_NAME = "EXTRA_PRESENT_NAME";
private List<PresentItem> listOfPresent;
private LayoutInflater layoutInflater;
private RecyclerView recyclerView;
private CustomAdapter adapter;
private PresentController presentController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_present);
recyclerView = (RecyclerView) findViewById(R.id.rec_list_activity);
recyclerView.setHasFixedSize(true);
layoutInflater = getLayoutInflater();
presentController = new PresentController(this, new FakePresentModel());
}
#Override
public void startDetailActivity(int id, String name, String info, String target, String advice, String price) {
//public void startDetailActivity(int id) {
Intent i = new Intent(this,PresentDetailActivity.class);
i.putExtra(EXTRA_PRESENT_ID, id);
i.putExtra(EXTRA_PRESENT_NAME, name);
startActivity(i);
}
#Override
public void setUpAdapterAndView(List<PresentItem> listOfPresent) {
this.listOfPresent = listOfPresent;
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new CustomAdapter();
recyclerView.setAdapter(adapter);
}
private class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder>{
#Override
public CustomAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = layoutInflater.inflate(R.layout.item_present, parent, false);
return new CustomViewHolder(v);
}
#Override
public void onBindViewHolder(CustomAdapter.CustomViewHolder holder, int position) {
PresentItem currentPresent = listOfPresent.get(position);
holder.id.setText(
Integer.toString(currentPresent.getId())
);
holder.name.setText(
currentPresent.getName()
);
}
#Override
public int getItemCount() {
return listOfPresent.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView id;
private TextView name;
private ViewGroup container;
public CustomViewHolder(View itemView){
super(itemView);
this.id = (TextView) itemView.findViewById(R.id.texv_item_id);
this.name = (TextView) itemView.findViewById(R.id.texv_item_name);
this.container = (ViewGroup) itemView.findViewById(R.id.root_list_present);
this.container.setOnClickListener(this);
}
#Override
public void onClick(View v) {
PresentItem presentItem = listOfPresent.get(
this.getAdapterPosition()
);
presentController.onListItemClick(presentItem);
}
}
}
}
The layout item_present.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="88dp"
android:id="#+id/root_list_present"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="#+id/texv_item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="#id" />
<TextView
android:id="#+id/texv_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="84dp"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="collier" />
</android.support.constraint.ConstraintLayout>
And my list layout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.lubbee.bruh.view.ListPresentActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rec_list_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
I have search for a long time now, and the only concording result I have fount seems to be fixed with the recyclerView.setHasFixedSize(true); which is not my case...
Thanks for your time!
PS : If there is some code parts needed missing, just say the word! :]
Just after the first loading.
After one scroll to the bottom and back to the top of the screen
<LinearLayout android:layout_width="match_parent"
android:layout_height="88dp"
android:weightSum="1"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Try this simple LinearLayout it should work
You can also add other properties, but for now try it out
I want to insert/show horizontal recyclerview into specific position (at this example position:1)
Vertical recyclerview - displays TextView + shape (rectange)
Horizontal recyclerview - displays ImageViews (next to each other)
.XML's
horizontal_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontal_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mkalejs.training.MainActivity">
vertical_layout.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/vertical_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
</android.support.constraint.ConstraintLayout>
vertical_layout_item.xml
<EditText
android:id="#+id/lbl_url_text"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#android:color/transparent"
android:inputType="textPersonName"
android:padding="5dp"
android:layout_gravity="center"
android:text="Name" />
<View
android:id="#+id/rectangle"
android:layout_width="195dp"
android:layout_height="100dp"
android:background="#drawable/rectangle" />
horizontal_layout_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:visibility="visible"
android:weightSum="1">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img_horizontal_slider"
android:layout_width="33dp"
android:layout_height="33dp"
android:adjustViewBounds="true"
android:visibility="visible" />
</LinearLayout>
There might be a small misunderstanding in recyclerview concept. I'm not sure where I can call horizontal_recyclerview" to not return it as a null (as I am now) and populate it with data. Is the XML structure wrong? Feel free to update code parts.
THIS SHOULD BE OKAY
MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vertical_layout);
ArrayList<PictureData> bundleOfData = prepareData();
RecyclerView rv = (RecyclerView) findViewById(R.id.vertical_recyclerview);
VerticalAdapter adapter = new VerticalAdapter(bundleOfData, this);
rv.setAdapter(adapter);
//rv.setNestedScrollingEnabled(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(layoutManager);
}
VerticalAdapter.class
public class VerticalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private ArrayList<PictureData> pictures;
Context context;
public VerticalAdapter(ArrayList<PictureData> pictures, Context context)
{
this.pictures = pictures;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
switch (viewType) {
case 1:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(itemView);
default:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_layout_item, parent, false);
return new VerticalViewHolder(itemView);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()){
case 1:
HorizontalViewHolder hVh = (HorizontalViewHolder) holder;
HorizontalAdapter horizontal_adapter = new HorizontalAdapter(pictures, context);
hVh.horizontal_rec.setAdapter(horizontal_adapter);
break;
default:
VerticalViewHolder vVh = (VerticalViewHolder) holder;
vVh.textView.setText(pictures.get(position).getPictureUrl());
break;
}
}
#Override
public int getItemCount() {return pictures.size();}
#Override
public int getItemViewType(int position) {
if(position == 1){
return 1;
}else{
return 0;
}
}
}
HorizontalViewHolder.class
I GET A ERROR HERE. OBVIOUSLY, I DON'T HAVE (img_horizontal_slider) IN HORIZONTAL_LAYOUT, but putting ImageView + RecyclerView in one layout file and call here would feel wrong (?)
public class HorizontalViewHolder extends RecyclerView.ViewHolder{
public ImageView imagePositionInLayout;
public RecyclerView horizontal_rec;
public HorizontalViewHolder(View itemView) {
super(itemView);
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontal_rec = itemView.findViewById(R.id.vertical_recyclerview);
imagePositionInLayout = itemView.findViewById(R.id.img_horizontal_slider);
horizontal_rec.setLayoutManager(layoutManager);
}
public ImageView getView() {
return imagePositionInLayout;
}
}
HorizontalAdapter.class
ublic class HorizontalAdapter extends RecyclerView.Adapter<HorizontalViewHolder> {
private ArrayList<PictureData> pictures;
Context context;
public HorizontalAdapter(ArrayList<PictureData> pictures, Context context) {
this.pictures = pictures;
this.context = context;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position)
{
Picasso.Builder builder = new Picasso.Builder(context);
Picasso picasso = builder.build();
picasso
.load("http://nuclearpixel.com/content/icons/2010-02-09_stellar_icons_from_space_from_2005/earth_128.png")
.placeholder(R.mipmap.ic_launcher_round)
.into(holder.imagePositionInLayout);
}
#Override
public int getItemCount() {
return pictures.size();
}
}
I'm new to Android so I hope people will give some good explanation on what is wrong and how to fix it. Every suggestion will be gladly accepted. I will highly appreciate if someone will actually look into this and explain, because I feel pretty lost right now.
I want to create a list of items that have a background image and a TextView. Although it creates the RecyclerView normally and sets the text, the background image doesn't set.
This is my Adapter Class
public class Casino_Adapter extends RecyclerView.Adapter<Casino_Adapter.ViewHolder> {
private Data[] mDataset;
private ClickListener clickListener;
public Context context;
// Provide a suitable constructor (depends on the kind of dataset)
public Casino_Adapter(Data[] myDataset) {
this.mDataset = myDataset;
this.context = context;
}
#Override
public Casino_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.casino_card_row, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final Context context = null;
// each data item is just a string in this case
public TextView mTextView;
public ImageView mImageView;
public CardView cardView;
//Typeface tf;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text);
mImageView = (ImageView) v.findViewById(R.id.image);
cardView = (CardView) v.findViewById(R.id.card_view);
//cardView.setPreventCornerOverlap(false);
}
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position].getText());
holder.mImageView.setImageResource(mDataset[position].getImage());
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
public interface ClickListener {
public void itemClicked(View view, int pos);
}
}
I have a Fragment Class where I want the RecyclerView to be.
public class CasinoFragment extends Fragment {
protected RecyclerView recyclerView;
protected RecyclerView.Adapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;
public CasinoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initial();
}
private void initial() {
final Data datas[] =
{
new Data("This is an item", R.drawable.ic_home_white_36dp),
new Data("This is an item 2", R.drawable.car),
new Data("asdasd`sdads", R.drawable.car),
new Data("This is an item 3", R.drawable.car)
};
mAdapter = new Casino_Adapter(datas);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_casino, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
return view;
}
}
And these are the Setters that I use.
public class Data {
int image;
String text;
public Data(String title, int backimage)
{
this.text = title;
this.image = backimage;
}
public String getText()
{
return text;
}
public int getImage()
{
return image;
}
public void setText()
{
this.text=text;
}
public void setImageID()
{
this.image = image;
}
}
Casino Row XML
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="8dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#111111"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/image_round"
android:src="#drawable/car" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/image"
android:id="#+id/rel_color"
android:background="#4c4c4c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Μπύρα"
android:textColor="#fcfcfc"
android:textSize="30sp"
android:shadowColor="#000000"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="5"
android:id="#+id/text"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
The result I get is the following.
The reason why you aren't seeing your image, is because it isn't actually visible.
Your rel_color layout has the same size attributes as your ImageView.
If you want to display a layout above the ImageView, you just need to remove the background of it, otherwise the ImageView will be hidden.
Just Like that. but remember your images dimensions must be low. in my case i used 200x200
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/rl_menu_item"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="5dp"
android:background="#android:color/transparent"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/img_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_blrr">
<ImageView
android:id="#+id/iv_icon"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="#drawable/gray_circle"
android:padding="15dp"
android:scaleType="centerCrop"
android:src="#drawable/school" />
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#id/iv_icon"
android:layout_alignLeft="#id/iv_icon"
android:layout_alignRight="#id/iv_icon"
android:layout_alignStart="#id/iv_icon"
android:layout_below="#id/iv_icon"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Your Favourite Place"
android:textColor="#android:color/black" />
</RelativeLayout>
</android.support.v7.widget.CardView>