'Toast inside onclick recyclerview
I'm making an app in which an activity shows a recyclerview. I'm trying to get a toast when the item in the list is clicked. I've seen the various solutions on StackOverflow and they don't help.
BasicsAdapter.java
public class BasicsAdapter extends RecyclerView.Adapter<BasicsAdapter.MyViewHolder> {
private List<Basic> basicsList;
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView title;
public MyViewHolder(View view){
super(view);
title = (TextView)view.findViewById(R.id.title);
}
@Override
public void onClick(View v) {
}
}
public BasicsAdapter(List<Basic> basicsList){
this.basicsList = basicsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.basic_list_row, parent, false);
itemView.setClickable(true);
itemView.setOnClickListener(true);
//itemView.setFocusableInTouchMode(true); //dont enable this
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Basic basic = basicsList.get(position);
holder.title.setText(basic.getTitle());
}
@Override
public int getItemCount() {
return basicsList.size();
}
}
basics_list.java
public class basics_list extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private List<Basic> basicList = new ArrayList<>();
private RecyclerView recyclerView;
private BasicsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.basics_list_name);
setContentView(R.layout.activity_basics_list);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager); */
// set recycler view
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new BasicsAdapter(basicList);
final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareBasicsData();
// Create Navigation drawer and inflate layout
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
// Adding menu icon to Toolbar
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
supportActionBar.setDisplayHomeAsUpEnabled(true);
}
// Set behavior of Navigation drawer
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Set item in checked state
menuItem.setChecked(true);
// TODO: handle navigation
// Closing drawer on item click
mDrawerLayout.closeDrawers();
return true;
}
});
}
private void prepareBasicsData() {
Basic basic = new Basic("Introduction to Linux");
basicList.add(basic);
basic = new Basic("What is The Terminal");
basicList.add(basic);
basic = new Basic("Programmers guide");
basicList.add(basic);
mAdapter.notifyDataSetChanged();
}
}
Solution 1:[1]
Use itemView.setOnClickListener(new View.OnClickListener() {})
instead of itemView.setOnClickListener(true)
.
This way you apply an OnClickListener
directly. It would look similar to this:
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void OnClick(View view) {
Toast.makeText(view.getContext(), "Item is clicked", Toast.LENGTH_SHORT).show();
}
});
And you might put it into the constructor.
Solution 2:[2]
Just set your OnClickListener
within your ViewHolder
.
public class BasicsAdapter extends RecyclerView.Adapter {
private List<Basic> basicsList;
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView title;
public MyViewHolder(View view){
super(view);
view.setOnClickListener(new View.OnClickListener() {
Toast.makeText(view.getContext(), "Item is clicked", LENGHT.SHORT).show();
});
title = (TextView)view.findViewById(R.id.title);
}
}
public BasicsAdapter(List<Basic> basicsList){
this.basicsList = basicsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.basic_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Basic basic = basicsList.get(position);
holder.title.setText(basic.getTitle());
}
@Override
public int getItemCount() {
return basicsList.size();
}
}
Solution 3:[3]
This might help you:
public class BasicsAdapter extends RecyclerView.Adapter<BasicsAdapter.MyViewHolder> {
private List<Basic> basicsList;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
Context context;
public MyViewHolder(View view){
super(view);
title = (TextView)view.findViewById(R.id.title);
}
}
public BasicsAdapter(Context context,List<Basic> basicsList){
this.context=context;
this.basicsList = basicsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.basic_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Basic basic = basicsList.get(position);
holder.title.setText(basic.getTitle());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context,"Show your text here",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return basicsList.size();
}
}
Solution 4:[4]
This may help you, I hope. He had used an interface to listen to the click of the view in ViewHolder. https://www.youtube.com/watch?v=wCSePoE3lqA
Solution 5:[5]
Here is an example with Kotlin:
In the inner class ...ViewHolder(...)
fun myFunction(data: Data){
binding.item.setOnClickListener {
Toast.makeText( it.context,"Clicked", Toast.LENGTH_SHORT).show()
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | CodyEngel |
Solution 3 | Android Geek |
Solution 4 | Bruno |
Solution 5 | Konrad |