我有一个带有按钮的列表视图。当我单击按钮时,我希望将整个项目设置为 alpha 值并更改按钮的文本。基本功能(设置 alpha 值和按钮文本更改的项目)有效,但多个列表项因此获得相同的效果。我也尝试在stackoverflow中阅读很多关于此的帖子,但对我没有用。我真的尝试了很多,请帮助。
我的适配器代码如下:
public class CustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
Context context;
ArrayList<CustomModel> rowItem;
String status = "strike";
CustomAdapter(Context context, ArrayList<CustomModel> rowItem) {
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.rowItem = rowItem;
}
@Override
public int getCount() {
return rowItem.size();
}
@Override
public Object getItem(int position) {
return rowItem.get(position);
}
@Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
@Override
public View getView(int position, View vi, ViewGroup parent) {
//View vi = convertView; // trying to reuse a recycled view
final ViewHolder holder;
if (vi == null) {
// The view is not a recycled one: we have to inflate
vi = mInflater.inflate(R.layout.order_item, null);
holder = new ViewHolder();
holder.rm = rowItem.get(position);
holder.rootLayout = (LinearLayout) vi.findViewById(R.id.root_Layout);
holder.tv_name = (TextView) vi.findViewById(R.id.item_name);
holder.tv_quantity = (TextView) vi.findViewById(R.id.item_qty);
holder.tv_price = (TextView) vi.findViewById(R.id.item_price);
holder.tv_rate = (TextView) vi.findViewById(R.id.item_rate);
holder.layout1 = (LinearLayout) vi.findViewById(R.id.layout1);
holder.layout2 = (LinearLayout) vi.findViewById(R.id.layout2);
holder.layout3 = (LinearLayout) vi.findViewById(R.id.layout3);
holder.layout4 = (LinearLayout) vi.findViewById(R.id.layout4);
holder.tv_prefs = (TextView) vi.findViewById(R.id.pref_name);
holder.baseLayout = (LinearLayout) vi
.findViewById(R.id.base_layout);
holder.strikeoff = (Button) vi.findViewById(R.id.item_strike);
holder.status = "strike";
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
//holder.strikeoff.setTag(holder);
holder.strikeoff.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//@SuppressWarnings("unused")
//ViewHolder holder = (ViewHolder) v.getTag();
if (holder.status.equals("strike")) {
holder.tv_rate.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.tv_price.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.layout1.setAlpha(.25f);
holder.layout2.setAlpha(.25f);
holder.layout3.setAlpha(.25f);
holder.layout4.setAlpha(.25f);
holder.tv_name.setAlpha(.25f);
holder.strikeoff.setText("Undo");
holder.status = "undo";
}
}
});
if (rowItem != null || !rowItem.isEmpty() || rowItem.size() != 0) {
holder.tv_name.setText(rowItem.get(position).getItemName());
holder.tv_quantity.setText(rowItem.get(position).getItemQuantity());
holder.tv_price.setText("Price: "
+ rowItem.get(position).getItemPrice());
holder.tv_rate.setText("Rate :"
+ rowItem.get(position).getItemRate());
if (!rowItem.get(position).getPrefName().equals("Nil")) {
holder.tv_prefs.setText(rowItem.get(position).getPrefName());
}
// Alternate colour for rows in listview
if (position % 2 == 0) {
holder.baseLayout.setBackgroundColor(Color.WHITE);
} else {
holder.baseLayout.setBackgroundColor(Color
.parseColor("#ededed"));
}
}
return vi;
}
class strikeButtonClickListener implements OnClickListener {
int position;
ViewHolder holder;
public strikeButtonClickListener(int pos, ViewHolder holder) {
this.position = pos;
this.holder = holder;
}
public void onClick(View v) {
if (holder.status.equals("strike")) {
holder.tv_rate.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.tv_price.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.layout1.setAlpha(.25f);
holder.layout2.setAlpha(.25f);
holder.layout3.setAlpha(.25f);
holder.layout4.setAlpha(.25f);
holder.tv_name.setAlpha(.25f);
holder.strikeoff.setText("Undo");
holder.status = "undo";
} else {
holder.tv_rate.setPaintFlags(holder.tv_rate.getPaintFlags()
& (~Paint.STRIKE_THRU_TEXT_FLAG));
holder.tv_price.setPaintFlags(holder.tv_rate.getPaintFlags()
& (~Paint.STRIKE_THRU_TEXT_FLAG));
holder.layout1.setAlpha(1f);
holder.layout2.setAlpha(1f);
holder.layout3.setAlpha(1f);
holder.layout4.setAlpha(1f);
holder.tv_name.setAlpha(1f);
holder.strikeoff.setText("Strike off");
holder.status = "strike";
}
}
}
static class ViewHolder {
CustomModel rm;
Button strikeoff;
View strikeview;
TextView tv_name, tv_quantity, tv_price, tv_rate, tv_prefs, ratetxt,
pricetxt;
LinearLayout baseLayout, rootLayout, layout1, layout2, layout3,
layout4;
FrameLayout frameLayout;
ViewGroup vg;
String status = "strike";
}
}