java - Android RecyclerView 点击监听器不工作

标签 java android

我一直在试图找出我的 RecyclerView 中哪里出了错误。点击不起作用。此代码是为我的更新做的准备,我将其添加到我的应用程序中,因此不要询问构造函数和参数。你能帮我一下吗,这是我的代码:

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);
    }
}

第二部分:

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);

    }
}

最佳答案

只需将 itemView 添加到 viewholder 并在 onBindViewHolder 内设置 itemView 上的点击监听器,如下所示

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);

    }
}
}

关于java - Android RecyclerView 点击监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36267750/

相关文章:

java - Android if else 声明关于布局的 Activity 调用

java - 深入了解Collections的removeAll方法

Java Reflection API - 获取 String[] 字段的值

java - 将 HtmlUnit 导入到 Android 项目

java - 使用 Java 在 XMPP 服务器中创建新用户

java - SNMP:打印页数的通用 OID

android - 如何在 Android 中制作水平 ListView?

Android Material Design View State 变化与 Elevation

android - RxJava如何实现死循环?

android - 检查来自外部类的android连接/获取系统服务