如何在我的适配器上添加EventListener?

时间:2016-05-03 13:54:02

标签: android

我有一个拥有RecycleView的Android Activity。我想添加一个事件监听器,所以我构建了这段代码:

public class ResultActivity extends AppCompatActivity {
    private List<Result> lista;
    private RecyclerView recyclerView;
    private ResultsAdapter pAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.results_activity);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        //recupero la lista delle medication
        ResultDAO manager = new ResultDAO(this);

        lista=manager.getResults();

        pAdapter = new ResultsAdapter(lista, new ResultsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Result item) {

                    try{
                        //recupero i dati della lista in pagina
                        final Dialog dialog = new Dialog(ResultActivity.this);

                        //setting custom layout to dialog
                        dialog.setContentView(R.layout.result_modal);
                        dialog.setTitle("Parametri");

                    }catch(Exception e){
                        Log.e("","");
                    }

            }
        });
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(pAdapter);
    }
}

这是我的自定义适配器:

public class ResultsAdapter extends RecyclerView.Adapter<ResultsAdapter.MyViewHolder> {
    private List<Result> list;
    private final OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Result item);
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{

        public TextView startDate, endDate,examination;
        Result result;

        public MyViewHolder(View view) {
            super(view);
            startDate = (TextView) view.findViewById(R.id.startDate);
            endDate = (TextView) view.findViewById(R.id.endDate);
            examination = (TextView) view.findViewById(R.id.examination);

        }
    }

    public ResultsAdapter(List<Result> list,OnItemClickListener listener)
    {
        this.listener=listener;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.results_list_row, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Result result = list.get(position);
        holder.startDate.setText(result.getDateStart()!=null ? result.getDateStart() : "");
        holder.endDate.setText(result.getDateEnd()!=null ? result.getDateEnd() : "");
        holder.examination.setText(result.getInfo().getDisplayName());
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
}

现在,如果我尝试启动我的应用程序并尝试单击一个或多个项目,则OnClick方法无法启动。

我该如何解决?

2 个答案:

答案 0 :(得分:2)

Read this answer for better understanding

适配器类中使用以下代码: -

holher.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
            // your code
            }

        });

答案 1 :(得分:1)

查看此博文。

  

http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/

它允许您添加点击聆听者&#39;以更简单,可重用的方式。 例如:

ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
    @Override
    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
        // Do your thing
    }
});