gpt4 book ai didi

android - 按钮在 ListView 项目中单击,混淆项目上的操作

转载 作者:行者123 更新时间:2023-11-29 00:15:55 24 4
gpt4 key购买 nike

我有一个带有按钮的 ListView 。当我单击按钮时,我希望为整个项目设置一个 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";
}

最佳答案

尝试在 CustomModel 类上添加一个 String 状态标志,并为此标志设置默认值:

public class CustomModel {
private String status = "strike";

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}

现在尝试在点击发生时改变状态值,并通知 adapter:

holder.strikeoff.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(rowItem.get(position).getStatus().equals("strike")){
rowItem.get(position).setStatus("undo");
}else{
rowItem.get(position).setStatus("strike");
}
notifyDataSetChanged();
}
}

根据 getView() 中的状态更改列表项 alpha 和文本值:

@Override
public View getView(int position, View vi, ViewGroup parent) {

if(rowItem.get(position).getStatus().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");
} 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");
}


return vi;
}

关于android - 按钮在 ListView 项目中单击,混淆项目上的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26923515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com