How to implement 2 or more recyIclerview in an activity ?
I am trying to implement 2 or more recyclerview in my activity
in this code line I am declaring the first adapter
private RecyclerView.Adapter mAdapter;
how to declare my second adapter?
by the way while running my app with the below code , the recyclerview is not scrolling and displaying correcly
mainactivity
private RecyclerView mRecyclerView;
private RecyclerView mRecyclerView1;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private static String LOG_TAG = "RecyclerViewActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
//first recycler
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
// LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mLayoutManager = new
LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
RecyclerView.ItemDecoration itemDecoration =
new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
//second recycler
mRecyclerView1 = (RecyclerView) findViewById(R.id.my_recycler_view1);
mRecyclerView1.setHasFixedSize(true);
LinearLayoutManager mLayoutManager1 = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView1.setLayoutManager(mLayoutManager1);
// mAdapter1 = new MyAdapter1(getDataSet1());
mRecyclerView1.setAdapter(mAdapter1);
myadapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<String> mDataset;
public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
public TextView txtHeader;
public TextView txtFooter;
public ImageViewHolder(View itemView) {
super (itemView);
txtHeader = (TextView) itemView.findViewById(R.id.firstLine);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine);
}
}
public void add(int position, String item) {
mDataset.add(position, item);
notifyItemInserted(position);
}
public void remove(String item) {
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<String> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
}
private static final int TYPE_IMAGE = 1;
private static final int TYPE_GROUP = 2;
#Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {
ImageViewHolder viewHolder = (ImageViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
viewHolder.txtFooter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
remove(name);
}
});
// viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.size();
}
}
my adpater1
public class MyAdapter1 extends RecyclerView.Adapter {
private ArrayList mDataset;
public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
public TextView txtHeader;
public TextView txtFooter;
public ImageViewHolder(View itemView) {
super (itemView);
txtHeader = (TextView) itemView.findViewById(R.id.firstLine);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine);
}
}
public void add(int position, String item) {
mDataset.add(position, item);
notifyItemInserted(position);
}
public void remove(String item) {
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter1(ArrayList<String> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout1, parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
}
private static final int TYPE_IMAGE = 1;
private static final int TYPE_GROUP = 2;
#Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {
ImageViewHolder viewHolder = (ImageViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
viewHolder.txtFooter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
remove(name);
}
});
// viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.size();
}
mainactivity layout
<TextView
android:id="#+id/textView"
android:layout_width="150dp"
android:layout_height="30dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:layout_alignParentTop="true"
android:paddingTop="5dp"
android:text="Recomanded for you"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="400dp"
android:layout_height="180dp"
android:layout_below="#id/textView"
android:scrollbars="vertical" />
<TextView
android:id="#+id/textView1"
android:layout_width="150dp"
android:layout_height="30dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:layout_alignParentTop="true"
android:paddingTop="5dp"
android:layout_below="#id/my_recycler_view"
android:text=" for you"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view1"
android:layout_width="400dp"
android:layout_height="180dp"
android:layout_below="#id/textView1"
android:scrollbars="vertical" />
</RelativeLayout>
Since code in your adapters is exactly the same, you don't need to declare two different classes. You can use just one class and create two different instances from it, passing to each instance the right set of data to render in the RecyclerView.
Hope this could help:
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView mRecyclerView1;
private RecyclerView.Adapter mAdapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
//first recycler
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(
new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
mAdapter = new MyAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
//second recycler
mRecyclerView1 = (RecyclerView) findViewById(R.id.my_recycler_view1);
mRecyclerView1.setHasFixedSize(true);
mRecyclerView1.setLayoutManager(
new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
mAdapter1 = new MyAdapter(getDataSet1());
mRecyclerView1.setAdapter(mAdapter1);
// ...
}
Related
My RecyclerAdapter
public class CollatzAdapter extends RecyclerView.Adapter<CollatzAdapter.ViewHolder> {
ArrayList<BigInteger> list = new ArrayList<BigInteger>();
List<Long> primeList;
public CollatzAdapter() {
}
public CollatzAdapter(ArrayList<BigInteger> list, ArrayList<Long> primeList) {
this.list = list;
this.primeList = primeList;
}
public CollatzAdapter(ArrayList<BigInteger> list) {
this.list = list;
}
#NonNull
#Override
public CollatzAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CollatzAdapter.ViewHolder holder, int position) {
holder.mIterationPosition.setText((position + ":"));
holder.mNumber.setText(list.get(position).toString());
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView mNumber;
public TextView mIterationPosition;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mNumber = itemView.findViewById(R.id.iteration_num);
mIterationPosition = itemView.findViewById(R.id.iteration_position);
//mPrime = itemView.findViewById(R.id.iteration_prime);
}
}
public void setList(ArrayList<BigInteger> list) {
this.list = list;
}
}
Where I Implement
iterationFragment = inflater.inflate(R.layout.fragment_iteration, container, false);
collatzViewModel = new ViewModelProvider(getActivity()).get(CollatzViewModel.class);
mRecyclerView = (RecyclerView) iterationFragment.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(false);
mLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new CollatzAdapter();
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
collatzViewModel.getCollatz().observe(getViewLifecycleOwner(),(list)->{
mAdapter.setList(list);
mAdapter.notifyDataSetChanged();
});
When RecyclerView contains large BigIntegers, say 300 characters for one integer and a list 100,000 of them, it lags when I use the scrollbar. Is there a way to prevent it from lagging when I scroll? It lags so much that it crashes the app. I don't understand why this happens since the RecyclerView should only load a couple of BigIntegers.
Repository: https://github.com/ErickSorto/Collatz-Calculator
i'm searching function that programmatically click method. so i found some method. 'performClick()'
like that :
recyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();
but it dosen't work in my case. I can't find solution. How do i use the performClick in Activity???
my Adapter - ViewHolder:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView virtNo;
private TextView score01;
private TextView score02;
private TextView score03;
private TextView totalScore;
private LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
this.virtNo = (TextView) itemView.findViewById(R.id.tv_virtNo);
this.score01 = (TextView) itemView.findViewById(R.id.tv_score01);
this.score02 = (TextView) itemView.findViewById(R.id.tv_score02);
this.score03 = (TextView) itemView.findViewById(R.id.tv_score03);
this.totalScore = (TextView) itemView.findViewById(R.id.tv_totalScore);
this.linearLayout = (LinearLayout) itemView.findViewById(R.id.ll_item_score);
virtNo.setOnClickListener(this);
score01.setOnClickListener(this);
score02.setOnClickListener(this);
score03.setOnClickListener(this);
score10.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onClickListener.onClick(v, getAdapterPosition(), items.get(getAdapterPosition()));
}
}
public void setOnClickListener(OnClickListener<Score> onClickListener) {
this.onClickListener = onClickListener;
}
public interface OnClickListener<T> {
void onClick(View v, int position, T item);
}
my Adapter - onBindViewHolder
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Score item = items.get(position);
holder.virtNo.setText(item.virtNo);
holder.score01.setText(item.score01);
holder.score02.setText(item.score02);
holder.score03.setText(item.score03);
holder.totalScore.setText(itemSum(item));
}
}
I have tried to keep things easy to understand here, it is a complete example for listening to click event on individual items in RecyclerView, there are other ways to do it as well. This code works, You may modify it as it fits you, in case you have any question write them in comments. code is also available at GitHub
public class RecyclerViewOneActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view_one);
initializeUI();
}
private void initializeUI() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.RecyclerViewOneActivity_RecyclerView);
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
ArrayList<String> strings = new ArrayList<>();
strings.add("first");
strings.add("second");
MyAdapter adapter = new MyAdapter(getApplicationContext(), strings);
recyclerView.setAdapter(adapter);
}
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private ArrayList<String> strings;
private LayoutInflater layoutInflater;
public MyAdapter(Context context, ArrayList<String> strings) {
this.context = context;
this.strings = strings;
layoutInflater = LayoutInflater.from(this.context);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.single_item_recycler_view_one, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final String text = this.strings.get(position);
holder.textView.setText("" + text);
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, ""+text, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return strings.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
private TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.single_item_recycler_view_one_linear_layout);
textView = (TextView) itemView.findViewById(R.id.single_item_recycler_view_one_textView);
}
}
}
}
single_item_recycler_view_one.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="wrap_content"
android:layout_margin="8dp"
android:id="#+id/single_item_recycler_view_one_linear_layout"
android:orientation="vertical">
<TextView
android:id="#+id/single_item_recycler_view_one_textView"
android:layout_width="match_parent"
android:textColor="#000"
android:layout_height="wrap_content"
android:text="Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
I here by attached my code.
I have a fragment with in that have recycler list item list.
If i click the recycler item on fragment i can't move to new activity.
And i want to get the recycler item value which item i have clicked.
So kindly please suggest me to how to intent fragment to activity once clicked recycler list item with in fragment and how to get the recycler item value?
ContactOneFragment
public class ContactOneFragment extends Fragment {
protected RecyclerView.LayoutManager mLayoutManager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_contactone, null, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
DetailModel detail[] = {
new DetailModel("Meenakshi.A","9566415797"),
new DetailModel("Pandimurugeswari.S","8940144873"),
new DetailModel("Selvam. K","7708410451"),
new DetailModel("Raja Lakshmi.V","9952754812"),
new DetailModel("Narmatha Devi.S","9894762259"),
new DetailModel("Murugan.M","9566368816"),
new DetailModel("SenthilMurugan.A","7667453595"),
};
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
DetailAdapter mAdapter = new DetailAdapter(detail);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
return view;
}
}
DetailAdapter
public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.ViewHolder> {
private DetailModel[] itemsData;
private final ArrayList<Integer> selected = new ArrayList<>();
OnItemClickListener mItemClickListener;
Context context;
public DetailAdapter(DetailModel[] itemsData) {
this.itemsData = itemsData;
}
// Create new views (invoked by the layout manager)
#Override
public DetailAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.contactnamenumber, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.namelist.setText(itemsData[position].getName());
viewHolder.numberlist.setText(itemsData[position].getNumber());
// viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
// viewHolder.imgViewIcon.setTag(new Integer(position));
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView namelist;
public TextView numberlist;
String name;
String number;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
namelist = (TextView) itemLayoutView.findViewById(R.id.name);
numberlist = (TextView) itemLayoutView.findViewById(R.id.contactnumber);
//imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
name=namelist.getText().toString();
number=numberlist.getText().toString();
itemLayoutView.setOnClickListener(this);
}
#Override public void onClick(View view) {
}
}
public interface OnItemClickListener {
public void onItemClick(View view , int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
// Return the size of your itemsData (invoked by the layout manager)
#Override
public int getItemCount() {
return itemsData.length;
}
} //Update - Code formatting
Modify your constructor DetailAdapter as following:
public DetailAdapter(Context context, DetailModel[] itemsData) {
this.context = context;
this.itemsData = itemsData;
}
On your onClick() method in adapter add following code:
#Override
public void onClick(View view) {
int pos = getAdapterPosition();
String tempName = itemsData[pos].getName();
String tempNumber = itemsData[pos].getNumber();
Intent intent = new Intent(context, YourActivity.class);
startActivity(intent);
}
Add this on your adapter,
View.OnClickListener mOnClickListener = new OrderClickListener();
then
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contactnamenumber, parent, false);
view.setOnClickListener(mOnClickListener);
return new ViewHolder(view);
}
now in adapter,
private class OrderClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
mOrderSelect.onOrderSelect(rvOrderHistory.getChildLayoutPosition(v));
}
}
From above steps you can get the position which item is clicked. Here you can go to next activity.
I want to create nested RecyclerViews and the problem is that only parent is showing.
Parent Adapter
public class SectionsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context mContext;
List<Section> mSections;
public SectionsAdapter(Context context, List<Section> sections) {
mContext = context;
mSections = sections;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_section, parent, false);
return new SectionViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int pos) {
Section section = mSections.get(pos);
if (holder instanceof SectionViewHolder) {
SectionViewHolder sectionVH = (SectionViewHolder) holder;
sectionVH.mTitle.setText(section.getTitle());
LinearLayoutManager manager = new LinearLayoutManager(mContext);
sectionVH.mPreferences.setLayoutManager(manager);
PreferencesAdapter preferencesAdapter = new PreferencesAdapter(mContext, section.getPreferences());
sectionVH.mPreferences.setAdapter(preferencesAdapter);
sectionVH.mPreferences.getAdapter().notifyDataSetChanged();
}
}
#Override
public int getItemViewType(int pos) {
return 0;
}
#Override
public int getItemCount() {
return mSections.size();
}
}
Child Adapter
public class PreferencesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int VIEW_BOOLEAN = 0;
public static final int VIEW_INT = 1;
Context mContext;
List<Preference> mPreferences;
public PreferencesAdapter(Context context, List<Preference> preferences) {
mContext = context;
mPreferences = preferences;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
default:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_preference_boolean, parent, false);
return new BooleanPreferenceViewHolder(view);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int pos) {
Preference preference = mPreferences.get(pos);
if (holder instanceof BooleanPreferenceViewHolder) {
BooleanPreferenceViewHolder booleanPreferenceVH = (BooleanPreferenceViewHolder) holder;
booleanPreferenceVH.mPreferenceTitle.setText(preference.getTitle());
booleanPreferenceVH.mPreferenceExplanation.setText(preference.getExplanation());
booleanPreferenceVH.mBooleanSwitch.setChecked((Boolean) preference.getValue());
}
}
#Override
public int getItemViewType(int pos) {
return VIEW_BOOLEAN;
}
#Override
public int getItemCount() {
return mPreferences.size();
}
}
Parent View Holder
public class SectionViewHolder extends RecyclerView.ViewHolder {
public TextView mTitle;
public RecyclerView mPreferences;
public SectionViewHolder(View v) {
super(v);
mTitle = (TextView) v.findViewById(R.id.sectionTitle);
mPreferences = (RecyclerView) v.findViewById(R.id.preferences);
}
}
Child View Holder
public class BooleanPreferenceViewHolder extends RecyclerView.ViewHolder {
public TextView mPreferenceTitle;
public TextView mPreferenceExplanation;
public Switch mBooleanSwitch;
public BooleanPreferenceViewHolder(View v) {
super(v);
mPreferenceTitle = (TextView) v.findViewById(R.id.preferenceTitle);
mBooleanSwitch = (Switch) v.findViewById(R.id.booleanSwitch);
mPreferenceExplanation = (TextView) v.findViewById(R.id.preferenceExplanation);
}
}
So I noticed that is showing only parent title and its recyclerview is empty. Also child onBindViewHolder method is not calling.
You can achieve what you want with only one RecyclerView, overriding the getItemViewType method properly. You can see an example here.
Below is an example using your Section, Preference and BooleanPreferenceViewHolder classes with the library SectionedRecyclerViewAdapter.
First create a section class:
class MySection extends StatelessSection {
String title;
List<Preference> mPreferences;
public MySection(String title, List<Preference> preferences) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_footer, R.layout.section_item);
this.title = title;
this.mPreferences = preferences;
}
#Override
public int getContentItemsTotal() {
return mPreferences.size(); // number of items of this section
}
#Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new BooleanPreferenceViewHolder(view);
}
#Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
BooleanPreferenceViewHolder itemHolder = (BooleanPreferenceViewHolder) holder;
// bind your view here
itemHolder.mPreferenceExplanation.setText(mPreference.get(position).getExplanation());
}
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(title);
}
}
Then you set up the RecyclerView with your Sections:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
MySection section1 = new MySection(mSections.get(0).getTitle(), mSections.get(0).getPreferences());
MySection section2 = new MySection(mSections.get(1).getTitle(), mSections.get(1).getPreferences());
// Add your Sections
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
I have an array of 600 items. I list down all the array items in a RecyclerView and scrolling well.But when I try to access onClick listener it is not getting. I try to Implement View.OnClickListener and Override onClick.But its not working.
I want to Toast item in the list item when user click one of the row item in the view
Adapter code is
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private String mItem;
private TextView mTextView;
public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mTextView = (TextView) view.findViewById(R.id.item_title);
}
public void setItem(String item) {
mItem = item;
mTextView.setText(item);
}
#Override
public void onClick(View view) {
Log.d("TAG", "onClick " + getPosition() + " " + mItem);
}
}
private String[] mDataset;
public MyAdapter(String[] dataset) {
mDataset = dataset;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setItem(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
Can any one please help me
Thanks in advance :)
I have create a sample project using your code onClickListener working fine.Like when i click on item shows me the clicked item position in Log.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private MyAdapter mAdapter;
private RecyclerView mRecyclerView;
private String[] dataSet = {"Waleed", "Sarwar", "Yousuf"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(dataSet);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
activity_main.xml
<?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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
public MyAdapter(String[] dataset) {
mDataset = dataset;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setItem(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private String mItem;
private TextView mTextView;
public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mTextView = (TextView) view.findViewById(R.id.item_title);
}
public void setItem(String item) {
mItem = item;
mTextView.setText(item);
}
#Override
public void onClick(View view) {
Log.d("TAG", "onClick " + getPosition() + " " + mItem);
}
}
}
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="56dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
if you are still facing the problem download the sample project from link below.
https://www.dropbox.com/s/xf3y8oxok0zlgxk/RecyclerViewOnClick.zip?dl=0
pls see jcobs ans it may help you
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
// do whatever
}
})
);
implement your code in onitemclick.
happy coding :)
You can handle it by setting on click listener to your parent view. Recyclerview don't provide you default item click listner same as listview.
So create object of your parent layout and set it click listner.
Your holder initialization.
class UserListHolder extends RecyclerView.ViewHolder {
RelativeLayout layoutMain;
UserListHolder(View itemView) {
layoutMain = (RelativeLayout)itemView.findViewById(R.id.layoutSquadItem);
}
}
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder,final int position) {
((UserListHolder)viewHolder).layoutMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle click
}
});
}
You can also refer recycler view on click listner