Below recycler view code is working fine and I can Toast message what recycler view position was selected in RecycleAdapter class. But I want to know how can I get what recycler position selected in HomeFragment class?. I tried to get position by setting onclick listener to Imageview and Textview but gives error as reference to Null. Basically I want to use get(getAbsoluteAdapterPosition() in HomeFragment class to know what item selected and navigate to another Fragment. get(getAbsoluteAdapterPosition() is working as expected inside RecycleAdapter.class
binding.recyclerview.setOnClickListener is not working in HomeFragment.class. no error but no when I click on recycler views this is not shooted.
txt.setOnClickListener is giving error as null refernce which makes sense. because this is inflated in Adapter class but trying to refer in HomeFragement class. So how can I get what selected in homefragment.class. Thanks in advance
HomeFragment.class '''
public class HomeFragment extends Fragment {
HomeFragmentBinding binding;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.id.home_fragment, container, false);
return v;
}
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
%% some code here %%
recycleAdapter = new RecycleAdapter(list);
binding.recyclerview.setAdapter(recycleAdapter);
TextView txt = (TextView) binding.getRoot().findViewById(R.id.recycler_text);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Clicked on Image", Toast.LENGTH_SHORT).show();
}
});
binding.recyclerview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "RecyclerView clicked", Toast.LENGTH_SHORT).show();
Log.d("RecyclerViewclicked", " --- ");
}
});
'''
RecycleAdapter.class
'''
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder>{
RecyclerRowItemsBinding binding;
List<IconsModel> iconsList;
public RecycleAdapter(List<IconsModel> iconslist) {
this.iconsList = iconslist;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
View v = inflator.inflate(R.layout.recycler_row_items, parent, false);
MyViewHolder myviewholder = new MyViewHolder(v);
return myviewholder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
IconsModel iconModel = iconsList.get(position);
holder.imageView.setImageResource(iconModel.getImageid());
holder.recyclertext.setText(iconModel.getIconName());
}
@Override
public int getItemCount() {
//return 20;
return iconsList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView recyclertext;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.recycler_image);
recyclertext = itemView.findViewById(R.id.recycler_text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IconsModel iconModel = iconsList.get(getAbsoluteAdapterPosition());
Toast.makeText(***itemView.getContext(), "Clicked on " iconModel.getIconName()***, Toast.LENGTH_SHORT).show();
}
});
}
}
;
} '''
home_fragment.xml
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
</androidx.constraintlayout.widget.ConstraintLayout>
''' recycler_row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
android:layout_margin="5dp"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardBackgroundColor="@color/home_bg_cardview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:backgroundTint="@color/home_bg_cardview"
android:weightSum="2">
<ImageView
android:id="@ id/recycler_image"
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="0.5"
app:srcCompat="?android:attr/actionModeWebSearchDrawable"
tools:ignore="ContentDescription" />
<TextView
android:id="@ id/recycler_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="29dp"
android:layout_weight="1.5"
android:gravity="center"
android:text="Browse Items"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
CodePudding user response:
If you want to get the click event of recyclerview item in HomeFragment ,
Interface It's a good choice
//RecycleAdapter
private RecyclerViewListener recyclerViewListener
public void setRecyclerViewListener(RecyclerViewListener recyclerViewListener){
this.RecyclerViewListener recyclerViewListener ;
}
public Interface RecyclerViewListener{
void onClick(View view);
}
class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView recyclertext;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
recyclertext = itemView.findViewById(R.id.recycler_text);
recyclertext .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(recyclerViewListener !=null)recyclerViewListener.onClick(v)
}
});
}
//HomeFragment
recycleAdapter.setRecyclerViewListener(new RecyclerViewListener{
@Override
public void onClick(View v) {
//todo
}
})
CodePudding user response:
Create Interface Class in your Project
public interface ItemClick {
public void onClick(IconsModel IconsModel,int pos);
}
Interface class call your recyclerAdapter
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
Context context;
List<IconsModel> iconsList;
ItemClick click;
public TokensListAdapter(Context context, List<IconsModel> iconsList, ItemClick click) {
this.context = context;
this.tokensModelList = tokensModelList;
this.click = click;
}
@NonNull
@Override
public TokensListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.R.layout.recycler_row_items,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TokensListAdapter.ViewHolder holder, int position) {
IconsModel iconModel = iconsList.get(position);
holder.imageView.setImageResource(iconModel.getImageid());
holder.recyclertext.setText(iconModel.getIconName());
holder.itemView.setOnClickListener(v -> {
click.onClick(iconModel,position);
});
}
@Override
public int getItemCount() {
return iconsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView recyclertext;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.recycler_image);
recyclertext = itemView.findViewById(R.id.recycler_text);
}
}
}
Interface Class implement Your Fragment
public class HomeFragment extends Fragment implements ItemClick {
//adapter assign
recycleAdapter = new RecycleAdapter(getContext,list,this);
override method import your fragment
@Override
public void onClick(ItemCLick click, int pos) {
Toast.makeText(getContext(), "Clicked" pos, Toast.LENGTH_SHORT).show();
}