简体   繁体   中英

Android RecyclerView click listener not working

I have been trying to figure out where have I made a mistake in my RecyclerView. The click is not working. This code is a preparation for my update that I will add to my app so don't ask about the constructor and the parameters. Can you help me out, here is my code:

public class PlanRecyclerAdapter extends RecyclerView.Adapter<PlanRecyclerAdapter.PlanViewHolder> {

    private ClickListener clickListener;
    private List<PlanRecycler> planList;
    private boolean isDone[];
    private int count = 0;
    private List<String> current_workout = new ArrayList<>();
    private String Plan_Name;
    private Context context;

    private Dbhelper dbhelper;
    SQLController dbcon;
    private SQLiteDatabase database;
    public String rest = "";


    public PlanRecyclerAdapter(List<PlanRecycler> planList, boolean isDone[], int count, String Plan_Name, Context context, Dbhelper dbhelper, SQLController dbcon, SQLiteDatabase database) {
        this.planList = new ArrayList<>();
        this.planList.addAll(planList);
        this.isDone = isDone;
        this.count = count;
        this.Plan_Name = Plan_Name;
        this.context = context;
        this.dbhelper = dbhelper;
        this.dbcon = dbcon;
        this.database = database;
    }

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

    @Override
    public void onBindViewHolder(PlanViewHolder holder, int position) {

        PlanRecycler addWorkout = planList.get(position);
        holder.Day_Holder.setText("Day " + (position + 1));
        holder.Workout_Name.setText(" - " + addWorkout.getPlan_Workout_Title());
        rest = addWorkout.getPlan_Workout_Title();

        current_workout.add(addWorkout.getPlan_Workout_Title());

    }

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

    public class PlanViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        protected TextView Workout_Name;
        protected TextView Day_Holder;

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

            itemView.setOnClickListener(this);

            Typeface Roboto_Medium = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Medium.ttf");
            Typeface Roboto_Regular = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Regular.ttf");

            Day_Holder = (TextView) itemView.findViewById(R.id.plan_day_holder_id);
            Day_Holder.setTypeface(Roboto_Regular);

            Workout_Name = (TextView) itemView.findViewById(R.id.plan_workout_title);
            Workout_Name.setTypeface(Roboto_Medium);

        }

        @Override
        public void onClick(View v) {

            if (clickListener != null) {
                clickListener.itemClick(v, getPosition());
            }
        }
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }


    public interface ClickListener {
        void itemClick(View view, int position);
    }
}

The second part:

public class PlanAdapter extends AppCompatActivity implements PlanRecyclerAdapter.ClickListener{
 .......

    @Override
    public void itemClick(View view, int position) {

        Toast.makeText(this,"radi",Toast.LENGTH_SHORT).show();


        Bundle ExerciseBundle = new Bundle();
        ExerciseBundle.putString("plan_current_plan", Plan_Name);
        ExerciseBundle.putString("plan_current_workout", current_workout.get(position));
        ExerciseBundle.putInt("plan_from_workout_id", position);
        Intent i = new Intent(
               getApplicationContext(),
                MenuRound.class);

        i.putExtras(ExerciseBundle);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);

    }
}

Just add the itemView to the viewholder and inside onBindViewHolder set a click listener on itemView, like this

public class PlanRecyclerAdapter extends RecyclerView.Adapter<PlanRecyclerAdapter.PlanViewHolder> {

private List<PlanRecycler> planList;
private boolean isDone[];
private int count = 0;
private List<String> current_workout = new ArrayList<>();
private String Plan_Name;
private Context context;

private Dbhelper dbhelper;
SQLController dbcon;
private SQLiteDatabase database;
public String rest = "";


public PlanRecyclerAdapter(List<PlanRecycler> planList, boolean isDone[], int count, String Plan_Name, Context context, Dbhelper dbhelper, SQLController dbcon, SQLiteDatabase database) {
    this.planList = new ArrayList<>();
    this.planList.addAll(planList);
    this.isDone = isDone;
    this.count = count;
    this.Plan_Name = Plan_Name;
    this.context = context;
    this.dbhelper = dbhelper;
    this.dbcon = dbcon;
    this.database = database;
}

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

@Override
public void onBindViewHolder(PlanViewHolder holder, int position) {

    PlanRecycler addWorkout = planList.get(position);
    holder.Day_Holder.setText("Day " + (position + 1));
    holder.Workout_Name.setText(" - " + addWorkout.getPlan_Workout_Title());
    rest = addWorkout.getPlan_Workout_Title();

    current_workout.add(addWorkout.getPlan_Workout_Title());
    holder.Item_View.setOnClickListener(new OnClickListener() {//set the click listener
        @Override
        public void onClick(View v) 
        {
            // add code here
        }
    });
}

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

public class PlanViewHolder extends RecyclerView.ViewHolder {

    protected TextView Workout_Name;
    protected TextView Day_Holder;
    protected View Item_View;//add item view to PlanViewHolder 

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

        Typeface Roboto_Medium = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Medium.ttf");
        Typeface Roboto_Regular = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Regular.ttf");
        Item_View = itemView;//set item view
        Day_Holder = (TextView) itemView.findViewById(R.id.plan_day_holder_id);
        Day_Holder.setTypeface(Roboto_Regular);

        Workout_Name = (TextView) itemView.findViewById(R.id.plan_workout_title);
        Workout_Name.setTypeface(Roboto_Medium);

    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM