动态在 Android Studio 的 RecyclerView 中显示按钮。

huangapple 未分类评论44阅读模式
英文:

Display buttons dynamically in recyclerview android studio

问题

  1. 我正在开发一个学生追踪应用程序尝试在每个学生下方显示动态按钮我将在下面粘贴一个图片展示我希望它看起来的样子
英文:

I am working on a student tracker app and I am trying to display dynamic buttons that get created under each of my students. I will paste an image below of what I hope for it to look like.

动态在 Android Studio 的 RecyclerView 中显示按钮。

At the moment, I am hardcoding the buttons for the first image. I am getting the picture of the student and their name from my firbase db. Now my questions is how can I generate the checkBox for each of the student's and make sure it's directly under their picture and name like above.

In my mind, I am thinking the algorithm would be something like

  1. Loop through my Students ArrayList
  2. Generate the checkBox for each student
  3. Place them onto my recyclerView.

I will paste relevant code down below.

daily_grading.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".DailyGrading">
  8. <androidx.recyclerview.widget.RecyclerView
  9. android:id="@+id/recyclerView"
  10. android:layout_width="409dp"
  11. android:layout_height="729dp"
  12. app:layout_constraintEnd_toEndOf="parent"
  13. app:layout_constraintStart_toStartOf="parent"
  14. app:layout_constraintTop_toTopOf="parent" />
  15. <CheckBox
  16. android:id="@+id/checkBox"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="PASS"
  20. app:layout_constraintBottom_toBottomOf="parent"
  21. app:layout_constraintEnd_toEndOf="@+id/recyclerView"
  22. app:layout_constraintHorizontal_bias="0.046"
  23. app:layout_constraintStart_toStartOf="parent"
  24. app:layout_constraintTop_toTopOf="parent"
  25. app:layout_constraintVertical_bias="0.499" />
  26. <CheckBox
  27. android:id="@+id/checkBox2"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="FAIL"
  31. app:layout_constraintBottom_toBottomOf="parent"
  32. app:layout_constraintEnd_toEndOf="parent"
  33. app:layout_constraintHorizontal_bias="0.261"
  34. app:layout_constraintStart_toStartOf="parent"
  35. app:layout_constraintTop_toTopOf="parent"
  36. app:layout_constraintVertical_bias="0.499" />
  37. </androidx.constraintlayout.widget.ConstraintLayout>

student_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content">
  7. <androidx.cardview.widget.CardView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_marginTop="50dp"
  11. android:foreground="?android:attr/selectableItemBackground"
  12. app:cardElevation="2dp"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent">
  16. </androidx.cardview.widget.CardView>
  17. <RelativeLayout
  18. android:id="@+id/relativeLayout2"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:padding="5dp"
  22. tools:layout_editor_absoluteX="0dp"
  23. tools:layout_editor_absoluteY="52dp">
  24. <ImageView
  25. android:id="@+id/imageView"
  26. android:layout_width="match_parent"
  27. android:layout_height="200dp"
  28. android:layout_marginTop="50dp"
  29. android:paddingTop="20dp"
  30. android:scaleType="centerCrop" />
  31. <TextView
  32. android:id="@+id/textView"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_below="@+id/imageView"
  36. android:layout_margin="10dp"
  37. android:textSize="16sp" />
  38. </RelativeLayout>
  39. </androidx.constraintlayout.widget.ConstraintLayout>

DailyGrading.class

  1. public class DailyGrading extends AppCompatActivity{
  2. RecyclerView recyclerView;
  3. Button addStudent;
  4. private DatabaseReference myRef;
  5. public ArrayList<Students> students;
  6. private RecyclerAdapter recyclerAdapter;
  7. private Button orderStudents;
  8. private EditText mEditTextAge;
  9. private EditText mEditTextAssignment;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.daily_grading);
  14. recyclerView = findViewById(R.id.recyclerView);
  15. addStudent = findViewById(R.id.addStudentButton);
  16. mEditTextAge = findViewById(R.id.EditTextAge);
  17. mEditTextAssignment = findViewById(R.id.EditTextAssignment);
  18. recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
  19. recyclerView.setHasFixedSize(true);
  20. myRef = FirebaseDatabase.getInstance().getReference();
  21. students = new ArrayList<>();
  22. ClearAll();
  23. GetDataFromFirebase();
  24. }
  25. // fetches images and name from firebase
  26. private void GetDataFromFirebase() {
  27. Query query = myRef.child("student");
  28. query.addValueEventListener(new ValueEventListener() {
  29. @Override
  30. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  31. ClearAll();
  32. for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
  33. Students student = new Students();
  34. if (snapshot.child("url").getValue() == null) {
  35. student.setImageUrl(snapshot.child("imageUrl").getValue().toString());
  36. }
  37. else {
  38. student.setImageUrl(snapshot.child("url").getValue().toString());
  39. }
  40. student.setName(snapshot.child("name").getValue().toString());
  41. students.add(student);
  42. }
  43. recyclerAdapter = new RecyclerAdapter(getApplicationContext(), students);
  44. recyclerView.setAdapter(recyclerAdapter);
  45. recyclerAdapter.notifyDataSetChanged();
  46. }
  47. @Override
  48. public void onCancelled(@NonNull DatabaseError databaseError) {
  49. }
  50. });
  51. }
  52. // will clear recyclerAdapter
  53. private void ClearAll() {
  54. if (students != null) {
  55. students.clear();
  56. if(recyclerAdapter != null) {
  57. recyclerAdapter.notifyDataSetChanged();
  58. }
  59. }
  60. students = new ArrayList<>();
  61. }
  62. // method to generate checkboxes dynamically
  63. public void generateButtonsDynamically() {
  64. for(int i = 0; i < students.size(); i++){
  65. }
  66. }

RecyclerAdapter.class

  1. package com.example.studenttracker;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.widget.ImageView;
  6. import android.view.ViewGroup;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. import androidx.annotation.NonNull;
  10. import androidx.recyclerview.widget.RecyclerView;
  11. import com.bumptech.glide.Glide;
  12. import java.util.ArrayList;
  13. public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
  14. private OnItemClickListener mListener;
  15. public interface OnItemClickListener {
  16. void onItemClick(int position);
  17. }
  18. public void setOnItemClickListener(OnItemClickListener listener) {
  19. mListener = listener;
  20. }
  21. private static final String Tag = "RecyclerView";
  22. private Context mContext;
  23. private ArrayList<Students> studentsArrayList;
  24. public RecyclerAdapter(Context mContext, ArrayList<Students> studentsArrayList) {
  25. this.mContext = mContext;
  26. this.studentsArrayList = studentsArrayList;
  27. }
  28. @NonNull
  29. @Override
  30. public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  31. View view = LayoutInflater.from(parent.getContext())
  32. .inflate(R.layout.student_item,parent,false);
  33. return new ViewHolder(view);
  34. }
  35. @Override
  36. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  37. //TextView
  38. holder.textView.setText(studentsArrayList.get(position).getName());
  39. Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
  40. }
  41. @Override
  42. public int getItemCount() {
  43. return studentsArrayList.size();
  44. }
  45. public class ViewHolder extends RecyclerView.ViewHolder {
  46. ImageView imageView;
  47. TextView textView;
  48. public ViewHolder(@NonNull View itemView) {
  49. super(itemView);
  50. imageView = itemView.findViewById(R.id.imageView);
  51. textView = itemView.findViewById(R.id.textView);
  52. itemView.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. if (mListener != null) {
  56. int position = getAdapterPosition();
  57. }
  58. }
  59. });
  60. }
  61. }
  62. }

答案1

得分: 1

student_item.xml 而非 daily_grading.xml 中添加复选框。

英文:

Add Check Boxes in student_item.xml instead daily_grading.xml

答案2

得分: 1

请尝试以下代码:

  1. <CheckBox
  2. android:id="@+id/checkBox"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="PASS"
  6. app:layout_constraintBottom_toBottomOf="parent"
  7. app:layout_constraintEnd_toEndOf="@+id/recyclerView"
  8. app:layout_constraintHorizontal_bias="0.046"
  9. app:layout_constraintStart_toStartOf="parent"
  10. app:layout_constraintTop_toTopOf="parent"
  11. app:layout_constraintVertical_bias="0.499"
  12. android:visibility="gone" />
  13. <CheckBox
  14. android:id="@+id/checkBox2"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="FAIL"
  18. app:layout_constraintBottom_toBottomOf="parent"
  19. app:layout_constraintEnd_toEndOf="parent"
  20. app:layout_constraintHorizontal_bias="0.261"
  21. app:layout_constraintStart_toStartOf="parent"
  22. app:layout_constraintTop_toTopOf="parent"
  23. app:layout_constraintVertical_bias="0.499"
  24. android:visibility="gone" />
  1. @Override
  2. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  3. //TextView
  4. holder.textView.setText(studentsArrayList.get(position).getName());
  5. Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
  6. if (studentsArrayList.get(position).displayButtons()) { //check if you need the buttons or not
  7. holder.checkBox.setVisibility(View.VISIBLE);
  8. holder.checkBox2.setVisibility(View.VISIBLE);
  9. } else {
  10. holder.checkBox.setVisibility(View.GONE);
  11. holder.checkBox2.setVisibility(View.GONE);
  12. }
  13. }
英文:

Try this:

  1. &lt;CheckBox
  2. android:id=&quot;@+id/checkBox&quot;
  3. android:layout_width=&quot;wrap_content&quot;
  4. android:layout_height=&quot;wrap_content&quot;
  5. android:text=&quot;PASS&quot;
  6. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  7. app:layout_constraintEnd_toEndOf=&quot;@+id/recyclerView&quot;
  8. app:layout_constraintHorizontal_bias=&quot;0.046&quot;
  9. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  10. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  11. app:layout_constraintVertical_bias=&quot;0.499&quot;
  12. android:visibility=&quot;gone&quot; /&gt;
  13. &lt;CheckBox
  14. android:id=&quot;@+id/checkBox2&quot;
  15. android:layout_width=&quot;wrap_content&quot;
  16. android:layout_height=&quot;wrap_content&quot;
  17. android:text=&quot;FAIL&quot;
  18. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  19. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  20. app:layout_constraintHorizontal_bias=&quot;0.261&quot;
  21. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  22. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  23. app:layout_constraintVertical_bias=&quot;0.499&quot;
  24. android:visibility=&quot;gone&quot; /&gt;

and

  1. @Override
  2. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  3. //TextView
  4. holder.textView.setText(studentsArrayList.get(position).getName());
  5. Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
  6. if (studentsArrayList.get(position).displayButtons()) { //check if you need the buttons or not
  7. holder.checkBox.setVisibility(View.VISIBLE);
  8. holder.checkBox2.setVisibility(View.VISIBLE);
  9. } else {
  10. holder.checkBox.setVisibility(View.GONE);
  11. holder.checkBox2.setVisibility(View.GONE);
  12. }
  13. }

答案3

得分: 1

把你的 student_item.xml 代码改成这样:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content">
  7. <androidx.cardview.widget.CardView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_marginTop="50dp"
  11. android:foreground="?android:attr/selectableItemBackground"
  12. app:cardElevation="2dp"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent">
  16. </androidx.cardview.widget.CardView>
  17. <RelativeLayout
  18. android:id="@+id/relativeLayout2"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:padding="5dp"
  22. tools:layout_editor_absoluteX="0dp"
  23. tools:layout_editor_absoluteY="52dp">
  24. <ImageView
  25. android:id="@+id/imageView"
  26. android:layout_width="match_parent"
  27. android:layout_height="200dp"
  28. android:layout_marginTop="50dp"
  29. android:paddingTop="20dp"
  30. android:scaleType="centerCrop" />
  31. <TextView
  32. android:id="@+id/textView"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_below="@+id/imageView"
  36. android:layout_margin="10dp"
  37. android:textSize="16sp" />
  38. <CheckBox
  39. android:id="@+id/passc"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@id/textView"
  43. android:text="通过" />
  44. <CheckBox
  45. android:id="@+id/failc"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_below="@id/textView"
  49. android:layout_toRightOf="@+id/passc"
  50. android:text="失败" />
  51. </RelativeLayout>
  52. </androidx.constraintlayout.widget.ConstraintLayout>

然后从 RecyclerAdapter.class 控制复选框的行为。

英文:

Change the code of your student_item.xml to this :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;wrap_content&quot;&gt;
  7. &lt;androidx.cardview.widget.CardView
  8. android:layout_width=&quot;match_parent&quot;
  9. android:layout_height=&quot;wrap_content&quot;
  10. android:layout_marginTop=&quot;50dp&quot;
  11. android:foreground=&quot;?android:attr/selectableItemBackground&quot;
  12. app:cardElevation=&quot;2dp&quot;
  13. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  14. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  15. app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  16. &lt;/androidx.cardview.widget.CardView&gt;
  17. &lt;RelativeLayout
  18. android:id=&quot;@+id/relativeLayout2&quot;
  19. android:layout_width=&quot;match_parent&quot;
  20. android:layout_height=&quot;match_parent&quot;
  21. android:padding=&quot;5dp&quot;
  22. tools:layout_editor_absoluteX=&quot;0dp&quot;
  23. tools:layout_editor_absoluteY=&quot;52dp&quot;&gt;
  24. &lt;ImageView
  25. android:id=&quot;@+id/imageView&quot;
  26. android:layout_width=&quot;match_parent&quot;
  27. android:layout_height=&quot;200dp&quot;
  28. android:layout_marginTop=&quot;50dp&quot;
  29. android:paddingTop=&quot;20dp&quot;
  30. android:scaleType=&quot;centerCrop&quot; /&gt;
  31. &lt;TextView
  32. android:id=&quot;@+id/textView&quot;
  33. android:layout_width=&quot;wrap_content&quot;
  34. android:layout_height=&quot;wrap_content&quot;
  35. android:layout_below=&quot;@+id/imageView&quot;
  36. android:layout_margin=&quot;10dp&quot;
  37. android:textSize=&quot;16sp&quot; /&gt;
  38. &lt;CheckBox
  39. android:id=&quot;@+id/passc&quot;
  40. android:layout_width=&quot;wrap_content&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:layout_below=&quot;@id/textView&quot;
  43. android:text=&quot;pass&quot; /&gt;
  44. &lt;CheckBox
  45. android:id=&quot;@+id/failc&quot;
  46. android:layout_width=&quot;wrap_content&quot;
  47. android:layout_height=&quot;wrap_content&quot;
  48. android:layout_below=&quot;@id/textView&quot;
  49. android:layout_toRightOf=&quot;@+id/passc&quot;
  50. android:text=&quot;fail&quot; /&gt;
  51. &lt;/RelativeLayout&gt;
  52. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

and control the behaviour of the checkboxes from RecyclerAdapter.class

答案4

得分: 1

步骤1:在一个XML文件中添加RecyclerView。

步骤2:在另一个XML文件中,将必须放在RecyclerView内部的元素添加进去(在此处添加所需的按钮)。

步骤3:将这两个XML文件链接起来。

这样,每次动态插入后,都会添加一个按钮。

希望您能找到解决方案。

英文:

Step 1: Add the recyclerview in one xml file.

Step 2: The elements that must be inside a reecyclerview in another xml file (Here add a button that you need).

Step 3: Link these two xml files.

Thus after each insertion dynamically a button will be added.

Hope you will find the solution.

huangapple
  • 本文由 发表于 2020年7月27日 03:55:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/63105037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定