我有回收数据库中显示的项目(服务)列表数据来自数据库,当我第一次单击项目时,它需要双击以执行操作,而当我下次单击相同的项目时,单击一次就可以正常工作< / p>
这是我的适配器代码
public class style_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<style> dataSet;
Context mContext;
int total_types;
String currency;
MediaPlayer mPlayer;
String[] styled;
HashMap<String, String> capitalCities = new HashMap<String, String>(); ;
boolean show=true;
String test="1";
int selected_position = 0;
private boolean fabStateVolume = false;
private OnItemstyle listener;
public interface OnItemstyle {
void onItemClick(style item);
}
public static class TextTypeViewHolder extends RecyclerView.ViewHolder {
ConstraintLayout constant;
View itemv;
TextView name, detail,prince_amount;
ImageView imageView,tick;
LinearLayout lsItem;
public TextTypeViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
prince_amount = (TextView) itemView.findViewById(R.id.prince_amount);
detail = (TextView)itemView.findViewById(R.id.detail);
imageView = (ImageView) itemView.findViewById(R.id.image);
tick = (ImageView) itemView.findViewById(R.id.tick);
itemv = itemView;
lsItem = (LinearLayout) itemView.findViewById(R.id.linearLayout);
constant = (ConstraintLayout) itemView.findViewById(R.id.constant);
}
}
public static class ImageTypeViewHolder extends RecyclerView.ViewHolder {
ConstraintLayout constant;
TextView names, details,prince_amounts;
ImageView imageViews,tick;
LinearLayout lsItems;
View itemv;
public ImageTypeViewHolder(View itemView) {
super(itemView);
names = (TextView) itemView.findViewById(R.id.name);
prince_amounts = (TextView) itemView.findViewById(R.id.prince_amount);
details = (TextView)itemView.findViewById(R.id.detail);
constant = (ConstraintLayout) itemView.findViewById(R.id.constant);
imageViews = (ImageView) itemView.findViewById(R.id.image);
tick = (ImageView) itemView.findViewById(R.id.tick);
itemv = itemView;
lsItems = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
public style_adapter(Context context, List<style> data, OnItemstyle listener) {
this.dataSet = data;
this.mContext = context;
total_types = dataSet.size();
this.listener = listener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case style.TEXT_TYPE:
view = LayoutInflater.from(mContext).inflate(R.layout.style_list, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new TextTypeViewHolder(view);
case style.IMAGE_TYPE:
view = LayoutInflater.from(mContext).inflate(R.layout.style_right, null, false);
RecyclerView.LayoutParams lps = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lps);
return new ImageTypeViewHolder(view);
}
return null;
}
@Override
public int getItemViewType(int position) {
switch (dataSet.get(position).getype()) {
case 0:
return style.TEXT_TYPE;
case 1:
return style.IMAGE_TYPE;
default:
return -1;
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int
listPosition)
{
final style object = dataSet.get(listPosition);
if (object != null) {
switch (object.getype()) {
case style.TEXT_TYPE:
object.setSelect(false);
SharedPreferences settings =
mContext.getSharedPreferences("YOUR_PREF_NAME", 0);
String currency_name = settings.getString("currency_name", "");
AssetManager am = mContext.getApplicationContext().getAssets();
Typeface face= Typeface.createFromAsset(am,"fonts/MontserratRegular.otf");
Typeface face1= Typeface.createFromAsset(am,"fonts/MontserratThin.ttf");
((TextTypeViewHolder) holder).name.setTypeface(face);
((TextTypeViewHolder) holder).detail.setTypeface(face1);
if(object.getImage().equals("null"))
{
Glide
.with(mContext)
.load(R.drawable.noimage_placeholder)
.into( ((TextTypeViewHolder) holder).imageView);
}
else{
Glide
.with(mContext)
.load(object.getImage())
.fitCenter()
.into( ((TextTypeViewHolder) holder).imageView);
}
((TextTypeViewHolder) holder).constant.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(object.isSelect()){
((TextTypeViewHolder)
holder).tick.setVisibility(View.VISIBLE);
selected_position = listPosition;
capitalCities.put(String.valueOf(listPosition),
String.valueOf(object.getId()));
object.setStyled(capitalCities);
listener.onItemClick(object);
test="2";
object.setSelect(false);
}
else {
((TextTypeViewHolder) holder).tick.setVisibility(View.GONE);
selected_position = listPosition;
capitalCities.remove(String.valueOf(listPosition));
object.setStyled(capitalCities);
test="1";
object.setSelect(true);
listener.onItemClick(object);
}
}
});
((TextTypeViewHolder) holder).name.setText(object.getName());
((TextTypeViewHolder) holder).detail.setText(object.getDetail());
((TextTypeViewHolder) holder).prince_amount.setText(currency_name+ "
"+object.getamount());
break;
case style.IMAGE_TYPE:
object.setSelect(false);
SharedPreferences setting = mContext.getSharedPreferences("YOUR_PREF_NAME", 0);
String currency_names = setting.getString("currency_name", "");
AssetManager ams = mContext.getApplicationContext().getAssets();
Typeface faces=
Typeface.createFromAsset(ams,"fonts/MontserratRegular.otf");
Typeface face1s= Typeface.createFromAsset(ams,"fonts/MontserratThin.ttf");
((ImageTypeViewHolder) holder).names.setTypeface(faces);
((ImageTypeViewHolder) holder).details.setTypeface(face1s);
if(object.getImage().equals("null"))
{
Glide
.with(mContext)
.load(R.drawable.noimage_placeholder)
.into( ((ImageTypeViewHolder) holder).imageViews);
}
else{
Glide
.with(mContext)
.load(object.getImage())
.fitCenter()
.into( ((ImageTypeViewHolder) holder).imageViews);
}
((ImageTypeViewHolder) holder).constant.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(object.isSelect()){
((ImageTypeViewHolder) holder).tick.setVisibility(View.VISIBLE);
selected_position = listPosition;
capitalCities.put(String.valueOf(listPosition),
String.valueOf(object.getId()));
object.setStyled(capitalCities);
object.setSelect(false);
test="2";
listener.onItemClick(object);
}
else {
((ImageTypeViewHolder) holder).tick.setVisibility(View.GONE);
selected_position = listPosition;
capitalCities.remove(String.valueOf(listPosition));
object.setStyled(capitalCities);
object.setSelect(true);
test="1";
listener.onItemClick(object);
}
}
});
((ImageTypeViewHolder) holder).names.setText(object.getName());
((ImageTypeViewHolder) holder).details.setText(object.getDetail());
((ImageTypeViewHolder) holder).prince_amounts.setText(currency_names+"
"+object.getamount());
break;
}
}
}
@Override
public int getItemCount() {
return dataSet.size();
}
}
设计代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:focusedByDefault="false"
android:descendantFocusability="blocksDescendants"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/constant"
android:orientation="vertical"
android:focusedByDefault="false"
android:descendantFocusability="blocksDescendants"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="5dp"
android:background="@color/white"
>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="horizontal">
<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/image"
android:src="@drawable/bpic"
android:scaleType="centerCrop"
android:layout_marginTop="20dp"
app:riv_corner_radius="10dip"
android:layout_gravity="top"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<LinearLayout
android:paddingLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textStyle="bold"
android:id="@+id/name"
android:paddingTop="5dp"
android:textSize="17sp"
android:text="Proraso Beard Oil"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:maxLines="5"
android:layout_weight="1"
android:id="@+id/detail"
android:ellipsize="end"
android:paddingTop="10dp"
android:textSize="13sp"
android:text="Beard Oil Proraso Beard Oil helps tame, soften,
soften and protect the beard. With macadamia and avocado oils, among
others, it has a scent of cedar wood and Mediterranean rose that leaves
a pleasant sensation of freshness."/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:maxLines="5"
android:layout_weight="1"
android:id="@+id/prince_amount"
android:textStyle="bold"
android:ellipsize="end"
android:paddingTop="10dp"
android:text="500 $"
android:textSize="16sp"
/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/tick"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_constraintTop_toTopOf="@+id/main"
android:layout_width="24dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="10dp"
android:layout_height="24dp"
android:visibility="gone"
android:background="@android:color/transparent"
android:src="@drawable/checked" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#dedede"/>
</LinearLayout>
我有一个多选的recyclerview ...仅当我选择recyclerview项时,图像视图才可见