如何在构造函数中传递Recycler视图适配器?

时间:2019-02-01 10:53:40

标签: java android android-recyclerview recycler-adapter

假设我有三个这样的适配器,每个适配器中都有一个定义的接口,即ClickListener,它具有onItemClick方法。

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.searchResultViewHolder> 
{

....
public interface ClickListener {
    void onItemClick(int paramInt, View paramView);
}

}

如何将适配器传递给构造函数,以便我可以访问类内部的ClickListener()方法适配器。

示例

public class ABC{

private RecyclerView.Adapter adapter;

public ABC(RecyclerView.Adapter adapter){
this.adapter = adapter;
}

private void xyz(){

//How to achieve this
recyclerView.setOnClickListener(new adapter.ClickListener(){

@Override
        public void onItemClick(int position, View v) {

        }
    });   

}

3 个答案:

答案 0 :(得分:2)

好吧,在“活动/片段”中,您具有RecyclerView实例, 在其中,您应该可以传递到ABC回收站,以便它可以附加onClick。

但是,我建议的一种更好的设计是: 您的活动/片段将适配器OnClickListener实施为:接收点击并将其传递给ABC,然后由ABC处理。

赞:

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.searchResultViewHolder> 
{

public void setListener(ClickListener listener){
    this.listener = listener;
}
public interface ClickListener {
    void onItemClick(int paramInt, View paramView);
}

}

public class ABC {
    public void adapterClicked(){
         // do something here on adapter item clicked.
    }
}

public class Activity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout....);

        SearchResultAdapter adapter = new ........();
        ABC abc = new ABC();

        adapter.setOnClickListener(() -> {
            abc.adapterClicked();
        }); // java 8
    }
}

答案 1 :(得分:0)

我尝试了以下方法创建接口RecyclerOnItemClickListener

import android.view.View;

public interface RecyclerOnItemClickListener {

public void onItemClick(View childView, int position);
}

在适配器类中,您可以获取RecyclerOnItemClickListener对象,该对象在调用的Activity / Fragment中实现

RecyclerOnItemClickListener mItemClickListener;

然后您可以在适配器的构造函数中初始化该变量

mItemClickListener = recyclerOnItemClickListener;

活动/片段实现RecyclerOnItemClickListener接口,因此将实现onItemClick方法。您可以在此处编写下一个活动中用于传递数据的代码

@Override
public void onItemClick(View childView, int position)
{
    Intent intent = new Intent(getActivity(), NextActivity.class);
    intent.putExtra("ITEM", mModels.get(position));
    startActivity(intent);
}

在适配器类中,您将需要初始化ViewHolder类,如下所示:

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView content;
TextView proba;
ImageView imageView;


public ViewHolder(View itemView) {
    super(itemView);

    content = (TextView) itemView.findViewById(R.id.textView);
    proba = (TextView) itemView.findViewById(R.id.textView2);
    imageView = (ImageView) itemView.findViewById(R.id.imageView);


}

    @Override
    public void onClick(View v)
    {
        if (mItemClickListener != null)
        {
            mItemClickListener.onItemClick(v, getPosition());
        }
    }
}

答案 2 :(得分:0)

根据您的程序,我了解到您希望ClickListener在RecyclerView上。 这是我的适配器。

我在视图持有者类中创建的

ClickListener。只需使用implements View.OnClickListener来实现您的类,然后通过getAdapterPosition()

吸引用户点击
class buses_list_adaptor extends RecyclerView.Adapter<buses_list_adaptor.viewholder> {
List <Detail_buses> list;

public buses_list_adaptor(List <Detail_buses> list) {
    this.list = list;
}

@Override
public buses_list_adaptor.viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View view = layoutInflater.inflate(R.layout.list_buses, parent, false);

    return new viewholder(view, list);
}

@Override
public void onBindViewHolder(buses_list_adaptor.viewholder holder, int position) {
    holder.bus_no.setText(list.get(position).getBus_no());
    holder.start_bustop.setText(list.get(position).getFrom());
    holder.stop_bustop.setText(list.get(position).getTo());


}

@Override
public int getItemCount() {
    return list.size();
}

class viewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView bus_no, start_bustop, stop_bustop;
    List <Detail_buses> list;

    public viewholder(View itemView, List <Detail_buses> list) {
        super(itemView);
        this.list = list;
        bus_no = itemView.findViewById(R.id.buses_bus_no);
        start_bustop = itemView.findViewById(R.id.buses_from_stop);
        stop_bustop = itemView.findViewById(R.id.buses_to_stop);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        Fragment fragment= new Bus_list_stp();
        Bundle bundle= new Bundle();
        bundle.putInt("position",getAdapterPosition());
        dashboardActivity.dashboardActivityOBJ.changeFragment(fragment,bundle);


    }
}

}

我希望这对您有用