21
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】RecyclerViewのクリックイベント

Posted at

はじめに

今年RecyclerViewのクリックイベントについて教える機会が多かったので記事にしてみました。
今回はRecyclerViewの基本的な実装については詳しく記述しませんが、最後に今回使ったコードをgitに上げておくので、よかったらそちらを参考にしてください。

#実装
RecyclerViewには、いくつかクリックイベントの実装方法がありますが、今回はAdapterからリスナーをはやしてActivityでToastを出す方法をいくつか紹介します。

##パターン1

RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.CustomViewHolder> {
    private Context context;
    private List<String> nameList;
    private View.OnClickListener listener;

    public RecyclerAdapter(Context context, List<String> nameList) {
        this.context = context;
        this.nameList = nameList;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycler, parent, false);
        return new CustomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {
        String number = numberList.get(position);
        holder.textView.setText(number);
        holder.linearLayout.setId(holder.getAdapterPosition());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view);
            }
        });
    }

    public void setOnItemClickListener(View.OnClickListener listener) {
        this.listener = listener;
    }

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

    static class CustomViewHolder extends RecyclerView.ViewHolder {
        final LinearLayout linearLayout;
        final TextView textView;

        public CustomViewHolder(View itemView) {
            super(itemView);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linear_layout);
            textView = (TextView) itemView.findViewById(R.id.text_view);
        }
    }
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acivity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        final List<String> nameList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            nameList.add(String.valueOf(i) + "さん");
        }
        RecyclerAdapter adapter = new RecyclerAdapter(this, nameList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, String.valueOf(nameList.get(view.getId())), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

このようにAdapterにsetOnItemClickListenerメソッドを準備して、それをActivityで呼んであげることによってActivityからイベントの検知ができるようになります。adapterからlistenerがはえてることは気になりますが...

##パターン2

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.CustomViewHolder> {
    private Context context;
    private List<String> nameList;
    private onItemClickListener listener;

    public RecyclerAdapter(Context context, List<String> nameList) {
        this.context = context;
        this.nameList = nameList;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycler, parent, false);
        return new CustomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CustomViewHolder holder, int position) {
        String number = nameList.get(position);
        holder.textView.setText(number);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view, nameList.get(holder.getAdapterPosition()));
            }
        });
    }

    public void setOnItemClickListener(onItemClickListener listener) {
        this.listener = listener;
    }

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

    public interface onItemClickListener {
        void onClick(View view, String name);
    }

    static class CustomViewHolder extends RecyclerView.ViewHolder {
        final LinearLayout linearLayout;
        final TextView textView;

        public CustomViewHolder(View itemView) {
            super(itemView);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linear_layout);
            textView = (TextView) itemView.findViewById(R.id.text_view);
        }
    }
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acivity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        final List<String> nameList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            nameList.add(String.valueOf(i) + "さん");
        }
        RecyclerAdapter adapter = new RecyclerAdapter(this, nameList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new RecyclerAdapter.onItemClickListener() {
            @Override
            public void onClick(View view, String name) {
                Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

このように先ほどAdapterでView.OnClickListenerと定義したところを、自分でカスタマイズしたonItemClickListenerで定義してあげることで、ClickListenerをカスタマイズしてあげることも可能です。

#最後に
今回のコードをgitに上げておきますので、コード全体を見たい方はこちらを参考にしてください。
https://github.com/Soma-Kato/SampleRecyclerView

21
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?