简体   繁体   中英

Getting the progress of multiple seekbars in recycler view

So I am displaying a recylerview with an Image and the name of a person. Below that is a seekbar, which the user can move to rate that person.

Now I want to store the ratings (=progress) of each seekbar in a list. However right now it only stores the rating of the last seekbar in the list.

So it stores only the progress of the last seekbar right now. I would like that when the user clicks the button that every current rating value of every seekbar is stored in a list. Thank you very much.

That is is my layout_listitem:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:id="@+id/parent_layoutREC"
    android:orientation="horizontal"
    android:layout_marginTop="13dp"
    android:gravity="center">

    <de.hdodenhof.circleimageview.CircleImageView
        android:paddingTop="2dp"
        android:paddingLeft="2dp"
        android:layout_width="73dp"
        android:layout_height="73dp"
        android:id="@+id/kunde_imageREC"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_marginLeft="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Canada"
        android:id="@+id/kunde_nameREC"
        android:textColor="#000"
        android:textSize="19sp"
        android:textStyle="bold"/>

</LinearLayout>



    <SeekBar
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="8dp"
        android:id="@+id/seek_Bar"
        android:max="10"
        android:progress="5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<TextView
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="0          1          2          3          4          5"
    android:textSize="20sp"/>

That is my RecyclerViewAdapter class:

public class RecyclerViewAdap extends 
RecyclerView.Adapter<RecyclerViewAdap.ViewHolder> {
private static final String TAG = "RecyclerViewAdap";
private ArrayList<String> mImageUrl = new ArrayList<>();
private ArrayList<String> mKundeNamen = new ArrayList<>();
public ArrayList<Integer> mBewertungen = new ArrayList<>();
private Context mContext;
public String bewertung;
private TextView progressSeekbar;

public RecyclerViewAdap(ArrayList<String> imageUrl, ArrayList<String> kundeNamen, Context context) {
    mImageUrl = imageUrl;
    mKundeNamen = kundeNamen;
    mContext = context;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    CircleImageView imageView;
    TextView kundeName;
    LinearLayout parentLayout;
    SeekBar seekBar1;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.kunde_imageREC);
        kundeName = itemView.findViewById(R.id.kunde_nameREC);
        parentLayout = itemView.findViewById(R.id.parent_layoutREC);

        seekBar1 = itemView.findViewById(R.id.seek_Bar);
        progressSeekbar = itemView.findViewById(R.id.progress_seekbar);

        seekBar1.setOnSeekBarChangeListener(seekBarChangeListener);
        //int progress = seekBar1.getProgress();
        //String.valueOf(Math.abs((long)progress)).charAt(0);
        //progressSeekbar.setText("Bewertung: " +
        //        String.valueOf(Math.abs((long)progress)).charAt(0) + "." + String.valueOf(Math.abs((long)progress)).charAt(1));

    }
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
    ViewHolder holder = new ViewHolder(view);
    Log.d(TAG, "onCreateViewHolder: ");
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
    Glide.with(mContext)
            .load(mImageUrl.get(position))
            .into(holder.imageView);
    holder.kundeName.setText(mKundeNamen.get(position));
    holder.parentLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, mKundeNamen.get(position), 
Toast.LENGTH_SHORT).show();
        }
    });
    holder.seekBar1.setTag(position);
}

@Override
public int getItemCount() {
    return mImageUrl.size();
}


SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // updated continuously as the user slides the thumb

        progressSeekbar.setText(String.valueOf(progress));


    }


    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // called when the user first touches the SeekBar
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // called after the user finishes moving the SeekBar
        if (seekBar.getTag().toString().equals("1")) {
            mBewertungen.add(seekBar.getProgress());
            if (mBewertungen.size() == 2) {
                mBewertungen.remove(0);
                Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
            }
        }
        if (seekBar.getTag().toString().equals("2")) {
            mBewertungen.add(seekBar.getProgress());
            if (mBewertungen.size() == 3) {
                mBewertungen.remove(1);
                Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
};

}

That is the activity where the Recyclerview is being displayed:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".BewertungEsserActvity"
android:orientation="vertical"
app:layoutManager="LinearLayoutManager"
android:background="@drawable/gradient_background">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="14dp"
        android:text="Bewerte deine Gäste"
        android:textColor="@color/colorDarkGrey"
        android:textSize="30sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorBlack"
        android:layout_marginBottom="10dp"/>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view">
    </androidx.recyclerview.widget.RecyclerView>

    <ProgressBar
        android:visibility="invisible"
        android:id="@+id/progressbar_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp" />
    <Button
        android:layout_marginRight="66dp"
        android:layout_marginLeft="66dp"
        android:id="@+id/bewerten_Btn"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/btn_background_profil"
        android:padding="10dp"
        android:text="Bewerten"
        android:textAllCaps="false"
        android:textColor="@color/colorWhite"
        android:textSize="16sp"
        android:textStyle="normal" />

    <View
        android:layout_marginBottom="10dp"
        android:layout_marginTop="34dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorBlack" />

</LinearLayout>

</ScrollView>

You can do it in multiple ways:

1- You can store seekbars value as a property in your model and update that property on OnSeekBarChangeListener and when the user hits the button get the data source from the adapter and then run a loop on it and call getter of that attribute.

2- You can store seekbars value as a List<> in your adapter and whenever the user hits the button get the list from the adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM