我有RecyclerView
和适配器
public class AdapterToUpdateOk extends RecyclerView.Adapter<AdapterToUpdateOk.VersionViewHolder> {
String[] desc;
Context context;
LayoutInflater inflater;
AdapterToUpdateOk adapter;
public AdapterToUpdateOk(Context context,String [] desc) {
this.context = context;
this.desc= desc;
inflater = LayoutInflater.from(context);
}
@Override
public VersionViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.inflater_sugessted_friends, viewGroup, false);
return new VersionViewHolder(view);
}
@Override
public void onBindViewHolder(final VersionViewHolder versionViewHolder, final int i) {
final VersionViewHolder v = versionViewHolder;
versionViewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
versionViewHolder.imageView.setImageResource(R.drawable.ic_ok);
}
});
}
@Override
public int getItemCount() {
return desc.length;
}
class VersionViewHolder extends RecyclerView.ViewHolder {
Button button;
ImageView imageView;;
public VersionViewHolder(final View itemView) {
super(itemView);
button= itemView.findViewById(R.id.button);
imageView = itemView.findViewById(R.id.imageview);
}
}
}
当前它可以工作,但它会影响其他未被点击的部分
答案 0 :(得分:3)
notifyItemChanged(Adapterposition) , Work fine , and here is some description
notifyItemChanged method tell OnBind To redraw item at that position, Your must consider update arrayData at that Position to reflect new changes before calling this method
versionViewHolder.ClickedView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayDataAt[position] = " New Data to replace Old one ";
notifyItemChanged(Adapterposition);
}
});
This is exactly like in my case , ( and the same as to update background color , text colors etc .
You can create an identifier ( simply a boolean array ) which contain status of your rows , let say all rows will have a boolean value false , when a row is click update booleanArray at that position to be true , remember to tell on bind to consider true element with specific operation like different colors , different font , etc , and finally notify notifyItemChanged(Adapterposition);
Onclick event changes appear , but after scrolling away from that view and back again OnBind Keep on Recreating the view with the same Data as old ones . ( its important to either update the array data at position or to have a flag that show data have been updated )
My view come with drawable item which i never update on bind , so Onclick , those items that appear to be at the same UI with clicked item will also reflect changes . ( There was mixed concept of dynamic view element and static view element )
Consider to put your data in Array first .
Onclick Update your SQLite data in background and update view to reflect that data (Don't waste memory on redraw the whole recyclerview from SQLite again)
Thanks to all contributers .
答案 1 :(得分:2)
在viewHolder构造函数中使用此代码:
public ViewHolder(View view){
button = (Button) view.findViewById(R.id.yourButtonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img.setImageResource(R.drawable.ic_ok);
}
});
}
答案 2 :(得分:2)
最好将其放入onBindViewHolder
并生成按钮点击,如此
@Override
public void onBindViewHolder(ViewHolder versionViewHolder, int i) {
versionViewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img.setImageResource(R.drawable.ic_ok);
}
});
}
从参数中删除最终关键字
答案 3 :(得分:0)
如@sumit所述,使用ViewHolder
构造函数中提到的代码。在onBind
中定义ViewHolder
功能,将img
设置为默认状态。
class ViewHolder extends RecyclerView.ViewHolder {
Button button;
ImageView img;
public ViewHolder(View view){
button = (Button) view.findViewById(R.id.ButtonId);
img = (ImageView) view.findViewById(R.id.ImageViewId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img.setImageResource(R.drawable.ic_ok);
}
});
}
void onBind(){
img.setImageResource(R.drawable.default_image_resource);
}
}
最后从适配器的onBind
拨打onBindViewHolder
,如
@Override
public void onBindViewHolder(ViewHolder versionViewHolder, int i) {
versionViewHolder.onBind();
}
您可以直接将img
设置为onBindViewHolder
中的默认资源。您应该在onBindViewHolder
或onRecycled
中对项目的小部件执行默认设置,否则您将看到修改后的视图,因为大多数时候都会回收视图。
答案 4 :(得分:0)
这是实现此目的的另一种方法。下面是您可以在代码中修改和使用的示例RecyclerView适配器。主要实现在onBindViewHolder和onCreateViewHolder上,它创建在TransactionViewHolder类上定义的自定义视图持有者。
public class TransactionHistoryAdapter extends RecyclerView.Adapter<TransactionViewHolder> {
private static final String TAG =TransactionHistoryAdapter.class.getSimpleName();
private List<Model> orders;
private Context context;
public TransactionHistoryAdapter(List<Model> orders, Context context) {
this.orders = orders;
this.context = context;
}
@Override
public TransactionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_order_item, parent, false);
return new TransactionViewHolder(view);
}
@Override
public void onBindViewHolder(final TransactionViewHolder holder, int position) {
final Order model = (Order) orders.get(position);
holder.date.setText(model.getOrder_date());
holder.deliveryCode.setText(model.getDelivery_code());
holder.totalCost.setText("Tsh "+model.getTotalCost());
if(model.getStatus()==-1){
holder.status.setText("Please wait, Your order has been PLACED");
holder.stateProgress.setCurrentStateNumber(StateProgressBar.StateNumber.ONE);
holder.totalCost.setText("Not Available");
holder.deliveryCode.setText(" - ");
holder.viewDetailsButton.setText(model.getOrder_summary());
}else if(model.getStatus()==0){
holder.status.setText("Your order is PENDING");
holder.stateProgress.setCurrentStateNumber(StateProgressBar.StateNumber.ONE);
}else if(model.getStatus()==1){
holder.status.setText("Your order is on Transit");
holder.stateProgress.setCurrentStateNumber(StateProgressBar.StateNumber.TWO);
}else if(model.getStatus()==2){
holder.status.setText("Your order was Delivered");
holder.stateProgress.setAllStatesCompleted(true);
}else{
holder. status.setText("This order was canceled");
}
if(model.getStatus()!=-1) {
holder.viewDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, OrderDetailsActivity.class);
intent.putExtra("order_id", model.getId());
context.startActivity(intent);
}
});
}
}
@Override
public int getItemCount() {
return orders.size();
}
}
这是一个示例ViewHolder类
public class TransactionViewHolder extends RecyclerView.ViewHolder {
public View itemView;
public TextView date;
public TextView totalCost;
public TextView status;
public TextView deliveryCode;
public StateProgressBar stateProgress;
public TextView viewDetailsButton;
public TransactionViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
date = (TextView) itemView.findViewById(R.id.order_date_value);
stateProgress = (StateProgressBar) itemView.findViewById(R.id.progress);
totalCost = (TextView) itemView.findViewById(R.id.total_value);
status = (TextView) itemView.findViewById(R.id.order_status);
deliveryCode = (TextView) itemView.findViewById(R.id.order_number_value);
viewDetailsButton = (TextView) itemView.findViewById(R.id.view_details_button);
}
}
答案 5 :(得分:0)
public class AdapterToUpdateOk extends RecyclerView.Adapter<AdapterToUpdateOk.VersionViewHolder> {
String[] desc;
Context context;
LayoutInflater inflater;
private OnDashBoardClickListener onDashBoardClickListener;
AdapterToUpdateOk adapter;
public AdapterToUpdateOk(Context context,String [] desc) {
this.context = context;
this.desc= desc;
inflater = LayoutInflater.from(context);
}
@Override
public VersionViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.inflater_sugessted_friends, viewGroup, false);
return new VersionViewHolder(view);
}
public void setOnDashBoardClickListener(OnDashBoardClickListener onDashBoardClickListener) {
this.onDashBoardClickListener = onDashBoardClickListener;
}
@Override
public void onBindViewHolder(final VersionViewHolder versionViewHolder, final int i) {
final VersionViewHolder v = versionViewHolder;
viewHolder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onDashBoardClickListener!=null)
onDashBoardClickListener.onClick(viewHolder.getAdapterPosition(),value);
}
});
}
class ViewHolder extends RecyclerView.ViewHolder {
Button button;
ImageView img;
public ViewHolder(View view){
button = (Button) view.findViewById(R.id.ButtonId);
img = (ImageView) view.findViewById(R.id.ImageViewId);
}
public void setOnClickListener(View.OnClickListener onClickListener){
if(onClickListener!=null)
title_gate_layout.setOnClickListener(onClickListener);
}
}interface OnDashBoardClickListener {void onClick(int position,String value);}}