Getting Problem In RecycleView with TextInputEditText - java

I am a new Programmer, actually I was trying to make an app. I was trying to make an activity that uses RecyclerView to Show TextInputEditText with spinner and TextView. It means that I was trying to make an activity that uses Recyclerview and In the layout of Recycler View, I have used Text Input Edit Text and Spinner and TextView. I have put these three in a single layout. But the main problem that I facing is that when I launch my app with my code then it remains blank activity it is not showing TextInputEditText nor spinner and nor TextView.
So the conclusion is I want to show [TextInputEditText, Spinner and TextView] in RecyclerView But my code is returning Blank Activity.
Here is my code:-
It is my adapter class:-
public class SpecialHoursAdapter extends RecyclerView.Adapter<SpecialHoursAdapter.SpecialHourHolder> {
// private ArrayList[] mDataSet;
private List<SpecialHorusModal> modal = new ArrayList<>();
private Context context;
public SpecialHoursAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public SpecialHourHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.special_hours_layout_card, parent, false);
return new SpecialHourHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SpecialHourHolder holder, int position) {
}
#Override
public int getItemCount() {
return modal.size();
}
public static class SpecialHourHolder extends RecyclerView.ViewHolder {
private TextInputEditText editText;
private Spinner spinner;
private TextView textView;
public SpecialHourHolder(#NonNull View itemView) {
super(itemView);
editText = itemView.findViewById(R.id.special_hours_editor);
spinner = itemView.findViewById(R.id.spinner_special_hours_date);
textView = itemView.findViewById(R.id.time_picker_special_horus);
}
}
}
It is My MainActivity:-
public class SpecialHoursActivity extends AppCompatActivity {
private RecyclerView containerRecyclerView;
private RecyclerView.LayoutManager first, container;
private SpecialHoursAdapter adapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_special_horus);
// mainRecyclerView = findViewById(R.id.activity_special_horus_main_recyclerview);
containerRecyclerView = findViewById(R.id.layout_container_recyclerview_special_hours);
// mainRecyclerView.setHasFixedSize(true);
containerRecyclerView.setHasFixedSize(true);
first = new LinearLayoutManager(this);
container = new LinearLayoutManager(this);
// mainRecyclerView.setLayoutManager(first);
containerRecyclerView.setLayoutManager(container);
containerRecyclerView.setAdapter(adapter);
}
}
Here is my RecyclerView Layout:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/special_hours_editor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Special Hours"
android:inputType="text"
android:maxLength="20" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="#+id/spinner_special_hours_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time_picker_special_horus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:text="00:00" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
My Main Layout -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_special_horus_main_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/layout_container_recyclerview_special_hours"
android:orientation="vertical"
android:scrollbars="vertical">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
I have not used any modal class because I don't know how to use it with Widgets.

you need to pass the data to your adapter
public SpecialHoursAdapter(Context context,List<SpecialHorusModal> data) {
this.context = context;
this.model=data; //it will show your recyclerview
}
we could not show the Recyclerview with empty data

You need to populate the data your Adapter holds, adapter with empty data will obviously show no items.
public class SpecialHoursAdapter extends RecyclerView.Adapter<SpecialHoursAdapter.SpecialHourHolder> {
private List<SpecialHorusModal> items = new ArrayList<>();
private Context context;
public SpecialHoursAdapter(Context context) {
this.context = context;
}
//Call this in onClick of your button, passing it the object to add
public void addItem(SpecialHoursModal item) {
items.add(item);
notifyDataSetChange(); //This is important
}
#NonNull
#Override
public SpecialHourHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.special_hours_layout_card, parent, false);
return new SpecialHourHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SpecialHourHolder holder, int position) {
//Bind the data to view for the object at this position
SpecialHorusModal item = items.get(position);
//...
}
#Override
public int getItemCount() {
return items.size();
}
public static class SpecialHourHolder extends RecyclerView.ViewHolder {
private TextInputEditText editText;
private Spinner spinner;
private TextView textView;
public SpecialHourHolder(#NonNull View itemView) {
super(itemView);
editText = itemView.findViewById(R.id.special_hours_editor);
spinner = itemView.findViewById(R.id.spinner_special_hours_date);
textView = itemView.findViewById(R.id.time_picker_special_horus);
}
}
}
When your button is clicked, or you need to add an item:
adapter.addItem(new SpecialHorusModal(...));

Related

How to select item from ImageView in ScrollView?

Please bear with me as I am new to android studio. I want to make a ScrollView containing images with corresponding name (TextView). I want to be able to select an image by touching it in the ScrollView, but I don't know how. I have implemented the ScrollView like this, I also want to be able to add pictures with a name attached to them.
Main_activity.xml
<HorizontalScrollView
android:id="#+id/scrollFilterView"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.57"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="#+id/gallery"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
scrollview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/filterView"
android:layout_width="90dp"
android:layout_height="90dp"
app:srcCompat="#android:drawable/sym_def_app_icon"
android:contentDescription="filterView" />
<TextView
android:id="#+id/textFilter"
android:layout_width="90dp"
android:gravity="center"
android:layout_height="wrap_content"
android:text="textFilter" />
MainActivity.java
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < NUMBER_OF_FILTERS_GRID; i++ ) {
View scrollView = inflater.inflate(R.layout.scrollview, gallery, false);
TextView textview = scrollView.findViewById(R.id.textFilter);
textview.setText("Filter "+ i);
ImageView filterView = scrollView.findViewById(R.id.filterView);
filterView.setImageResource(R.mipmap.ic_launcher);
gallery.addView(scrollView);
}
}
Some input to if this is the right path to take and what functions i should use is very appreciated :)
Use a RecyclerView instead of a ListView, and used with and adapter, this will grant you more control over each item displayed on the list (in this case in the RecyclerView),
you will need to create and Adapter class, a simple class for your Items and a new XML layout that will serve as the model of each item in the list.
Simple class for your items:
public class dataSetItem {
private Integer image;
private String listItemText;
public Integer getImage() {
return image;
}
public void setImage(Integer image) {
this.image = image;
}
public String getListItemText() {
return listItemText;
}
public void setListItemText(String listItemText) {
this.listItemText = listItemText;
}
}
In your mainActivity remove the listView and for loop, use this instead:
public class MyActivity extends Activity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList mAdapter = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
recyclerView = (RecyclerView) findViewById(R.id.your_recycler_view);
mDataset.add(dataSetItem("item 1", R.mipmap.ic_launcher))
mDataset.add(dataSetItem("item 2", R.mipmap.ic_launcher))
// You can add more items to the mDataset list and call the adapter again to update the RecyclerView
// 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);
}
// ...
}
Create this XML layout which will serve as the model for each item, called my_item_list_layout:
<?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:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/the_image_i_want_to_click"
android:layout_width="80dp"
android:layout_margin="10dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="#color/ADRBlack"
/>
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="220dp"
android:textColor="#color/Black"
android:textSize="16sp"
tools:text="Example Restaurant" />
</LinearLayout>
</LinearLayout>
Now the adapter class, in this case called MyAdapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private customItem[] 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(customItem[] 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.my_item_list_layout, 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].getListItemText);
holder.textView.setBackground(mDataset[position].getImage);
//Now to do something when you clic the image use this
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
//Your action here
}
});
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
}

ListView CustomAdapter not showing first position results

I'm new to Android Studio and coding in general and I am trying to display a custom ListView with an image of a colour and the name of the colour. I have used a custom Adapter to inflate the view with two arrays containing the drawable image colours and string of the names.
The problem I am having - the custom adapter is only displaying the layers from position 1 and onwards. For example, where the first layer should be img_01 and "red" - it is displaying img_02 and "Orange".
I've tried debugging the code and it seems that though the adapter is setting the values for position 0, but I cannot find the reason why it is not displaying (as the others are displaying fine).
The images & names are just placeholders, so an alternative solution that doesn't include both the drawables and names wouldn't be viable,
Thank you in advance
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context context;
int[] images;
String[] colour;
LayoutInflater mInflater;
public CustomAdapter(Context c, int[] i, String[] col) {
this.context = c; //sets the application context that has been passed to the adapter
this.images = i; //sets the images array that has been passed to the adapter
this.colour = col; //sets the colour array that has been passed to the adapter
mInflater = (LayoutInflater.from(context)); //sets the layout inflater from context
}
#Override
public int getCount() { //total number of elements in the array
return images.length;
}
#Override
public Object getItem(int position) { //gets the item using the position
return null;
}
#Override
public long getItemId(int position) { //gets the item position
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) { //this is used to update the view
convertView = mInflater.inflate(R.layout.custom_layout, null);
ImageView image = convertView.findViewById(R.id.imageView);
TextView text = convertView.findViewById(R.id.textView);
text.setText(colour[position]);
image.setImageResource(images[position]);
return convertView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView mListView;
int[] images = {R.drawable.img01,
R.drawable.img02,
R.drawable.img03,
R.drawable.img04};
String[] colour = {"Red",
"Orange",
"Yellow",
"Green"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), images, colour);
//passes information of images and application context to the item adapter
mListView.setAdapter(customAdapter);
//sets the adapter to the listview
}
}
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="9dp"
app:srcCompat="#drawable/img01" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="36dp"
android:text="colourName" />
</RelativeLayout>
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try to set the height of the ListView to wrap_content instead of hardcoding it
<ListView
...
android:layout_height="wrap_content"/>
You can do it with RecyclerView.
Create a custom class
public class ExampleItem {
private int mImageResource; //your color image
private String mText1; //your text
public ExampleItem(int imageResource, String text1) {
mImageResource = imageResource;
mText1 = text1;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
}
Create a XML layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="2dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:text="Line 1"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Create a Recycler View Adapter
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
}
#Override
public int getItemCount() {
return mExampleList.size();
}
}
your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.application.recyclerviewproject.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical" />
</RelativeLayout>
MainActivity.java
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);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.img01, "Red"));
exampleList.add(new ExampleItem(R.drawable.img02, "Orange"));
exampleList.add(new ExampleItem(R.drawable.img03, "Yellow"));
exampleList.add(new ExampleItem(R.drawable.img04, "Green"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}

"E/RecyclerView: No adapter attached; skipping layout"

So, for a personal project I'm making a stream app for Android which are separated by three fragments in the main screen where each of those fragment will be a list of a type of show in our archive, like movies, TV series and animations by using a RecyclerView inside of them.
That being said, after some errors that I managed to fix, the app is not showing what was expected, by skipping the RecyclerView because it has no "adapter".
Here is the code so far:
The main activity:
public class MenuPrincipal extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_principal);
AbasMenuPrincipalAdapter adapter = new AbasMenuPrincipalAdapter( getSupportFragmentManager() );
adapter.adicionar( new FilmesFragment() , "Filmes");
adapter.adicionar( new SeriesFragment() , "Series");
adapter.adicionar( new AnimacoesFragment() , "Animações");
ViewPager viewPager = (ViewPager) findViewById(R.id.abas_view_pager);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.abas);
tabLayout.setupWithViewPager(viewPager);
}
}
One of the fragments used on the main screen:
public class AnimacoesFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
ArrayList<Item> exemploItemList = new ArrayList<Item>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.animacoes_fragment, container, false);
TextView tv = view.findViewById(R.id.text1);
tv.setText("Você está na terceira aba");
exemploItemList.add(new Item(R.drawable.ic_android, "Line1", "line2"));
exemploItemList.add(new Item(R.drawable.ic_desktop_mac, "Line2", "line3"));
exemploItemList.add(new Item(R.drawable.ic_laptop_windows, "Line4", "line5"));
mRecyclerView = (RecyclerView) view.findViewById(R.id.AnimacaoRecyclerView);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
ItemAdapter adapter = new ItemAdapter(exemploItemList);
mAdapter = adapter;
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
And its layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/AnimacaoRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:scrollbars="vertical" />
</LinearLayout>
This is what I'm trying to show on that RecyclerView (later will be the real thing, now it's just a test):
public class Item {
private int mImageResource;
private String text1;
private String text2;
public Item(int mImageResource, String text1, String text2) {
this.mImageResource = mImageResource;
this.text1 = text1;
this.text2 = text2;
}
public int getmImageResource() {
return mImageResource;
}
public void setmImageResource(int mImageResource) {
this.mImageResource = mImageResource;
}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
}
It's layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/imagem"
android:padding="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="0dp"
android:text="#string/line1"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/textView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="28dp"
android:text="#string/line2"
android:textSize="15sp"
android:textStyle="bold"
android:id="#+id/textView2"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Finally, the adapter used to convert the list:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private ArrayList<Item> mItemList;
public static class ItemViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
ItemViewHolder ivh = new ItemViewHolder(v);
return ivh;
}
public ItemAdapter(ArrayList<Item> itemList){
mItemList = itemList;
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Item currentItem = mItemList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
#Override
public int getItemCount() {
return mItemList.size();
}
}
First of all, check your fragment layout file. The linear layout parent orientation set to 'horizontal' and width of it's child views ('textview' & 'recyclerview') is match parent. This won't work.
Try to initialize your recyclerview adapter in 'onCreate' and use 'notifyDataSetChanged()' after setting up the adapter with recyclerview.

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>

Nested RecyclerView not getting focus

I have a RecyclerView that contains a bunch of CardViews. Each CardView then has its own RecyclerView in it. The problem is that only the "main" RecyclerView is scrolling. The nested one in the CardView is not getting focus, so I can't scroll it. How can i make it so that both will be scrollable?
Parent Layout
<android.support.design.widget.CoordinatorLayout android:id="#+id/coordinatorLayout_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_savedFiles"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="8dp"/>
</android.support.design.widget.CoordinatorLayout>
Parent Adapter
public class AdapterSavedFilesCards extends RecyclerView.Adapter<AdapterSavedFilesCards.ViewHolderSavedFilesCard>{
/**
* List of cards in the recycler view
*/
private final ArrayList<CardViewSavedFiles> cards;
private final Context context;
public AdapterSavedFilesCards(final Context context){
this.context = context;
cards = new ArrayList<>(Arrays.asList(new CardViewSavedFiles[]{
new CardViewSavedFiles("Card One"),
new CardViewSavedFiles("Card Two"),
new CardViewSavedFiles("Card Three")
}));
}
#Override
public ViewHolderSavedFilesCard onCreateViewHolder(ViewGroup parent, int viewType){
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_saved_files, parent, false);
return new AdapterSavedFilesCards.ViewHolderSavedFilesCard(view);
}
#Override
public void onBindViewHolder(ViewHolderSavedFilesCard holder, int position){
//Get the card at this position
final CardViewSavedFiles card = cards.get(position);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.recyclerView.setItemAnimator(null);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setAdapter(card.getAdapterSavedFiles());
holder.title.setText(card.getTitle());
holder.subTitle.setText("Subtitle");
((CardView)holder.itemView).setCardBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), card.getBackgroundColor()));
}
#Override
public int getItemCount(){
return cards.size();
}
public ArrayList<CardViewSavedFiles> getCards(){
return cards;
}
protected class ViewHolderSavedFilesCard extends RecyclerView.ViewHolder{
private final TextView title;
private final TextView subTitle;
private final RecyclerView recyclerView;
public ViewHolderSavedFilesCard(final View itemView){
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
subTitle = (TextView) itemView.findViewById(R.id.subTitle);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
}
}
}
Child Layout
<android.support.v7.widget.CardView
android:id="#+id/cardView_savedFactors"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="128dp"
android:paddingStart="#dimen/content_padding_start"
android:paddingEnd="#dimen/content_padding_end"/>
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Child Adapter
public class AdapterSavedFiles extends RecyclerView.Adapter<AdapterSavedFiles.ViewHolderSavedFiles>{
private final ArrayList<File> files = new ArrayList<>();
private final FILE_TYPE fileType;
public AdapterSavedFiles(final Context context, final FILE_TYPE fileType){
this.fileType = fileType;
switch (fileType){
case ONE:
files.addAll(Arrays.asList(FileManager.getInstance(context).getFilesDirOne().listFiles()));
break;
case TWO:
files.addAll(Arrays.asList(FileManager.getInstance(context).getFilesDirTwo().listFiles()));
break;
}
}
#Override
public ViewHolderSavedFiles onCreateViewHolder(ViewGroup parent, int viewType){
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_saved_file, parent, false);
return new AdapterSavedFiles.ViewHolderSavedFiles(view);
}
#Override
public void onBindViewHolder(ViewHolderSavedFiles holder, int position){
holder.fileName.setText("List item " + position);
switch (fileType){
case ONE:
holder.icon.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.itemView.getContext(), R.color.primary)));
holder.icon.setText("P");
break;
case TWO:
holder.icon.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(holder.itemView.getContext(), R.color.green)));
holder.icon.setText("F");
break;
}
}
#Override
public int getItemCount(){
return files.size();
}
public ArrayList<File> getFiles(){
return files;
}
protected class ViewHolderSavedFiles extends RecyclerView.ViewHolder{
private final TextView fileName;
private final TextView icon;
public ViewHolderSavedFiles(final View itemView){
super(itemView);
icon = (TextView) itemView.findViewById(R.id.icon);
fileName = (TextView) itemView.findViewById(R.id.file_name);
}
}
}
Layout Screenshot
You may use ListView in Child adapter in onBindViewHolder instead of TextView.This may solve scrolls child items inside CardView.

Categories

Resources