我目前使用cardview的回收站视图遇到2个问题。 第一个是复选框。 复选框似乎一直在检查/取消选中。 我知道我需要设置onCheckedChangeListener,只是不确定要放入什么。 我的第二个问题是recycleler view / cardview中的每个项目有4个字符串和1个复选框,但有时其中一个字符串太大,这搞砸了整个事情..如果字符串太长我有什么办法可以制作它会使cardview变大,所以其中一个单词会出现在第一个单词下面吗?
这是自定义对象类:
public class Workout{ private String exercise; private String percent; private String reps; private String weight; private boolean check1; public Workout(String exercise, String percent, String reps, String weight, boolean check1) { this.exercise = exercise; this.percent = percent; this.reps = reps; this.weight = weight; this.check1 = check1; } /* accessors omitted for readability */ }这是适配器:
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> { Context mContext; ArrayList<Workout> workout; public MyRecyclerAdapter(Context context, ArrayList<Workout> workout) { mContext = context; this.workout = workout; } // INITIALIZE HOLDER @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_item, null); MyViewHolder holder = new MyViewHolder(view); return holder; } //BIND DATA TO VIEWS @Override public void onBindViewHolder(MyViewHolder holder, int position) { final Workout objWorkout = workout.get(position); holder.exercise.setText(workout.get(position).getExercise()); holder.percent.setText(workout.get(position).getPercent()); holder.reps.setText(workout.get(position).getReps()); holder.weight.setText(workout.get(position).getWeight()); holder.check1.setOnCheckedChangeListener(); //LISTENER holder.setItemClickListener(new ItemClickListener() { @Override public void onItemClick(View view, int pos) { Toast.makeText(mContext, workout.get(pos).getExercise(),Toast.LENGTH_LONG).show(); } }); } @Override public int getItemCount() { return workout.size(); } }这是自定义项的xml:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_margin="10dp" card_view:cardCornerRadius="10dp" card_view:cardElevation="10dp" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="15dp" android:paddingLeft="15dp" android:paddingRight="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Squat" android:textSize="20sp" android:id="@+id/txtExercise" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="%" android:textSize="20sp" android:id="@+id/txtPercentage" android:paddingLeft="30dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/txtExercise" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Reps" android:textSize="20sp" android:id="@+id/txtReps" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Weight" android:textSize="20sp" android:id="@+id/txtWeight" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/check1" android:layout_toStartOf="@+id/check1"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/check1" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="31dp" android:layout_marginEnd="31dp" android:checked="false"/> </RelativeLayout>谢谢您的帮助!
I have 2 problems with my current recycler view with cardview. The first is with the checkbox. The checkboxs seem to check/uncheck themselves all the time. I know I need to set onCheckedChangeListener, just not sure what to put in it. My second issue is each item in the recycler view/cardview has 4 Strings and 1 checkbox, but sometimes one of the strings is too large, which screws up the whole thing.. Is there any way if a string is too long I can make it make the cardview larger so one of the words will go underneath the first word?
Here is the custom object class:
public class Workout{ private String exercise; private String percent; private String reps; private String weight; private boolean check1; public Workout(String exercise, String percent, String reps, String weight, boolean check1) { this.exercise = exercise; this.percent = percent; this.reps = reps; this.weight = weight; this.check1 = check1; } /* accessors omitted for readability */ }Here is the adapter:
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> { Context mContext; ArrayList<Workout> workout; public MyRecyclerAdapter(Context context, ArrayList<Workout> workout) { mContext = context; this.workout = workout; } // INITIALIZE HOLDER @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_item, null); MyViewHolder holder = new MyViewHolder(view); return holder; } //BIND DATA TO VIEWS @Override public void onBindViewHolder(MyViewHolder holder, int position) { final Workout objWorkout = workout.get(position); holder.exercise.setText(workout.get(position).getExercise()); holder.percent.setText(workout.get(position).getPercent()); holder.reps.setText(workout.get(position).getReps()); holder.weight.setText(workout.get(position).getWeight()); holder.check1.setOnCheckedChangeListener(); //LISTENER holder.setItemClickListener(new ItemClickListener() { @Override public void onItemClick(View view, int pos) { Toast.makeText(mContext, workout.get(pos).getExercise(),Toast.LENGTH_LONG).show(); } }); } @Override public int getItemCount() { return workout.size(); } }Here is the xml for custom item:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_margin="10dp" card_view:cardCornerRadius="10dp" card_view:cardElevation="10dp" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="15dp" android:paddingLeft="15dp" android:paddingRight="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Squat" android:textSize="20sp" android:id="@+id/txtExercise" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="%" android:textSize="20sp" android:id="@+id/txtPercentage" android:paddingLeft="30dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/txtExercise" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Reps" android:textSize="20sp" android:id="@+id/txtReps" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Weight" android:textSize="20sp" android:id="@+id/txtWeight" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/check1" android:layout_toStartOf="@+id/check1"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/check1" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="31dp" android:layout_marginEnd="31dp" android:checked="false"/> </RelativeLayout>Thanks for the help!
更多推荐
发布评论