我有一个包含RecyclerView的底部对话框片段,在RecyclerView适配器中我有要选择的项目列表,如果用户选择了一个项目,片段中的TextView应该更改颜色和文本。
适配器:
public class ServiceStaffAdapter extends RecyclerView.Adapter<ServiceStaffAdapter.ViewHolder> {
List<StaffMoreStoreList> StaffMoreStoreItems;
private Context context;
private SharedPreferences spStore;
private ArrayList<String> staffArray, initialsArray = new ArrayList<String>();
private View vDialogService;
private String staffArrayString, staffIdString;
public ServiceStaffAdapter(List<StaffMoreStoreList> StaffMoreStoreItems, Context context){
super();
this.StaffMoreStoreItems = StaffMoreStoreItems;
this.context = context;
}
@Override
public ServiceStaffAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_staff_list, parent, false);
ServiceStaffAdapter.ViewHolder viewHolder = new ServiceStaffAdapter.ViewHolder(v);
SharedPreferences sp2= context.getSharedPreferences("STORE", Context.MODE_PRIVATE);
staffArrayString = sp2.getString("staffArray","");
staffArray = new ArrayList<String>();
if(staffArrayString.equals("")){
staffArray = new ArrayList<String>();
}else{
String replace = staffArrayString.replace("[","");
String replace1 = replace.replace("]","");
List<String> arrayList = new ArrayList<String> (Arrays.asList(replace1.split(",")));
staffArray = new ArrayList<String>();
for(String fav:arrayList){
staffArray.add(String.valueOf(fav.trim()));
}
}
//array of string for staffs selected
return viewHolder;
}
@Override
public void onBindViewHolder(final ServiceStaffAdapter.ViewHolder holder, int position) {
final StaffMoreStoreList staffMoreStoreList = StaffMoreStoreItems.get(position);
holder.tvStaffName.setText(staffMoreStoreList.getStaffName());
StaffMoreStoreItems.get(position).getStaffId();
Log.e("staffArray size", String.valueOf(staffArray.size()));
Log.e("staff array", staffArray.toString());
staffIdString = staffMoreStoreList.getStaffId();
//pre-select staffs
if(staffArray.contains(StaffMoreStoreItems.get(position).getStaffId())){
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.primary));
}else{
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.transparent));
}
holder.cardStaff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(staffArray.contains(staffMoreStoreList.getStaffId())){
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.transparent));
staffArray.removeAll(Collections.singleton(staffMoreStoreList.getStaffId()));
}else{
holder.profile_image.setBorderColor(ContextCompat.getColor(context, R.color.primary));
staffArray.add(staffMoreStoreList.getStaffId());
}
spStore = context.getSharedPreferences("STORE", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = spStore.edit();
editor1.putString("staffArray", staffArray.toString());
editor1.commit();
}
});
}
@Override
public int getItemCount() {
return StaffMoreStoreItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView tvStaffName;
private CardView cardStaff;
private CircleImageView profile_image;
private ViewHolder(View itemView) {
super(itemView);
tvStaffName = (TextView) itemView.findViewById(R.id.tvStaffName);
cardStaff = (CardView) itemView.findViewById(R.id.cardStaff);
profile_image = (CircleImageView) itemView.findViewById(R.id.profile_image);
}
}
}
在适配器中单击CardViews时,我想在包含RecyclerView的片段中更改TextView。 TextView将根据staffArray的大小进行相应更改。
答案 0 :(得分:0)
将此内容复制到适配器中。创建一个接口来执行onClick。
public interface OnClickListner {
void setOnItemClick(int postion, View view);
}
然后在适配器OnClickListner onClickListner ;
然后创建一个setter public void setOnClickListner(OnClickListner onClickListner ) {
this.onClickListner = onClickListner ;
}
现在在OnBind
holder.myview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickListner .setOnItemClick(position,view);
}
});
然后在你的片段中粘贴这些。
adapter.setOnClickListner(this);
他们会出现错误,将鼠标放在那里它会要求你实施onclick, 实现方法,并在片段中做任何你想做的事情,