listview 刷新_listview自动刷新数据「建议收藏」

listview 刷新_listview自动刷新数据「建议收藏」1.整体刷新@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentV

listview

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); } } } 






今天的文章listview 刷新_listview自动刷新数据「建议收藏」分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/68822.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注