Not displaying horizontal and vertical list in fragment - java

Creating a tabbed fragment activity, in my home_fragment i am trying to display a horizontal and a vertical list view, but its not displaying the list, below is my code of home_fragment for onCreateView() method --
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_home, container, false);
horizontalList = (RecyclerView)v.findViewById(R.id.horizontal_recycler);
verticalList = (RecyclerView)v.findViewById(R.id.recyle_view);
horizontalList.setHasFixedSize(true);
verticalList.setHasFixedSize(true);
//set horizontal LinearLayout as layout manager to creating horizontal list view
LinearLayoutManager horizontalManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontalList.setLayoutManager(horizontalManager);
horizontalAdapter = new HorizontalListAdapter(getActivity());
horizontalList.setAdapter(horizontalAdapter);
//set vertical LinearLayout as layout manager for vertial listview
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
verticalList.setLayoutManager(layoutManager);
verticalAdapter = new VerticalListAdapter(getActivity());
verticalList.setAdapter(verticalAdapter);
return v;
}
Is there any changes to be made here.
HorizontalListAdapter.java --
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder>{
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
}
}
}
item_horizontal_list.xml--
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>

Not displaying horizontal and vertical list in fragment
in your onBindViewHolder you are no displaying any data check out your adapter class
You need to set any data inside your onBindViewHolder like this
Sample code
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder> {
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
viewHolder.edtName.setText("NILU");
viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
TextView edtName, edtPhone, edtPass, edtAdrress;
ImageView imageView;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
edtName = view.findViewById(R.id.edtUname);
imageView = view.findViewById(R.id.imageView);
}
}
}
Changes in your layout you need to set id to your imageview and textview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:id="#+id/edtUname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
here is the good tutorial of Android working with Card View and Recycler View

Related

CardView not showing inside fragment

I am a novice Java/Android studio student, just blindly following examples on the internet.
I wanted to make a CardView inside a fragment, but most of the examples out there were putting cardview inside an activity, so I searched and changed the appropriate codes. After some time tinkering, I managed to make it build without errors.
However, cardview is not showing up in RecyclerView.
I suspect it is a layout issue, but after all the changes with layout_height and layout_width, I can't get cardview to be seen.
Any help will be appreciated.
My code:
HomeFragment.java
public class HomeFragment extends Fragment implements View.OnClickListener {
final int ITEM_SIZE = 5;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
Button bh = v.findViewById(R.id.bh);
bh.setOnClickListener(this);
RecyclerView rv = (RecyclerView) v.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
rv.setHasFixedSize(true);
rv.setLayoutManager(layoutManager);
List<Item> items = new ArrayList<>();
Item[] item = new Item[ITEM_SIZE];
item[0] = new Item(R.drawable.a, "#1");
item[1] = new Item(R.drawable.b, "#2");
item[2] = new Item(R.drawable.c, "#3");
item[3] = new Item(R.drawable.d, "#4");
item[4] = new Item(R.drawable.e, "#5");
RecyclerAdapter adaptor = new RecyclerAdapter(getActivity(), items, R.layout.fragment_home);
rv.setAdapter(adaptor);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bh:
((BottomNavigationView)getActivity().findViewById(R.id.nav_view)).setSelectedItemId(R.id.navigation_settings);
break;
}
}
}
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context context;
List<Item> items;
int item_layout;
public RecyclerAdapter(Context context, List<Item> items, int item_layout) {
this.context = context;
this.items = items;
this.item_layout = item_layout;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_home, null);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Item item = items.get(position);
Drawable drawable = ContextCompat.getDrawable(context, item.getImage());
holder.image.setBackground(drawable);
holder.title2.setText(item.getTitle());
holder.cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, item.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return this.items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title2;
CardView cardview;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
title2 = (TextView) itemView.findViewById(R.id.title2);
cardview = (CardView) itemView.findViewById(R.id.cardview);
}
}
}
Item.java
public class Item {
int image;
String title;
int getImage() { return this.image; }
String getTitle() { return this.title; }
public Item(int image, String title) {
this.image = image;
this.title = title;
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/MainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_home"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/bh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_home" />
<Button
android:id="#+id/bh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:text="#string/title_settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
card.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="20dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/title2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:text="TextView"
android:textSize="25sp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Seems you are inflating the wrong layout in RecyclerAdapter.java
try this
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context context;
List<Item> items;
int item_layout;
public RecyclerAdapter(Context context, List<Item> items, int item_layout) {
this.context = context;
this.items = items;
this.item_layout = item_layout;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, null);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Item item = items.get(position);
Drawable drawable = ContextCompat.getDrawable(context, item.getImage());
holder.image.setBackground(drawable);
holder.title2.setText(item.getTitle());
holder.cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, item.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return this.items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title2;
CardView cardview;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
title2 = (TextView) itemView.findViewById(R.id.title2);
cardview = (CardView) itemView.findViewById(R.id.cardview);
}
}

Item alignments are not proper in horizontal recycler view using grid layout manager

The view inside recycler view is not aligned properly. The items inside recyclerview are having different height on adding new item.
I tried of using display metrics but could not achieve it. My item list can be changed by adding or deleting items but the view padding, width and height must h=not get disturbed.
categoryRecyclerView = findViewById(R.id.categoriesRecyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1,GridLayoutManager.HORIZONTAL, false);
categoryRecyclerView.setLayoutManager(gridLayoutManager);
categoryRecyclerView.addItemDecoration(new ItemDecorator(getResources().getDimensionPixelSize(R.dimen.item_spacing), 3));
CategoryItemAdapter categoryItemAdapter = new CategoryItemAdapter(this, categoryList);
categoryRecyclerView.setAdapter(categoryItemAdapter);
Code for Adapter
public class CategoryItemAdapter extends RecyclerView.Adapter<CategoryItemAdapter.MyViewHolder> {
private Context context;
private List<Category> categoryList;
private onItemClickListener mItemClickListener;
public CategoryItemAdapter(Context context, List<Category> categoryList) {
this.context = context;
this.categoryList = categoryList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryItemAdapter.MyViewHolder holder, int position) {
Category category = (Category) categoryList.get(position);
holder.textView.setText(category.getCategoryName());
}
#Override
public int getItemCount() {
return categoryList.size();
}
public void setOnItemClickListener(onItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public interface onItemClickListener {
void onItemClickListener(View view, int position, Category category);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cardview;
TextView textView;
ImageView selectItem;
RelativeLayout selectItemLayout;
LinearLayout linearLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
cardview = itemView.findViewById(R.id.cardview);
textView = itemView.findViewById(R.id.categoryName);
linearLayout = itemView.findViewById(R.id.llbackground);
selectItemLayout = itemView.findViewById(R.id.selectItemLayout);
selectItem = itemView.findViewById(R.id.selectItem);
selectItemLayout.setVisibility(authenticationHelper.isAdmin() ? View.VISIBLE : View.GONE);
itemView.setOnClickListener(this);
Util.setRandomGradientColor(linearLayout, GradientDrawable.Orientation.LEFT_RIGHT);
}
#Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClickListener(view, getAdapterPosition(), categoryList.get(getAdapterPosition()));
}
}
}
}
Code for view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardview"
android:layout_width="150dp"
android:layout_height="100dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp">
<LinearLayout
android:id="#+id/llbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/category_gradient"
android:gravity="center">
<TextView
android:id="#+id/categoryName"
android:text="Men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#color/colorWhite"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/selectItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
android:padding="5dp">
<ImageView
android:id="#+id/selectItem"
android:layout_width="20dp"
android:layout_height="20dp"
android:clickable="false"
android:tag="#string/unchecked"
android:enabled="false"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/circle"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Recycler view in activity
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/categoriesRecyclerView"
android:layout_below="#+id/categoriesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
Thanks for help!!

Application keeps crashing with "E/RecyclerView: No adapter attached; skipping layout"

My application keeps crashing whenever I try to open up the layout with a RecyclerView.
Apparently, I am getting this error:
E/RecyclerView: No adapter attached; skipping layout
The code was mostly copied from the documentation.
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textView;
public MyViewHolder(TextView v) {
super(v);
textView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
}
ChatActivity.java
public class ChatActivity extends AppCompatActivity{
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
String[] myDataset = {"Hello", "Who r u", "Wait what", "Nice to meet you", "Yeet"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
recyclerView.setAdapter(mAdapter);
}
}
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back_button"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
my_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/textView" />
</LinearLayout>
Your my_text_view.xml should contain the TextView as root element according your implementation.
Approach-1:
So, just change this like below solve your problem.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/textView" />
Approach-2:
Or change your onCreateViewHolder implementation:
onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
And change ViewHolder also:
MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textView;
public MyViewHolder(View v) {
super(v); textView = v;
textView = v.findViewById(R.id.textView);
}
}

What's wrong with my implementation of RecyclerView?

I am following the Android Developer's explanatin on how to set RecyclerView, but I get some errors, for example android.widget.LinearLayout cannot be cast to android.widget.TextView.
The code taken from here : RecyclerView
This is my code, including all necessary files:
MainActivity.java:
package com.example.lastlocation.recyclerviewer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
String[] myDataset = new String[5];
myDataset[0] = "Data0";
myDataset[1] = "Data1";
myDataset[2] = "Data2";
myDataset[3] = "Data3";
myDataset[4] = "Data4";
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
}
This is MyAdapter.java:
package com.example.lastlocation.recyclerviewer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(MyViewHolder 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]);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
}
this is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<View
android:id="#+id/view"
android:layout_width="386dp"
android:layout_height="135dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="66dp"
android:layout_height="64dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/view"
app:srcCompat="#android:color/holo_orange_dark" />
<TextView
android:id="#+id/textView2"
android:layout_width="68dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:text="John Doe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="375dp"
android:layout_height="367dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.428"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
And this is the xml for a single text view called my_text_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
As you can see I nearly copied the code from the Android Developer's website, but I still don't know where is the problem.
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
The problem is in your ViewHolder constructor it should be like that
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(View v) {
super(v);
mTextView = v.findViewById(R.id.your_text_view_id);
}
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. This will fix the problem.
The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it.
You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below
#Override
public UserListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ViewHolder vh = new ViewHolder((inflater.inflate(R.layout.your_row_layout, parent, false)));
return vh;
}
and change your viewholder as below
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(View v) {
super(v);
mTextView = v.findViewById(R.id.my_text_view);
}
}
Just Simply you need to change your onCreateViewHolder method by taking as
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
return view;

LinearLayout within Recyclerview, within another LinearLayout within another RecyclerView

Good morning, I have a problem trying to link a Recyclerview with another Recyclerview, I mean one within others, the first recyclerview is in my activity, which in turn is filler for a second recyclerview, which is in a LinearLayout, since Once that recyclerview is filled by another layout that has an ImageView and a TextView, the problem is that I can not use the two recycler view since the app stops, but if I use the layout separately if they work in the same way, I know if I explain, I have 3 layouts which are:
The problem is that I can not use both recycler view since the app stops, but if I use the layout separately if they work in the same way, I do not know if I explain myself, I have 3 layouts, the one in the activity that contains a recyclerview, the container of the elements that contains the second recyclerview and a texview, and finally the layout portraitLayout that contains an imageView and a texView, if I only use the layout of the activity, the second one is rendered well but without any image, and if I remove the layout of the activity and leave the second with the third this is rendered with their respective images.
I have 2 adapters which are:
AdapterPortrait:
`public class Adapter_Portrait extends RecyclerView.Adapter<Adapter_Portrait.ViewHolder> {
private List<Portrait> portraits;
private int layout;
private OnItemClickListener listener;
private Context context;
public Adapter_Portrait(List<Portrait> portraits, int layout, OnItemClickListener listener) {
this.portraits = portraits;
this.layout = layout;
this.listener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(this.layout,parent,false);
ViewHolder vh=new ViewHolder(v);
context=parent.getContext();
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(portraits.get(position),this.listener);
}
#Override
public int getItemCount() {
return portraits.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
this.imageView = itemView.findViewById(R.id.imagePortrait);
this.textView = itemView.findViewById(R.id.textNamePortrait);
}
public void bind(final Portrait portrait, final OnItemClickListener listener){
this.textView.setText(portrait.getName_portrait());
Picasso.with(context).load(portrait.getImagePortrait()).fit().into(imageView);
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
listener.onItemClick(portrait, getAdapterPosition());
}
});
}
}
public interface OnItemClickListener{
void onItemClick(Portrait portrait, int position);
}
}`
AdapterList_Pieces:
`public class AdapterList_Pieces extends
RecyclerView.Adapter<AdapterList_Pieces.ViewHolder> {
private List<Artist> artists;
private int layout;
private OnItemClickListener listener;
private Context context;
public AdapterList_Pieces(List<Artist> artists, int layout, OnItemClickListener listener) {
this.artists = artists;
this.layout = layout;
this.listener = listener;
}
public class ViewHolder extends RecyclerView.ViewHolder{
RecyclerView recyclerView;
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
this.recyclerView = itemView.findViewById(R.id.RecyclerViewPieces);
this.textView = itemView.findViewById(R.id.textViewPieces);
}
public void bind(final Artist artist, final OnItemClickListener listener){
this.textView.setText(artist.getName_artist());
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
listener.onItemClick(artist, getAdapterPosition());
}
});
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(this.layout,parent,false);
ViewHolder vh=new ViewHolder(v);
context=parent.getContext();
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(artists.get(position),this.listener);
}
#Override
public int getItemCount() {
return artists.size();
}
public interface OnItemClickListener{
void onItemClick(Artist artist, int position);
}
}`
My Activity is:
public class PiecesActivity extends AppCompatActivity {
List<Artist> artists;
List<Portrait> portraits;
RecyclerView recyclerView;
RecyclerView recyclerViewPiece;
RecyclerView.Adapter rAdapter;
RecyclerView.LayoutManager layoutManagerPiece;
RecyclerView.Adapter AdapterPiece;
RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.portraitcontenetorslayout);
recyclerView= findViewById(R.id.recyclerViewListPieces);
recyclerViewPiece= findViewById(R.id.RecyclerViewPieces);
artists= ConsultingInformation.getArtist();
portraits= ConsultingInformation.getPortrait();
layoutManager = new LinearLayoutManager(this);
layoutManagerPiece = new LinearLayoutManager(this);
rAdapter= new Adapter_Portrait(portraits,R.layout.portraitlayout, new Adapter_Portrait.OnItemClickListener(){
#Override
public void onItemClick(Portrait portrait, int position) {
Toast.makeText(PiecesActivity.this, "This piece is "+ portraits.get(position).getName_portrait(), Toast.LENGTH_LONG).show();
}
});
recyclerViewPiece.setHasFixedSize(true);
recyclerViewPiece.setItemAnimator(new DefaultItemAnimator());
recyclerViewPiece.setLayoutManager(layoutManagerPiece);
recyclerViewPiece.setAdapter(rAdapter);
AdapterPiece= new AdapterList_Pieces(artists,R.layout.portraitcontenetorslayout, new AdapterList_Pieces.OnItemClickListener(){
#Override
public void onItemClick(Artist artist, int positionArt) {
Toast.makeText(PiecesActivity.this, "This list of pieces is "+ artists.get(positionArt).getName_artist(), Toast.LENGTH_LONG).show();
}
});
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(AdapterPiece);
}
}`
I had read that it was not possible to put a list downloadble within another, but i did not get an unstable method for my case that are two list of objects one within another.
My layouts just in case.
PortraitLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content">
<ImageView
android:id="#+id/imagePortrait"
android:layout_width="78dp"
android:layout_height="63dp"
android:layout_marginLeft="25dp"
android:textAlignment="center"
android:contentDescription="TODO"
android:layout_marginStart="25dp" />
<TextView
android:id="#+id/textNamePortrait"
android:layout_width="225dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="TextView"
android:layout_marginStart="10dp" />
</LinearLayout>
portraitcontenetorlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewPieces"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#BCBCBC"
android:paddingLeft="7dp"
android:text=" TextView"
android:gravity="center_vertical" />
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerViewPieces"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
activitypieces:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="30dp"
android:text="Obras" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewListPieces"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Categories

Resources