1,最近在面试题目的时候突然看到了问题“ListView对其指定的子Item进行单独的刷新”,当时感觉一脸懵逼,还是第一次听到这个问题,也就是不能使用adapter.notifyDataSetChanged(),一点思路都没有,下班吃饭之后,突然觉得还是敲代码来试试吧,说不定思路就出来了。
2,首先添加我们在布局文件中添加ListView,布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.qianmo.listviewupdatebyone.MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
创建MyAdapter适配器,继承自BaseAdapter类,代码如下:
package com.qianmo.listviewupdatebyone.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.qianmo.listviewupdatebyone.R; import java.util.List; /** * Created by Administrator on 2017/3/22 0022. * E-Mail:543441727@qq.com */ public class MyAdapter extends BaseAdapter { private Context mContext; private List<String> datas; public MyAdapter(Context context, List<String> datas) { this.mContext = context; this.datas = datas; } @Override public int getCount() { return datas == null ? 0 : datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_adapter, null); viewHolder = new ViewHolder(); viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.mTextView.setText(datas.get(position)); return convertView; } class ViewHolder { private TextView mTextView; } }
在activity中添加适配器,并添加点击监听
package com.qianmo.listviewupdatebyone; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import com.qianmo.listviewupdatebyone.adapter.MyAdapter; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView mListView; private List<String> datas; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { datas = new ArrayList<>(); for (int i = 0; i < 20; i++) { datas.add(i + ""); } mListView = (ListView) findViewById(R.id.listview); myAdapter = new MyAdapter(MainActivity.this, datas); mListView.setAdapter(myAdapter); //这里实现单个更新 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); } }
以上是最基本的ListView的创建,所以这里我们来看看我们能有几种方法来实现
3,实现方法
①通过onItemClick()方法中的View就是我们的ViewHolder来更新数据
所以我们的代码可以这样写
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { datas.set(position, "这是被更新的数据哦"); if (position != datas.size()-1){ datas.set(position+1, "这是被更新的数据哦,但是不展示"); } //通过拿到TextView来更新数据 TextView testView = (TextView) view.findViewById(R.id.textView); testView.setText(datas.get(position)); } });
看一下效果
没问题 ,我们继续看下一种方法
②通过再次调用adapter的getView()方法来更新
代码如下:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { datas.set(position, "这是被更新的数据哦"); if (position != datas.size()-1){ datas.set(position+1, "这是被更新的数据哦,但是不展示"); } //通过调用getview myAdapter.getView(position, view, mListView); } });
看一下效果,没问题
暂且只试出来两种了,如果还有其他思路 ,欢迎共享·····