RecyclerView Fatal Exception:main - android

Can some one assist me to know what has been causing this error in My recyclerview i have tried changing the variable name at
Adapter1.java:48
But it still giving me the same error.
java.lang.NullPointerException
at ke.co.clickaway.squirrel.clickawaytechnologies.adpter.Adapter1.getItemCount(Adapter1.java:48)
the code is as follows
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ArrayList<DataList> list;
//in the onCreate Bundle
RecyclerView rview=(RecyclerView) findViewById(R.id.rview);
Adapter1 adapter1=new Adapter1(this,list);
rview.setAdapter(adapter1);
StaggeredGridLayoutManager lm=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
rview.setLayoutManager(lm);
the information class
public class DataList {
public int imageView;
public String title;
public static List<DataList> getData(){
ArrayList<DataList> mList=new ArrayList<>();
int[] imag={R.drawable.web_ic,R.drawable.palette_ic,R.drawable.pencil,R.drawable.sware_ic,R.drawable.phone_ic};
String[] lable={"Web Development","Branding","Graphic Design","Systems","Mobile Apps"};
for(int i=0;i<lable.length&& i<imag.length;i++){
DataList dataList=new DataList();
dataList.title=lable[i];
dataList.imageView=imag[i];
mList.add(dataList);
}
return mList;
}
}
And My adapter
public class Adapter1 extends RecyclerView.Adapter<Adapter1.ViewHolder> {
private List<DataList> mList;
private Context mContext;
public Adapter1(Context context,List<DataList> list){
mList=list;
mContext=context;
}
private Context getContext(){
return mContext;
}
#Override
public Adapter1.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context=parent.getContext();
LayoutInflater inflater=LayoutInflater.from(context);
View customView=inflater.inflate(R.layout.custom_view,parent,false);
ViewHolder viewHolder=new ViewHolder(customView);
return viewHolder;
}
#Override
public void onBindViewHolder(Adapter1.ViewHolder holder, int position) {
DataList dataList=mList.get(position);
holder.imag.setImageResource(dataList.imageView);
holder.lable.setText(dataList.title);
}
#Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imag;
public TextView lable;
public ViewHolder(View itemView) {
super(itemView);
imag= (ImageView)itemView.findViewById(R.id.imageView1);
lable=(TextView)itemView.findViewById(R.id.textView1);
}
}
}
my custom View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#drawable/one" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="dummy Text" />
</LinearLayout>
the RecyclerView snippet in activty_main.xml
<android.support.v7.widget.RecyclerView
android:layout_below="#id/appbarlayout"
android:id="#+id/rview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

list is null. So initialize your list before you create your adapter or change getItemCount() method in your adapter to this:
#Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}

As per your code the list is null, so instead of using ArrayList<DataList> list in MainActivity use ArrayList<DataList> list = new ArrayList<>() or ArrayList<DataList> list = DataList.getData()

try add this code :
list = Datalist.getData();
before this :
Adapter1 adapter1=new Adapter1(this,list);

Related

how to intent in recyclerview when we click on image [duplicate]

This question already has answers here:
RecyclerView onClick
(49 answers)
Closed 1 year ago.
So,basically in this as soon as I will click on the image view i should go on the next page that is firstpage .IN this for firstpage I have used recyclerview and for the introduction pge i have used gridadapter.but when I click on the imag view its not getting intented to the next page.I tried all possible sols. ut still the same.plzz help.
this is my introduction.xml code
<?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="wrap_content"
android:background="#drawable/ic_launcher_background"
tools:context=".MainActivity2">
<androidx.cardview.widget.CardView
android:layout_width="199dp"
android:layout_height="199dp"
app:cardElevation="10dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="35dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="193dp"
android:layout_height="194dp">
<ImageView
android:id="#+id/imageView4"
android:layout_width="143dp"
android:layout_height="109dp"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="16dp"
tools:srcCompat="#tools:sample/avatars"
android:onClick="image"/>
<TextView
android:id="#+id/textView15"
android:layout_width="158dp"
android:layout_height="33dp"
android:layout_marginTop="4dp"
android:text="text"
android:textColor="#color/DarkBlue"
android:textSize="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imageView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
this is my MainActivity2.java code
public abstract class MainActivity2 extends AppCompatActivity implements ClickedListener {
RecyclerView recycleview1;
List<String> titles;
List<Integer>images;
GridAdapter adapter;
ImageView image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intropage);
recycleview1 = findViewById(R.id.recycleview1);
titles = new ArrayList<>();
images = new ArrayList<>();
titles.add("LATEST MOVIES");
titles.add("UPCOMING MOVIES");
titles.add("TRENDING MOVIES");
titles.add("NOW PLAYING");
titles.add("RELEASED MOVIES");
titles.add("HINDI MOVIES");
titles.add("MARATHI MOVIES");
titles.add("ENGLISH MOVIES");
titles.add("TELUGU MOVIES");
titles.add("TAMIL MOVIES");
images.add(R.drawable.latestmovie);
images.add(R.drawable.upcomingmovies);
images.add(R.drawable.trendingmovie);
images.add(R.drawable.nowplaying);
images.add(R.drawable.releasedmovie);
images.add(R.drawable.hindimovies);
images.add(R.drawable.marathimovies);
images.add(R.drawable.englishmovies);
images.add(R.drawable.telugumovies);
images.add(R.drawable.tamilmovies);
adapter = new GridAdapter(this,titles,images,this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recycleview1.setLayoutManager(gridLayoutManager);
recycleview1.setAdapter(adapter);
}
#Override
public void onPictureClicked() {
Intent intent = new Intent(MainActivity2.this, MainActivity1.class);
startActivity(intent);
}
}
this is my GridAdapter.java code
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<String>titles;
List<Integer>images;
LayoutInflater inflater;
public GridAdapter(Context context,List<String>titles,List<Integer>images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.intoduction,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.titles.setText(titles.get(position));
holder.images.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.textView15);
images = itemView.findViewById(R.id.imageView4);
}
}
}
this is my firstpage.xml code
<?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:background="#color/LightGrey"
android:layout_marginTop="5dp"
tools:context=".MainActivity1">
<ImageView
android:id="#+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
tools:srcCompat="#tools:sample/avatars" />
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Title"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="-10dp"
android:text="Description"
android:textSize="20sp">
</TextView>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Ratings"
android:textSize="20sp">
</TextView>
</LinearLayout>
</LinearLayout>
This interface will be implemented in class where your adapter is called.
I created an interface and implemented in MainActivity2, that interface has on override() method clickedListener. This clickedListener will be called whenever an image clicked and then the intent inside clickedListener will be called and you will be moved to MainActivity1.java.
This is your working code.
public interface ClickedListener {
void onPictureClicked();
}
GridAdapter.java
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<String>titles;
List<Integer>images;
LayoutInflater inflater;
ClickedListener clickedListener;
public GridAdapter(Context context,List<String>titles,List<Integer>images, ClickedListener clickedListener ){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(context);
this.clickedListener = clickedListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.intoduction,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.titles.setText(titles.get(position));
holder.images.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.textView15);
images = itemView.findViewById(R.id.imageView4);
images.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickedListener.onPictureClicked();
}
});
}
}
}
MainActivity2
public class MainActivity2 extends AppCompatActivity implements ClickedListener{
RecyclerView recycleview1;
List<String> titles;
List<Integer>images;
GridAdapter adapter;
ImageView image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intropage);
recycleview1 = findViewById(R.id.recycleview1);
titles = new ArrayList<>();
images = new ArrayList<>();
titles.add("LATEST MOVIES");
titles.add("UPCOMING MOVIES");
titles.add("TRENDING MOVIES");
titles.add("NOW PLAYING");
titles.add("RELEASED MOVIES");
titles.add("HINDI MOVIES");
titles.add("MARATHI MOVIES");
titles.add("ENGLISH MOVIES");
titles.add("TELUGU MOVIES");
titles.add("TAMIL MOVIES");
images.add(latestmovie);
images.add(R.drawable.upcomingmovies);
images.add(R.drawable.trendingmovie);
images.add(R.drawable.nowplaying);
images.add(R.drawable.releasedmovie);
images.add(R.drawable.hindimovies);
images.add(R.drawable.marathimovies);
images.add(R.drawable.englishmovies);
images.add(R.drawable.telugumovies);
images.add(R.drawable.tamilmovies);
adapter = new GridAdapter(this,titles,images,this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recycleview1.setLayoutManager(gridLayoutManager);
recycleview1.setAdapter(adapter);
}
}
#Override
public void onPictureClicked() {
Intent intent = new Intent(getApplicationContext(), MainActivity1.class);
startActivity(intent);
}
}
MainActivity1.java
public class MainActivity1 extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter MovieAdapter;
RecyclerView.LayoutManager layoutManager;
String[] movieNameList = {"BHOOT","PANGA","PSYCHO","JUMANJI","DIL BECHARA","BAAGHI 3","THE BRIDGE CURSE","LOST GIRLS","BAD BOYS FOR LIFE",
"THAPPAD","SADAK 2","TANHAJI","BHEESHMA" };
String[] movieDescriptionList = {"Horror/Mystery","Sport/Drama","Thriller/Psychological Thriller","Comedy/Action","Romance/Drama","Action/Thriller",
"Horror/Thriller","Mystery/Drama","Action/Comedy","Drama","Drama/Thriller","Action/Historical drama","Romance/Rom-com"};
String[] movieRatings = {"3.5/5","4.5/5","3/5","4.5/5","4/5","4/5","3.5/5","2.5/5","3/5","5/5","3.5/5","4/5","3/5"};
int[] movieImages = {R.drawable.bhoot,R.drawable.panga,R.drawable.psycho,R.drawable.jumanji,R.drawable.dil_bechera,R.drawable.baaghi3,R.drawable.bridgecurse,
R.drawable.lostgirls,R.drawable.bad_boys,R.drawable.thappad,R.drawable.sadak2,R.drawable.tanhaji,R.drawable.bheeshma};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
recyclerView = findViewById(R.id.recycleview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
MovieAdapter= new MovieAdapter(this,movieNameList,movieDescriptionList,movieRatings,movieImages);
recyclerView.setAdapter(MovieAdapter);
}
}

Scrolling RecyclerView inside a RecyclerView Item

I have a RecyclerView with some dummy string data in each item. Each item has also a recyclerview which contains some other dummy data (A - Z, see below) that will be visible when a button is clicked.
The problem I have is that I can not scroll the recyclerview within the item as I want. When I scroll, only the outer recyclerview scrolls (as you seen below)
Here how is it look like:
Here are the code I wrote:
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private StringAdapter mStringAdapter;
private ArrayList<String> mStrings = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStrings.add("This");
mStrings.add("is");
mStrings.add("a");
mStrings.add("test");
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mStringAdapter = new StringAdapter(mStrings, this);
mRecyclerView.setAdapter(mStringAdapter);
}
}
Here, the adapter resonsible for the outer main recyclerview. Note that the recyclerview of each main recyclerview item gets visible when the button is clicked :
public class StringAdapter extends RecyclerView.Adapter<StringAdapter.ViewHolder> {
private ArrayList<String> mStrings;
private Context mContext;
private SomeInnerDataAdapter mSomeInnerDataAdapter;
public StringAdapter(ArrayList<String> strings, Context context) {
mStrings = strings;
mContext = context;
}
#NonNull
#Override
public StringAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.item, viewGroup, false);
return new StringAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull StringAdapter.ViewHolder viewHolder, int i) {
String text = mStrings.get(i);
viewHolder.onBindText(text);
}
#Override
public int getItemCount() {
return mStrings.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
private RecyclerView mRecyclerView;
private Button mButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.textView);
mRecyclerView = (RecyclerView) itemView.findViewById(R.id.someOtherData);
mButton = (Button) itemView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "Item is clicked", Toast.LENGTH_LONG).show();
mRecyclerView.setVisibility(View.VISIBLE);
ArrayList<String> strings = new ArrayList<>();
strings.add("A");
strings.add("B");
strings.add("C");
strings.add("D");
strings.add("E");
strings.add("F");
strings.add("G");
strings.add("H");
strings.add("I");
strings.add("J");
strings.add("K");
strings.add("L");
strings.add("M");
strings.add("N");
strings.add("O");
strings.add("P");
strings.add("R");
strings.add("S");
strings.add("T");
strings.add("U");
strings.add("V");
strings.add("W");
strings.add("X");
strings.add("Y");
strings.add("Z");
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
mSomeInnerDataAdapter = new SomeInnerDataAdapter(strings, mContext);
mRecyclerView.setAdapter(mSomeInnerDataAdapter);
}
});
}
public void onBindText(String text){
mTextView.setText(text);
}
}
}
The adapter responsible for the recyclerview within a item:
public class SomeInnerDataAdapter extends RecyclerView.Adapter<SomeInnerDataAdapter.ViewHolder> {
private ArrayList<String> mStrings;
private Context mContext;
public SomeInnerDataAdapter(ArrayList<String> strings, Context context) {
mStrings = strings;
mContext = context;
}
#NonNull
#Override
public SomeInnerDataAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.someotherdata_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SomeInnerDataAdapter.ViewHolder viewHolder, int i) {
String text = mStrings.get(i);
viewHolder.onBindString(text);
}
#Override
public int getItemCount() {
return mStrings.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.someotherdata_textview);
}
public void onBindString(String text){
mTextView.setText(text);
}
}
}
The xml layouts are the following:
// item.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/someOtherData"
android:layout_gravity="right"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<Button
android:id="#+id/button"
android:text="show comment"
android:layout_gravity="bottom|center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
and someotherdata_item.xml:
<TextView
android:id="#+id/someotherdata_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" />

Horizontal RecyclerView Like Paytm

I have to implement a custom horizontal RecyclerView having a header (title) at top and a section (See All) right side at the end of the RecyclerView.
I created a RecyclerView with a header and footer but I want to have a right sided section (See All) of which onclick event I wish to fire some event.
In Paytm App, it is implemented
I wish to get results as follows
This will Gives You Idea
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/verticalScrollRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView></RelativeLayout>
vertical_scroll_single_entry.xml
<?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="100dp"
android:weightSum="1"
android:gravity="center_vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/selectAllButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See All >>"
android:textAllCaps="false"/></LinearLayout>
Custom Adapter class For Vertical Scroller
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
private Context context;
private ArrayList arrayList;
private LayoutInflater layoutInflater;
public CustomAdapter(Context context, ArrayList arrayList) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
this.arrayList = arrayList;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.vertical_scroll_single_entry, parent, false);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
//initialise values to views inside holder at runtime
holder.recyclerView.setAdapter(new CustomAdapterTwo(context, arrayList));
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setHasFixedSize(true);
}
#Override
public int getItemCount() {
return arrayList.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
RecyclerView recyclerView;
Button selectAllButton;
public CustomViewHolder(View itemView) {
super(itemView);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
selectAllButton = (Button) itemView.findViewById(R.id.selectAllButton);
selectAllButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Select All At : " + String.valueOf(getLayoutPosition()), Toast.LENGTH_SHORT).show();
}
}}
horizontal adapter single entry file recycler_view_single_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="match_parent" android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove this button \n and put image view"
android:textAllCaps="false"/></LinearLayout>
Horizontal recycler view adapter class
public class CustomAdapterTwo extends RecyclerView.Adapter<CustomAdapterTwo.CustomViewHolder> {
private Context context;
private ArrayList arrayList;
private LayoutInflater layoutInflater;
public CustomAdapterTwo(Context context, ArrayList arrayList) {
this.context = context;
this.arrayList = arrayList;
this.layoutInflater = LayoutInflater.from(context);
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.recycler_view_single_item, parent, false);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return arrayList.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder {
public CustomViewHolder(View itemView) {
super(itemView);
}
}}
Your main activity class
public class MainActivity extends AppCompatActivity {
private RecyclerView verticalScrollRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialiseView();
}
private void initialiseView() {
verticalScrollRecyclerView = (RecyclerView) findViewById(R.id.verticalScrollRecyclerView);
ArrayList<String> stringArrayList = new ArrayList<>();
stringArrayList.add("One");
stringArrayList.add("Two");
stringArrayList.add("Three");
stringArrayList.add("Four");
stringArrayList.add("Five");
stringArrayList.add("Six");
stringArrayList.add("Seven");
stringArrayList.add("Eight");
stringArrayList.add("Nine");
stringArrayList.add("Ten");
//setting adapter and layout manager to recyclerView
verticalScrollRecyclerView.setLayoutManager(new LinearLayoutManager(this));
verticalScrollRecyclerView.setAdapter(new CustomAdapter(this, stringArrayList));
verticalScrollRecyclerView.setHasFixedSize(true);
}}
Looks Like

How to make RecyclerView to width="wrap_content"

I want to make RecyclerView like this one:
But in my case, child view doesn't set as width="wrap_content" or it RecyclerView doesn't set as width="wrap_content" and on "center"
Here is RecyclerView in activity_layout.xml:
<RelativeLayout 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.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is setting Adapter onCreate:
public void initRecyclerView(List<Invitation> invitationList) {
recyclerView.setHasFixedSize(true);
GridLayoutManager manager = new GridLayoutManager(this, 4);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(new ItemGridAdapter(getApplicationContext(), invitationList));
}
here is my Adapter:
public class ItemGridAdapter extends RecyclerView.Adapter<ItemGridAdapter.ViewHolderItem> {
private final Context context;
private final List<Invitation> list;
private final DrawableHelper drawableHelper;
public ItemGridAdapter(Context context, List<Invitation> list) {
this.context=context;
this.list=list;
this.drawableHelper = new DrawableHelper();
}
#Override
public ItemGridAdapter.ViewHolderItem onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false);
return new ItemGridAdapter.ViewHolderItem(context,view);
}
#Override
public void onBindViewHolder(final ItemGridAdapter.ViewHolderItem viewHolder, int position) {
Invitation invitation = list.get(position);
viewHolder.position=position;
Picasso.with(context)
.load(invitation.getCustomUser().getAvatar())
.transform(new CircleTransformation())
.placeholder(drawableHelper.getDrawableForName(invitation.getCustomUser().getFullName()))
.into(viewHolder.userIcon);
if (invitation.getYelpID()!=null&&invitation.getYelpID().length()>0){
viewHolder.votedIcon.setVisibility(View.VISIBLE);
}else{
viewHolder.votedIcon.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolderItem extends RecyclerView.ViewHolder {
public ImageView votedIcon;
public ImageView userIcon;
Context mContext;
int position;
public ViewHolderItem(Context mContext,View itemView) {
super(itemView);
this.mContext = mContext;
userIcon=(ImageView)itemView.findViewById(R.id.userIcon);
votedIcon = (ImageView)itemView.findViewById(R.id.votedIcon);
}
}
}
here is layout of item R.layout.item_grid for adapter :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:background="#drawable/white_circle"
android:padding="3dp"
android:id="#+id/userIcon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<ImageView
android:id="#+id/votedIcon"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/icon_voted"/>
</RelativeLayout>
Android Support Library as of 23.2 supports this WRAP_CONTENT in RecyclerView by default.

Android access Listview inside RecyclerView

I want to make a ListView inside a RecyclerView that should display some data in a list form. I currently have a basic Adapter and RecyclerView with no ListView inside of it, so can someone tell me how to modify my code to include a listview inside the RecyclerView?
Adapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.AdapterViewHolder> {
public static class AdapterViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView summary;
AdapterViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.recycler_list_title);
summary = (TextView) itemView.findViewById(R.id.recycler_list_summary);
}
}
List<Items> items;
public RVAdapter(List<Items> items){
this.items = items;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public AdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_recycler, viewGroup, false);
AdapterViewHolder pvh = new AdapterViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(AdapterViewHolder adapterViewHolder, int i) {
adapterViewHolder.title.setText(item.get(i).title);
adapterViewHolder.summary.setText(item.get(i).summary);
}
#Override
public int getItemCount() {
return item.size();
}
}
Items:
public class Items {
String title;
String summary;
public Items(String title, String summary) {
this.title = title;
this.summary = summary;
}
}
Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="0dp" />
</LinearLayout>
list_recycler:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<TextView
android:id="#+id/recycler_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColor"
android:textSize="16sp"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/recycler_list_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColorSub"
android:textSize="14sp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
How I'm creating the ListView:
private List item;
private RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
rv = (RecyclerView) ((Activity) MainActivity.context).findViewById(R.id.recycler);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
initializeData();
initializeAdapter();
}
private void initializeData(){
item = new ArrayList<>();
item.add(new Items("Title", "Summary"));
}
private void initializeAdapter() {
RVAdapter adapter = new RVAdapter(item);
rv.setAdapter(adapter);
}
}
ListView listview = (ListView) findViewById(R.id.listview);
ListView parentListView=(ListView)listview.getParent();
you can get parent listView by this way

Categories

Resources