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

huangapple go评论81阅读模式
英文:

Display buttons dynamically in recyclerview android studio

问题

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

英文:

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

<?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>

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" />
    
        </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++){
        }
    }

RecyclerAdapter.class

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

答案1

得分: 1

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

英文:

Add Check Boxes in student_item.xml instead daily_grading.xml

答案2

得分: 1

请尝试以下代码:

<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" />
@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);

    if (studentsArrayList.get(position).displayButtons()) { //check if you need the buttons or not
        holder.checkBox.setVisibility(View.VISIBLE);
        holder.checkBox2.setVisibility(View.VISIBLE);
    } else {
        holder.checkBox.setVisibility(View.GONE);
        holder.checkBox2.setVisibility(View.GONE);
    }
}
英文:

Try this:

&lt;CheckBox
        android:id=&quot;@+id/checkBox&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;PASS&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;@+id/recyclerView&quot;
        app:layout_constraintHorizontal_bias=&quot;0.046&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintVertical_bias=&quot;0.499&quot;
        android:visibility=&quot;gone&quot; /&gt;

    &lt;CheckBox
        android:id=&quot;@+id/checkBox2&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;FAIL&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.261&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintVertical_bias=&quot;0.499&quot;
        android:visibility=&quot;gone&quot; /&gt;

and

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

    if (studentsArrayList.get(position).displayButtons()) { //check if you need the buttons or not
        holder.checkBox.setVisibility(View.VISIBLE);
        holder.checkBox2.setVisibility(View.VISIBLE);
    } else {
        holder.checkBox.setVisibility(View.GONE);
        holder.checkBox2.setVisibility(View.GONE);
    }
}

答案3

得分: 1

把你的 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="通过" />

        <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="失败" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

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

英文:

Change the code of your student_item.xml to this :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;androidx.cardview.widget.CardView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;50dp&quot;
android:foreground=&quot;?android:attr/selectableItemBackground&quot;
app:cardElevation=&quot;2dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;RelativeLayout
android:id=&quot;@+id/relativeLayout2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:padding=&quot;5dp&quot;
tools:layout_editor_absoluteX=&quot;0dp&quot;
tools:layout_editor_absoluteY=&quot;52dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/imageView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;200dp&quot;
android:layout_marginTop=&quot;50dp&quot;
android:paddingTop=&quot;20dp&quot;
android:scaleType=&quot;centerCrop&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/imageView&quot;
android:layout_margin=&quot;10dp&quot;
android:textSize=&quot;16sp&quot; /&gt;
&lt;CheckBox
android:id=&quot;@+id/passc&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@id/textView&quot;
android:text=&quot;pass&quot; /&gt;
&lt;CheckBox
android:id=&quot;@+id/failc&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@id/textView&quot;
android:layout_toRightOf=&quot;@+id/passc&quot;
android:text=&quot;fail&quot; /&gt;
&lt;/RelativeLayout&gt;
&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://go.coder-hub.com/63105037.html
匿名

发表评论

匿名网友

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

确定