Java 在recyclerview android studio中动态显示按钮
我正在开发一个学生跟踪器应用程序,我正在尝试显示在每个学生下创建的动态按钮。我会在下面粘贴一张我希望它看起来像的图片 目前,我正在为第一张图片的按钮进行硬编码。我从我的firbase db中得到了学生的照片和他们的名字。现在我的问题是如何为每个学生生成复选框,并确保它直接位于他们的图片和姓名下方,如上图所示 在我看来,我认为算法应该是Java 在recyclerview android studio中动态显示按钮,java,android,firebase,android-studio,Java,Android,Firebase,Android Studio,我正在开发一个学生跟踪器应用程序,我正在尝试显示在每个学生下创建的动态按钮。我会在下面粘贴一张我希望它看起来像的图片 目前,我正在为第一张图片的按钮进行硬编码。我从我的firbase db中得到了学生的照片和他们的名字。现在我的问题是如何为每个学生生成复选框,并确保它直接位于他们的图片和姓名下方,如上图所示 在我看来,我认为算法应该是 循环浏览我的学生列表 为每个学生生成复选框 把它们放在我的回收视图上 我将把相关代码粘贴到下面 daily_grading.xml <?xml versi
daily_grading.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DailyGrading">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/recyclerView"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FAIL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.261"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="52dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:paddingTop="20dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_margin="10dp"
android:textSize="16sp" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
DailyGrading.class
public class DailyGrading extends AppCompatActivity{
RecyclerView recyclerView;
Button addStudent;
private DatabaseReference myRef;
public ArrayList<Students> students;
private RecyclerAdapter recyclerAdapter;
private Button orderStudents;
private EditText mEditTextAge;
private EditText mEditTextAssignment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daily_grading);
recyclerView = findViewById(R.id.recyclerView);
addStudent = findViewById(R.id.addStudentButton);
mEditTextAge = findViewById(R.id.EditTextAge);
mEditTextAssignment = findViewById(R.id.EditTextAssignment);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setHasFixedSize(true);
myRef = FirebaseDatabase.getInstance().getReference();
students = new ArrayList<>();
ClearAll();
GetDataFromFirebase();
}
// fetches images and name from firebase
private void GetDataFromFirebase() {
Query query = myRef.child("student");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ClearAll();
for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
Students student = new Students();
if (snapshot.child("url").getValue() == null) {
student.setImageUrl(snapshot.child("imageUrl").getValue().toString());
}
else {
student.setImageUrl(snapshot.child("url").getValue().toString());
}
student.setName(snapshot.child("name").getValue().toString());
students.add(student);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), students);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// will clear recyclerAdapter
private void ClearAll() {
if (students != null) {
students.clear();
if(recyclerAdapter != null) {
recyclerAdapter.notifyDataSetChanged();
}
}
students = new ArrayList<>();
}
// method to generate checkboxes dynamically
public void generateButtonsDynamically() {
for(int i = 0; i < students.size(); i++){
}
}
package com.example.studenttracker;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
private static final String Tag = "RecyclerView";
private Context mContext;
private ArrayList<Students> studentsArrayList;
public RecyclerAdapter(Context mContext, ArrayList<Students> studentsArrayList) {
this.mContext = mContext;
this.studentsArrayList = studentsArrayList;
}
@NonNull
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.student_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//TextView
holder.textView.setText(studentsArrayList.get(position).getName());
Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
}
@Override
public int getItemCount() {
return studentsArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
}
}
});
}
}
}
在student_item.xml中添加复选框,而不是在daily_grading.xml中添加复选框
在student_item.xml中添加复选框,而不是在daily_grading.xml中添加复选框
试试这个:
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/recyclerView"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
android:visibility="gone" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FAIL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.261"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
android:visibility="gone" />
试试这个:
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/recyclerView"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
android:visibility="gone" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FAIL"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.261"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
android:visibility="gone" />
将student_item.xml的代码更改为:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="52dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:paddingTop="20dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_margin="10dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/passc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:text="pass" />
<CheckBox
android:id="@+id/failc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_toRightOf="@+id/passc"
android:text="fail" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
并控制RecyclerAdapter.class中复选框的行为。将student_item.xml的代码更改为:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="52dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:paddingTop="20dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_margin="10dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/passc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:text="pass" />
<CheckBox
android:id="@+id/failc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_toRightOf="@+id/passc"
android:text="fail" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
并控制RecyclerAdapter.class中复选框的行为。步骤1:在一个xml文件中添加recyclerview 步骤2:必须位于另一个xml文件中的reecyclerview中的元素(此处添加所需的按钮) 步骤3:链接这两个xml文件 因此,每次插入后都会动态添加一个按钮
希望您能找到解决方案。步骤1:在一个xml文件中添加recyclerview 步骤2:必须位于另一个xml文件中的reecyclerview中的元素(此处添加所需的按钮) 步骤3:链接这两个xml文件 因此,每次插入后都会动态添加一个按钮
希望您能找到解决方案。您希望动态创建什么?按钮还是复选框?我会将视图永久添加到xml中,但会将其可见性设置为“消失”。然后,您可以根据您的数据库更改每个职位在onBindViewHolder中的可见性。我想动态创建复选框@TuhinSubhracan您给我看一个示例请@EinUserName请发布您的RecyclerAdapter的内容。您想动态创建什么?按钮还是复选框?我会将视图永久添加到xml中,但会将其可见性设置为“消失”。然后,您可以根据您的数据库更改每个职位在onBindViewHolder中的可见性。我想动态创建复选框@TuhinSubhracan您给我看一个示例请@EinUserName请发布您的RecyclerAdapter的内容。我应该将onBindViewHolder放在哪里。在
DailyGrading
类中?否,它必须是recyclerView适配器的一部分。我觉得它可以访问复选框。我想这也是@Tanzeem Iqbal的意思。您想要列表中多个条目的复选框,对吗?我想要从db返回的每个图像/名称的复选框。还有什么是displayButtons()。我没有访问它的权限,为什么在运行时动态创建它而不是通过xml?我应该把onBindViewHolder放在哪里。在DailyGrading
类中?否,它必须是recyclerView适配器的一部分。我觉得它可以访问复选框。我想这也是@Tanzeem Iqbal的意思。您想要列表中多个条目的复选框,对吗?我想要从db返回的每个图像/名称的复选框。还有什么是displayButtons()。我没有访问权限,为什么不通过xml在运行时动态创建它?@CodeMonkey请更改代码,运行项目并通知我。有新问题。现在,我的另一个活动页面显示了复选框。@CodeMonkey那么您可能正在为这两个页面使用相同的XML文件。请为您的recyclerview创建一个单独的XML文件,并使用其中的代码。不要忘记在recyclerview中替换XML文件View View=LayoutInflater.from(parent.getContext()).flate(R.layout.your\u new\u xml\u文件,parent,false)代码>我添加了新问题,请看@CodeMonkey。请更改代码,运行项目并让我知道。有新问题。现在,我的另一个活动页面显示了复选框。@CodeMonkey那么您可能正在为这两个页面使用相同的XML文件。请为您的recyclerview创建一个单独的XML文件,并使用其中的代码。不要忘记在recyclerview中替换XML文件View View=LayoutInflater.from(parent.getContext()).flate(R.layout.your\u new\u xml\u文件,parent,false)代码>我添加了新问题,请看