Android: Partial refresh of ListView, androidlistview

Source: Internet
Author: User

Android: Partial refresh of ListView, androidlistview

1. Introduction

Most programmers are familiar with the ListView refresh mechanism in android. After modifying or adding the data source in the adapter, call yydatasetchanged () to refresh the ListView. In this mode, the control will display different content in getView based on different data sources. This mode is the most common refresh mode. When we slide the ListView back and forth, call the getView method of the adapter, and then the listview draws the View returned by the adapter. In this mode, the display content or status of the View is recorded in the data source in the adapter. The listview is updated less frequently and is updated as the data source changes.

->Introduction of ListView partial Refresh:

Suppose our ListView Item has a progress bar and a button. When we click the button, the progress bar will be refreshed from 0 to 100, generally, the refresh process must be completed within 1 s. That is to say, after a Button event is triggered in any Item of ListView, the refresh process must be within 1 s or shorter, the progress bar needs to be refreshed 100 times. Obviously, if we modify the data source, it is obviously inappropriate to call yydatasetchanged () to refresh the data source. The efficiency is extremely low and it is not necessarily effective. Then, we naturally want to get the object of the ProgressBar control inside the View after clicking, and then directly call the setProgress of progressBar, I thought this was a success. Suddenly, you will find that when the progressBar is being updated, and now, sliding down the listview, suddenly found a progress bar below is also updated. After careful analysis, it makes sense that the View in ListView is reused. When you slide down the listview, The progressBar object you operate is no longer the Item you just clicked, because many items reuse a View. How can this problem be solved?

2. Solution

Record the position of the clicked Item, and constantly judge whether the position is between visible items during the update process. If yes, the position is updated. If no, it is not updated.

private void updateProgressPartly(int progress,int position){int firstVisiblePosition = listview.getFirstVisiblePosition();int lastVisiblePosition = listview.getLastVisiblePosition();if(position>=firstVisiblePosition && position<=lastVisiblePosition){View view = listview.getChildAt(position - firstVisiblePosition);if(view.getTag() instanceof ViewHolder){ViewHolder vh = (ViewHolder)view.getTag();vh.pb.setProgress(progress);}}}


Other related code:

ListAdapter

/* * $filename: ListAdapter.java,v $ * $Date: 2014-9-19  $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */package net.mobctrl.listviewdemo;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ProgressBar;/* *@author: ZhengHaibo   *blog:     http://blog.csdn.net/nuptboyzhb *mail:    zhb931706659@126.com *web:     http://www.mobctrl.net *2014-9-19  Nanjing,njupt,China */public class ListAdapter extends BaseAdapter {private List<Model> datas;private Context context;private UpdateCallback updateCallback;public UpdateCallback getUpdateCallback() {return updateCallback;}public void setUpdateCallback(UpdateCallback updateCallback) {this.updateCallback = updateCallback;}public ListAdapter(Context context) {datas = new ArrayList<Model>();this.context = context;}public void addData(Model model) {datas.add(model);notifyDataSetChanged();}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn datas.size();}@Overridepublic Object getItem(int pos) {// TODO Auto-generated method stubreturn datas.get(pos);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(final int pos, View convertView, ViewGroup viewGroup) {final Model model = datas.get(pos);ViewHolder viewHolder = null;if (null == convertView) {viewHolder = new ViewHolder();convertView = LayoutInflater.from(context).inflate(R.layout.list_item_layout, null);viewHolder.pb = (ProgressBar) convertView.findViewById(R.id.pb_show);viewHolder.btn = (Button) convertView.findViewById(R.id.btn);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();convertView.setTag(viewHolder);}viewHolder.btn.setText(model.getName());viewHolder.btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(null != updateCallback){updateCallback.startProgress(model,pos);}}});viewHolder.pb.setProgress(0);// cache the viewreturn convertView;}public static class ViewHolder {ProgressBar pb;Button btn;}}

Activity

Package net. mobctrl. listviewdemo; import net. mobctrl. listviewdemo. listAdapter. viewHolder; import org. androidannotations. annotations. afterViews; import org. androidannotations. annotations. EActivity; import org. androidannotations. annotations. uiThread; import org. androidannotations. annotations. viewById; import android. app. activity; import android. view. view; import android. widget. listView;/*** @ author Zheng Haibo * @ webset http://www.mobctrl.net * Partial refresh of ListView */@ EActivity (R. layout. activity_main) public class MainActivity extends Activity implements UpdateCallback {@ ViewByIdListView; private ListAdapter adapter; @ AfterViewsvoid afterViews () {adapter = new ListAdapter (this); adapter. setUpdateCallback (this); listview. setAdapter (adapter); initDatas ();} private void initDatas () {for (int I = 0; I <100; I ++) {Model model = new Model (I, "<Click> -->"); adapter. addData (model) ;}@ Overridepublic void startProgress (final Model model, final int position) {/** start the Thread to update the Progress */new Thread (new Runnable () {@ Overridepublic void run () {for (int I = 0; I <= 100; I ++) {updateProgressInUiThread (model, I, position); try {Thread. sleep (50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}). start () ;}@ UiThreadvoid updateProgressInUiThread (Model model, int progress, int position) {updateProgressPartly (progress, position);} private void updateProgressPartly (int progress, int position) {int firstVisiblePosition = listview. getFirstVisiblePosition (); int lastVisiblePosition = listview. getLastVisiblePosition (); if (position> = firstVisiblePosition & position <= lastVisiblePosition) {View view = listview. getChildAt (position-firstVisiblePosition); if (view. getTag () instanceof ViewHolder) {ViewHolder h_= (ViewHolder) view. getTag (); Vl. pb. setProgress (progress );}}}}


Layout:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="40dp"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:gravity="center"        android:text="test" />    <ProgressBar        android:id="@+id/pb_show"        style="@android:style/Widget.Holo.ProgressBar.Horizontal"        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        android:layout_toRightOf="@id/btn"        android:background="@android:color/background_light"        android:max="100"        android:progress="0" /></RelativeLayout>

Source code for the entire project: https://github.com/nuptboyzhb/ListViewPartRefreash

Effect:


The ListView can be refreshed normally no matter how it slides up or down.



Not for commercial purposes without permission

Welcome to the QQ Group Discussion: android Development Alliance:272209595





How to refresh the view in Android (not listView)

Do not update the UI in the main thread. The UI has a high priority and must be updated using the handler thread.
 
How to refresh the android ListView

Call the notifyDataSetChanged () method of your adapter after returning it. It refreshes the listview when the data in the listview changes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.