1.整体刷新
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.list_view);
final List<Message> messageList = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
String time = Calendar.getInstance().getTime().toLocaleString();
messageList.add(new Message(
R.drawable.icon_contact,
"好友" + i,
"我是文本消息" + i,
time));
}
final BaseAdapter adapter = new MessageAdapter(this, messageList);
listView.setAdapter(adapter);
findViewById(R.id.add_message).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String time = Calendar.getInstance().getTime().toLocaleString();
messageList.add(0, new Message(
R.drawable.icon_contact,
"好友",
"我是文本消息",
time));
adapter.notifyDataSetChanged();
}
});
findViewById(R.id.remove_message).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (messageList.size() > 0) {
messageList.remove(0);
adapter.notifyDataSetChanged();
}
}
});
}
}
部分刷新,有些数据只是listview的某个item的数据发生了变化,这个时候并不需要整体刷新,只需要改变对应的item的数据:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<Message> datas;
private EditText modifyPosition;
private View modifyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
datas = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
String time = Calendar.getInstance().getTime().toLocaleString();
datas.add(new Message(
R.drawable.icon_contact,
"好友" + i,
"我是文本消息" + i,
time));
}
ListAdapter adapter = new MessageAdapter(this, datas);
listView.setAdapter(adapter);
modifyPosition = (EditText) findViewById(R.id.modify_position);
modifyButton = findViewById(R.id.modify_message);
modifyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
int position = Integer.parseInt(modifyPosition.getText().toString());
Message message = datas.get(position);
message.setContent("这是修改后的消息" + position);//这只是把数据设置好,但是并没有刷新显示界面!
partialRefresh(listView, position);//刷新数据
} catch (NumberFormatException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "输入不合法", Toast.LENGTH_SHORT).show();
}
}
});
}
private void partialRefresh(ListView listView, int position) {
if (position >= listView.getFirstVisiblePosition()
&& position <= listView.getLastVisiblePosition()) {
int childIndex = position - listView.getFirstVisiblePosition();
Toast.makeText(getApplicationContext(),childIndex+"",Toast.LENGTH_LONG).show();
View child = listView.getChildAt(childIndex);
if (child.getTag() instanceof ViewHolder) {
//child.getTag() 是一个viewholder,在viewholder中有一个刷新方法,把传入的内容填充进对应的控件
((ViewHolder) child.getTag()).refreshContent(datas.get(position).getContent());
// ((ViewHolder) child.getTag()).refreshContent(datas.get(position).getContent());
}
}
}
}
viewholder
public class MessageAdapter extends BaseAdapter {
private List<Message> messageList;
private LayoutInflater layoutInflater;
public MessageAdapter(Context context, List<Message> messageList) {
this.layoutInflater = LayoutInflater.from(context);
this.messageList = messageList;
}
@Override
public int getCount() {
return messageList.size();
}
@Override
public Object getItem(int position) {
return messageList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.text_message_item, parent, false);
TextView time = (TextView) convertView.findViewById(R.id.message_time);
TextView name = (TextView) convertView.findViewById(R.id.message_name);
ImageView head = (ImageView) convertView.findViewById(R.id.message_head);
TextView content = (TextView) convertView.findViewById(R.id.message_content);
viewHolder = new ViewHolder(time, name, head, content);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Message message = messageList.get(position);
viewHolder.getTime().setText(message.getTime());
viewHolder.getName().setText(message.getName());
viewHolder.getHead().setImageResource(message.getHeadImageResId());
viewHolder.getContent().setText(message.getContent());
return convertView;
}
public static class ViewHolder {
private TextView time;
private TextView name;
private ImageView head;
private TextView content;
public ViewHolder(
TextView time,
TextView name,
ImageView head,
TextView content) {
this.time = time;
this.name = name;
this.head = head;
this.content = content;
}
public TextView getTime() {
return time;
}
public TextView getName() {
return name;
}
public ImageView getHead() {
return head;
}
public TextView getContent() {
return content;
}
public void refreshContent(String content) {
this.content.setText(content);
}
}
}