XML:
<androidx.recyclerview.widget.RecyclerView
android:visibility="visible"
android:id="#+id/itemshowRecylerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/singleitemforrecylerview"/>
Java Code:
itemshowRecylerview.setLayoutManager(new LinearLayoutManager(MainActivity.this));
itemshowRecylerview.setHasFixedSize(true);
Viewholder Code:
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.singleitemforrecylerview, null,false);
return new ItemDetailsAdapter.ViewHolder(view);
}
You need to change your custom adapter class, pass parent instead of null in your inflate method
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.singleitemforrecylerview, parent,false);
return new ItemDetailsAdapter.ViewHolder(view);
}
You have use this type of RecyclerView set orientation and layoutManager in RecyclerView and working code shown in below.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:spanCount="1">
</androidx.recyclerview.widget.RecyclerView>
recyclerView.setAdapter(yourAdapter);
Related
I just created a list view with a custom view to create a country list for select
<?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="match_parent"
android:background="#color/primary_light"
android:orientation="vertical"
tools:context=".RnActivity.RnView.RnUser.ActivitySignUpOptions">
<ListView
android:id="#+id/country_list_for_select_list_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primary"
/>
</LinearLayout>
custom view
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/view_layout_country_list_item_country_icon_image"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="#drawable/gjh_logo_b"
/>
<TextView
android:id="#+id/view_layout_country_list_item_country_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="start|center_vertical"
android:layout_weight="1"
android:text="India"
android:textSize="18sp" />
<TextView
android:id="#+id/view_layout_country_list_item_country_code"
android:layout_width="50dp"
android:layout_height="match_parent"
android:gravity="center|center_vertical"
android:layout_gravity="right"
android:text="IN"
android:textSize="18sp"
/>
</LinearLayout>
In Activity
ListView country_list_for_select_list_area;
#Override
public void lodeControls() {
country_list_for_select_list_area=findViewById(R.id.country_list_for_select_list_area);
}
#Override
public void setEvents() {
}
RxAdopterCountry countryAdopter;
#Override
public void createAdopters() {
countryAdopter=new RxAdopterCountry(getActivity(),R.layout.view_layout_country_list_item, RsXtraCountryList.getInstance().getCountryList());
}
#Override
public void setData() {
country_list_for_select_list_area.setAdapter(countryAdopter);
}
In Adopter
private final ArrayList<RentCountry> countryList;
private Context context;
public RxAdopterCountry(#NonNull Context context, int resource, ArrayList<RentCountry> countryList) {
super(context, resource);
this.context=context;
this.countryList=countryList;
}
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.view_layout_country_list_item, parent, false);
}
ImageView view_layout_country_list_item_country_icon_image=convertView.findViewById(R.id.view_layout_country_list_item_country_icon_image);
TextView view_layout_country_list_item_country_name=convertView.findViewById(R.id.view_layout_country_list_item_country_name);
TextView view_layout_country_list_item_country_code=convertView.findViewById(R.id.view_layout_country_list_item_country_code);
view_layout_country_list_item_country_name.setText(countryList.get(position).getName());
view_layout_country_list_item_country_code.setText(countryList.get(position).getCode());
view_layout_country_list_item_country_icon_image.setImageResource(countryList.get(position).getFlag_id());
return super.getView(position, convertView, parent);
}
i debugged the whole code and there are 250 items are coming in
private final ArrayList<RentCountry> countryList;
but list view is showing nothing
What I am doing wrong? I tried everything and debugged the whole code 10 times. yes I am getting the data in the ArrayList bu still noting is appearing in the list view.
Please help
The problem with your code is you are returning super.getView(position, convertView, parent) from getView while you should be returning the View you just created that is convertView .
For super constructor call you have to use super(context, resource, countryList) so that Adapter can return the size of list.
Change getView like below
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
//...
return convertView;
}
Also you should be using RecyclerView now. ListView and GridView are legacy widgets for a long time . RecyclerView is much more flexible.
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);
}
}
I have a fragment class which uses Recycleview to show list of Text and Image.
How can I access Recycleview row items and set an custom text to any Textview or Hide the Desired Textview from fragment class.
This is my layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingBottom="4dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="4dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
<include
android:id="#+id/include_tag"
layout="#layout/row_item"/>
</FrameLayout>
and My row item layout is
<?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">
<TextView
android:id="#+id/flower_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:maxLines="1"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textColor="#E91E63"
android:fontFamily="sans-serif-condensed"
android:text="Sagar Rawal"/>
<TextView
android:layout_marginTop="8dp"
android:id="#+id/flower_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
android:maxLines="3"
android:textColor="#673AB7"
android:text="This Text need to Be Change "/>
</RelativeLayout>
and my Fragment class i s
public View onCreateView(final LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity_main, container, false);
View test1View = view.findViewById(R.id.include_tag);
mhideText = (TextView) test1View.findViewById(R.id.flower_details);
mhideText.setVisibility(View.INVISIBLE);
return view;
But it doesn't work. and Textview is still Visible.
Please help. Thanks in advance.
You must have a custom ViewHolder which should extends RecyclerView.ViewHolder. In your ViewHolder you can link row item and you can access all views from your row layout.
Your custom ViewHolder looks like:
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;
//Note: To set custom text
textView.setText("My custom text");
//Note: To hide TextView remove comment from below line
//textView.setVisibility(View.GONE);
}
}
Above code shows sample ViewHolder as MyViewHolder where you can access your views.
If you are passing parent Layout to MyViewHolder instead of passing TextView then you can find child views using v.findViewById(R.id.xxx)
Your adapter should contain similar code as below:
#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;
}
In above code snippet, creating instance of MyViewHolder by passing the view TextView. You can pass parent layout in your case its RelativeLayout from your row layout.
Use this ViewHolder to take any actions on views like set custom text to TextView or you can hide TextView as per your requirements.
To refer full example you can follow the android documentation or click here.
To achieve communication between ViewHolder and fragment you can use Interface.
Thank you!
Try doing this :
rowview = inflater.inflate(R.layout.row_item, container, false);
mhideText = (TextView)rowview.findViewById(R.id.flower_details);
mhideText.setVisibility(View.INVISIBLE);
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;
Im super confused between what is passed to the methods initially.
In ArrayAdapter getview method, What is the View and ViewGroup passed
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
....
}
List_item xml is
<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"
android:weightSum="1">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:fontFamily="serif"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
....
</LinearLayout>
What is the ViewGroup passed in RecyclerView adapter here.
#Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
Number_list_item is
<FrameLayout 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:padding="16dp">
<TextView
android:id="#+id/tv_item_number"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:fontFamily="monospace"
android:textSize="42sp"
tools:text="#42" />
</FrameLayout>
What does the below line do exacty besides inflating the layout.
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately
View view = inflater.inflate(layoutIdForListItem, viewGroup,
shouldAttachToParentImmediately);
When you want to show any view on UI it must have some height and width. In a inflate method,
First parameter is a resource id of a view, which should inflate. In a case of Adapter, it should be item view which is going to be recycled.
Second parameter is a ViewGroup, generally parent view. Parent view must be a ViewGroup because ViewGroup contains child views not vice versa. In a case of ListView sometimes size(width) of an inflated view not adopted automatically depends on its parent.
For an example - If a ListView have a width match_parent then there is a possibility width of your inflated view less than its parent if you skipped this parameter.
Third parameter is used to decide whether you want to add your inflated view inside parent ViewGroup(2nd parameter). In a case of ListView, if you make it true then you will get RunTimeException (View already have a parent), this parameter should be true if you want to add new view inside parent view.
For an example - If I want to add View in my UI only after sudden action, then I will inflate a 'View' and add in a ViewGroup.