-2

I have an Array list here,

I want each of them when clicked, they will do a specific function or intent an activity.

Currently: each item contains, ImageButton and a Textview in a list.

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;


public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {

    String mString;
    private RecyclerView mRecyclerView;
    private VerticalListAdapter mVerticalListAdapter;

    static MyBottomSheetDialogFragment newInstance(String string) {
        MyBottomSheetDialogFragment f = new MyBottomSheetDialogFragment();
        Bundle args = new Bundle();
        args.putString("string", string);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mString = getArguments().getString("string");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_sheet_modal_list, container, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        List<Model> list = new ArrayList<Model>();
        list.add(new Model(R.drawable.a1, "Call"));
        list.add(new Model(R.drawable.a2, "Chat"));
        list.add(new Model(R.drawable.a3, "Android"));
        list.add(new Model(R.drawable.a4, "Location"));
        list.add(new Model(R.drawable.a5, "Call"));
        list.add(new Model(R.drawable.a6, "Chat"));
        list.add(new Model(R.drawable.a7, "Android"));


        mVerticalListAdapter = new VerticalListAdapter(getContext(), list);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mVerticalListAdapter);
    }

I can provide additional XML , Java codes of the project that is relatively connected to this code, Please leave a comment for clarification.

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;


public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.MyViewHolder>{

    private Context mContext;
    private List<Model> mModelList;

    public VerticalListAdapter(Context context, List<Model> list) {
        this.mContext = context;
        this.mModelList = list;
    }

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

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.txtView.setText(mModelList.get(position).getName());
        holder.imgView.setImageDrawable(mContext.getResources().getDrawable(mModelList.get(position).getImgId()));
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView txtView;
        public ImageButton imgView;

        public MyViewHolder(View view) {
            super(view);
            txtView = (TextView) view.findViewById(R.id.txtView);
            imgView = (ImageButton) view.findViewById(R.id.imgView);
        }
    }
}

1 Answers1

0

What you need to do is to implement the OnClickListener to your Holder class and add id to your LinearLayout in modal_list_item.xml so you can click on the entire layout not only its views (if I understand you correctly) and eventually set the onClickListener.

When you do this, you will need to implement the onClick method. For inspiration see the code below:

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView txtView;
    public ImageButton imgView;

    private LinearLayout linearLayout;

    public MyViewHolder(View view) {
        super(view);
        txtView = (TextView) view.findViewById(R.id.txtView);
        imgView = (ImageButton) view.findViewById(R.id.imgView);

        linearLayout = (LinearLayout) view.findViewById(R.id.linear_l);
        linearLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.i("RecyclerView Item", String.valueOf(getLayoutPosition()));
        mModelList.remove(getLayoutPosition());
        notifyDataSetChanged();
    }
}
Matcoil
  • 890
  • 11
  • 23
  • http://imgur.com/a/dFSTF here is my visual example of my application. Each of it is a ImageButton and i want each of them to have their own function. I'm sorry but i can't understand your answer. –  Nov 27 '16 at 11:34