我在 Bottom Sheet Layout 中创建了 Recyclerview,如何为此实现 Onclicklistener 方法?
I created Recyclerview inside the Bottom Sheet Layout , How can i implement Onclicklistener method for that?
我将上下文作为 'Suggestion_Adapter.Onitemclicklistenersuggestion) getContext()' 传递了一个错误,我该如何解决这个问题?我在下面提交了底部sheetclass和适配器class,谢谢
我想在底部的 Onclicklistener 方法中传递哪个上下文 sheet
suggestion_adapter.setOnitemclicklistener((**context**);
import java.util.ArrayList;
import java.util.List;
public class Bottomsheet extends BottomSheetDialogFragment implements Suggestion_Adapter.Onitemclicklistenersuggestion{
DatabaseReference databaseReference2;
private RecyclerView recyclerViewsuggestion;
private Suggestion_Adapter suggestion_adapter;
private List<Model_upload> suggestion_uploads;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.bottom_sheet_layout,container,false);
databaseReference2= FirebaseDatabase.getInstance().getReference().child("Product_Details");
recyclerViewsuggestion=view.findViewById(R.id.recyclerviewsuggestionid);
recyclerViewsuggestion.setHasFixedSize(true);
LinearLayoutManager layoutManager
= new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerViewsuggestion.setLayoutManager(layoutManager);
suggestion_uploads=new ArrayList<>();
databaseReference2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
suggestion_uploads.clear();
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
{
Model_upload mp=dataSnapshot1.getValue(Model_upload.class);
if(mp.getSize().equals("S"))
{
Model_upload md=dataSnapshot1.getValue(Model_upload.class);
// md.setMkey(dataSnapshot1.getKey());
suggestion_uploads.add(md);
}
else if(mp.getSize().equals("M"))
{
Model_upload md=dataSnapshot1.getValue(Model_upload.class);
// md.setMkey(dataSnapshot1.getKey());
suggestion_uploads.add(md);
}
else if(mp.getSize().equals("L"))
{
Model_upload md=dataSnapshot1.getValue(Model_upload.class);
// md.setMkey(dataSnapshot1.getKey());
suggestion_uploads.add(md);
}
}
suggestion_adapter=new Suggestion_Adapter(getContext(),suggestion_uploads);
recyclerViewsuggestion.setAdapter(suggestion_adapter);
suggestion_adapter.setOnitemclicklistener((Suggestion_Adapter.Onitemclicklistenersuggestion) getContext());
suggestion_adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
@Override
public void clicktoupdate(int position) {
Toast.makeText(getContext(), ""+position, Toast.LENGTH_SHORT).show();
}
}
My Adapter class -> SuggestionAdapter
package com.app.futurelistuser.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.app.futurelistuser.Model.Model_upload;
import com.app.futurelistuser.R;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Suggestion_Adapter extends RecyclerView.Adapter<Suggestion_Adapter.Suggestion_viewholder> {
private Context mcontext;
private List<Model_upload> mlist;
private Onitemclicklistenersuggestion listener;
public Suggestion_Adapter(Context mcontext, List<Model_upload> mlist) {
this.mcontext = mcontext;
this.mlist = mlist;
}
@NonNull
@Override
public Suggestion_viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mcontext).inflate(R.layout.suggesstion_card_view,parent,false);
return new Suggestion_viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull Suggestion_viewholder holder, int position) {
Model_upload mu=mlist.get(position);
holder.tvname.setText(""+mu.getCloth_name());
// holder.tvprice.setText(""+mu.getPrice());
Picasso.get()
.load(mu.getImg_link())
.centerCrop()
.fit()
.into(holder.iv);
}
@Override
public int getItemCount() {
return mlist.size();
}
public class Suggestion_viewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView iv;
private TextView tvname,tvprice;
public Suggestion_viewholder(@NonNull View itemView) {
super(itemView);
iv=itemView.findViewById(R.id.imageView1);
tvname=itemView.findViewById(R.id.nameid);
tvprice=itemView.findViewById(R.id.priceid);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(listener!=null)
{
int position=getAdapterPosition();
if(position!=RecyclerView.NO_POSITION)
{
listener.clicktoupdate(position);
}
}
}
}
public interface Onitemclicklistenersuggestion
{
void clicktoupdate(int poistion);
}
public void setOnitemclicklistener(Onitemclicklistenersuggestion mlistener)
{
listener=mlistener;
}
}
不传递上下文,而是这样做
suggestion_adapter.setOnitemclicklistener(new Suggestion_Adapter.Onitemclicklistenersuggestion(){
@Override
public void clicktoupdate(int poistion); {
//handle click event
}
});
我将上下文作为 'Suggestion_Adapter.Onitemclicklistenersuggestion) getContext()' 传递了一个错误,我该如何解决这个问题?我在下面提交了底部sheetclass和适配器class,谢谢 我想在底部的 Onclicklistener 方法中传递哪个上下文 sheet
suggestion_adapter.setOnitemclicklistener((**context**);
import java.util.ArrayList;
import java.util.List;
public class Bottomsheet extends BottomSheetDialogFragment implements Suggestion_Adapter.Onitemclicklistenersuggestion{
DatabaseReference databaseReference2;
private RecyclerView recyclerViewsuggestion;
private Suggestion_Adapter suggestion_adapter;
private List<Model_upload> suggestion_uploads;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.bottom_sheet_layout,container,false);
databaseReference2= FirebaseDatabase.getInstance().getReference().child("Product_Details");
recyclerViewsuggestion=view.findViewById(R.id.recyclerviewsuggestionid);
recyclerViewsuggestion.setHasFixedSize(true);
LinearLayoutManager layoutManager
= new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerViewsuggestion.setLayoutManager(layoutManager);
suggestion_uploads=new ArrayList<>();
databaseReference2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
suggestion_uploads.clear();
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
{
Model_upload mp=dataSnapshot1.getValue(Model_upload.class);
if(mp.getSize().equals("S"))
{
Model_upload md=dataSnapshot1.getValue(Model_upload.class);
// md.setMkey(dataSnapshot1.getKey());
suggestion_uploads.add(md);
}
else if(mp.getSize().equals("M"))
{
Model_upload md=dataSnapshot1.getValue(Model_upload.class);
// md.setMkey(dataSnapshot1.getKey());
suggestion_uploads.add(md);
}
else if(mp.getSize().equals("L"))
{
Model_upload md=dataSnapshot1.getValue(Model_upload.class);
// md.setMkey(dataSnapshot1.getKey());
suggestion_uploads.add(md);
}
}
suggestion_adapter=new Suggestion_Adapter(getContext(),suggestion_uploads);
recyclerViewsuggestion.setAdapter(suggestion_adapter);
suggestion_adapter.setOnitemclicklistener((Suggestion_Adapter.Onitemclicklistenersuggestion) getContext());
suggestion_adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
@Override
public void clicktoupdate(int position) {
Toast.makeText(getContext(), ""+position, Toast.LENGTH_SHORT).show();
}
}
My Adapter class -> SuggestionAdapter
package com.app.futurelistuser.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.app.futurelistuser.Model.Model_upload;
import com.app.futurelistuser.R;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Suggestion_Adapter extends RecyclerView.Adapter<Suggestion_Adapter.Suggestion_viewholder> {
private Context mcontext;
private List<Model_upload> mlist;
private Onitemclicklistenersuggestion listener;
public Suggestion_Adapter(Context mcontext, List<Model_upload> mlist) {
this.mcontext = mcontext;
this.mlist = mlist;
}
@NonNull
@Override
public Suggestion_viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mcontext).inflate(R.layout.suggesstion_card_view,parent,false);
return new Suggestion_viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull Suggestion_viewholder holder, int position) {
Model_upload mu=mlist.get(position);
holder.tvname.setText(""+mu.getCloth_name());
// holder.tvprice.setText(""+mu.getPrice());
Picasso.get()
.load(mu.getImg_link())
.centerCrop()
.fit()
.into(holder.iv);
}
@Override
public int getItemCount() {
return mlist.size();
}
public class Suggestion_viewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView iv;
private TextView tvname,tvprice;
public Suggestion_viewholder(@NonNull View itemView) {
super(itemView);
iv=itemView.findViewById(R.id.imageView1);
tvname=itemView.findViewById(R.id.nameid);
tvprice=itemView.findViewById(R.id.priceid);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(listener!=null)
{
int position=getAdapterPosition();
if(position!=RecyclerView.NO_POSITION)
{
listener.clicktoupdate(position);
}
}
}
}
public interface Onitemclicklistenersuggestion
{
void clicktoupdate(int poistion);
}
public void setOnitemclicklistener(Onitemclicklistenersuggestion mlistener)
{
listener=mlistener;
}
}
不传递上下文,而是这样做
suggestion_adapter.setOnitemclicklistener(new Suggestion_Adapter.Onitemclicklistenersuggestion(){
@Override
public void clicktoupdate(int poistion); {
//handle click event
}
});