首页 > 学院 > 开发设计 > 正文

ListView 单条item刷新

2019-11-09 15:03:24
字体:
来源:转载
供稿:网友
listView列表刷新某一条数据时采用notifyDataSetChanged()代价有点高,最好能局部刷新。局部刷新的重点是,找到要更新的那项的View,然后再根据业务逻辑更新数据即可。大概就是3步:1.找到要更新的那条数据的item 对于的view因为listView条目的复用,所以当前点击的位置对应的条目为position-FirstVisiblePosition2.获取该item中对于的相关View ,修改相关view显示的值3.获取该item中对于的相关View ,修改相关view显示的值举个例子:
public class ListAdapter extends BaseAdapter{    PRivate List<Person> mList;    private Context mContext;    private int count=0;    public ListAdapter(List<Person> list) {        mList = list;    }    @Override    public int getCount() {        return mList!=null?mList.size():0;    }    @Override    public Object getItem(int position) {        return mList.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        count++;        Log.d("getView被调用的次数===",count+"");        mContext=parent.getContext();        ViewHolder holder=null;        if(convertView==null){            holder=new ViewHolder();            convertView= LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false);            holder.mTextView= (TextView) convertView.findViewById(R.id.tv);            convertView.setTag(holder);        }else{            holder= (ViewHolder) convertView.getTag();        }        String s = mList.get(position).getName();        holder.mTextView.setText(s);        return convertView;    }    private static class ViewHolder{        TextView mTextView;    }}
public class Person {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
public class MainActivity extends AppCompatActivity {    private ListView mListView;    private List<Person> mList=new ArrayList<>();    private ListAdapter mAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        initData();        setListener();    }    private void setListener() {        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                int firstVisiblePosition = mListView.getFirstVisiblePosition(); //屏幕内当前可以看见的第一条数据                if(position-firstVisiblePosition>=0){                    //1.获取当前点击的条目的view                    View itemView = mListView.getChildAt(position - firstVisiblePosition);                    //2.查找出相应的控件                    TextView textView= (TextView) itemView.findViewById(R.id.tv);                    //3.更新ui                    textView.setText("我是更新后的数据"+position);                    //4.更新数据源                   mList.get(position).setName("我是更新后的数据"+position);                }            }        });    }    private void initData() {        Person person=null;        for (int i = 0; i < 30; i++) {            person=new Person();            person.setName("条目"+i);            mList.add(person);        }        mAdapter=new ListAdapter(mList);        mListView.setAdapter(mAdapter);    }    private void initView() {        mListView= (ListView) findViewById(R.id.lv);    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表