[英]Getting the progress of multiple seekbars in recycler view
So I am displaying a recylerview with an Image and the name of a person. 因此,我正在显示带有图像和人名的recylerview。 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: 那是我的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: 那是我的RecyclerViewAdapter类:
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: 那是显示Recyclerview的活动:
<?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. 1-您可以将seekbars值存储为模型中的属性,并在OnSeekBarChangeListener
上更新该属性,当用户单击按钮时,从适配器获取数据源,然后对其进行循环并调用该属性的getter。
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. 2-您可以在适配器中将seekbars值存储为List<>
,每当用户单击按钮时,就从适配器获取列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.