我有一个带有按钮的列表视图。当我点击按钮时,我希望整个项目设置一个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";
}
}
答案 0 :(得分:2)
尝试在CustomModel
类上添加一个字符串状态标志,并将deafult值设置为此标志:
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;
}
答案 1 :(得分:0)
使用SetTag getTag ... ...
public class Audio_Post_Adapter extends BaseAdapter {
Context mContext;
ArrayList<String> mSampalList;
Holder mHolder;
LayoutInflater inflater;
public Audio_Post_Adapter(Context mContext) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(mContext);
this.mContext = mContext;
this.mSampalList = mSampalList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mSampalList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertview, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertview;
if(view == null){
view = LayoutInflater.from(mContext).inflate(
R.layout.view_audio, null,false);
mHolder = new Holder();
mHolder.mLn_main_audio = (LinearLayout) view.findViewById(R.id.mLn_main_audio);
mHolder.mImg_profile_user = (CustomNetworkImageView) view.findViewById(R.id.mImg_profile_user);
view.setTag(mHolder);
}else{
mHolder = (Holder) view.getTag();
}
mHolder.mImg_profile_user.setTag(position);
mHolder.mImg_profile_user.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int index = (Integer) v.getTag();
//What ever list is take the data from it using this index
String data = mSampalList.get(index);
}
});
return view;
}
public class Holder{
private LinearLayout mLn_main_audio;
private CustomNetworkImageView mImg_profile_user,mImg_post_img;
}
}