1. 概述
在自己开发中,有这样一个需求,如下图所示:点击图中的领券购买button,然后让文字变成已购买。这篇文章主要记录下自己在实现过程中遇到的问题:自己直接在 adapter中给 领券购买设置点击事件,然后修改文字为 已购买,如果只有一屏幕数据,是可以的,如果数据一多,上下滑动listview列表,之前的已购买 的文字又会恢复成 领券购买,而且可能下边的有的 item的文字 也变成 已购买,发生数据错乱,这篇文章主要记录下自己的解决方案
2. 解决方案如下
严格来说这个不是我的解决方案,这个是百度好多资料然后搜出来的,代码放到这里,给自己作为一个学习的笔记,也希望可以给以后有这种需求的兄弟们留作一个参考。
1>:ListViewActivity如下:
/**
* Created by wn on 2018/4/27.
*/
public class ListViewActivity extends Activity implements MyAdapter.Callback,AdapterView.OnItemClickListener{
//ListView控件
private ListView mList;
//ListView数据源
private List data;
private MyAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
data = new ArrayList();
mList = (ListView)findViewById(R.id.mList);
for(int i = 0; i < 20; i ++){
AppBean appBean = new AppBean();
appBean.setAppName("斗鱼APP"+i);
appBean.setDownloadStatus("安装");
data.add(appBean);
}
adapter = new MyAdapter(data,this);
mList.setAdapter(adapter);
mList.setOnItemClickListener(this);
}
@Override
public void click(View view) {
switch (view.getId()){
case R.id.mTv:
int tv = (int) view.getTag(R.id.tv);
Toast.makeText(ListViewActivity.this,"我是文本"+tv,Toast.LENGTH_SHORT).show();
break;
case R.id.mBtn:
int btn = (int) view.getTag(R.id.btn);
Toast.makeText(ListViewActivity.this,"我是按钮"+btn,Toast.LENGTH_SHORT).show();
if (data.get(btn).getDownloadStatus().equals("已安装")){
data.get(btn).setDownloadStatus("安装");
}else if(data.get(btn).getDownloadStatus().equals("安装")){
data.get(btn).setDownloadStatus("已安装");
}
updateItem(btn);
//adapter.notifyDataSetChanged(); //更新全部
break;
}
}
//listView局部更新
private void updateItem(int position) {
//屏幕中第一个可见的条目位置
int firstVisiblePosition = mList.getFirstVisiblePosition();
//屏幕中最后一个可见的条目位置
int lastVisiblePosition = mList.getLastVisiblePosition();
//在看见范围内才更新,不可见的滑动后自动会调用getView方法更新
if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
//获取指定位置view对象
View view = mList.getChildAt(position - firstVisiblePosition);
adapter.getView(position, view, mList);
}
}
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
Toast.makeText(ListViewActivity.this,"我是条目"+i,Toast.LENGTH_SHORT).show();
}
}
2>:MyAdapter如下:
/**
* Created by wn on 2018/4/27.
*/
public class MyAdapter extends BaseAdapter implements View.OnClickListener {
//上下文
private Context context;
//数据项
private List data;
private Callback mCallback;
public MyAdapter(List data,Callback callback){
this.data = data;
mCallback = callback;
}
@Override
public int getCount() {
return data == null ? 0 : data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
System.out.println("position:"+position+"convertView:"+convertView);
ViewHolder viewHolder = null;
if(context == null) {
context = viewGroup.getContext();
}
if(convertView == null){
convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item,null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView)convertView.findViewById(R.id.mTv);
viewHolder.button = (Button)convertView.findViewById(R.id.mBtn);
//为convertView设置tag标志
convertView.setTag(viewHolder);
}
//获取viewHolder实例
viewHolder = (ViewHolder)convertView.getTag();
//设置数据
viewHolder.textView.setText(data.get(position).getAppName());
viewHolder.button.setText(data.get(position).getDownloadStatus());
//为viewHolder.mTv和viewHolder.mBtn设置tag标记
viewHolder.textView.setTag(R.id.tv,position);
viewHolder.button.setTag(R.id.btn,position);
//设置监听事件
viewHolder.textView.setOnClickListener(this);
viewHolder.button.setOnClickListener(this);
return convertView;
}
@Override
public void onClick(View view) {
mCallback.click(view);
}
static class ViewHolder{
TextView textView;
Button button;
}
public interface Callback {
void click(View view);
}
}
3>:AppBean如下:
/**
* Created by wn on 2018/4/27.
*/
public class AppBean implements Serializable {
private String appName ;
private String downloadStatus ;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getDownloadStatus() {
return downloadStatus;
}
public void setDownloadStatus(String downloadStatus) {
this.downloadStatus = downloadStatus;
}
}
4>:listview_main.xml布局文件
5>:list_item.xml布局文件:
6>:values文件夹下的ids.xml文件:
-
-
运行结果如下图所示:这个图是百度别人的