Listview, gridview partial refresh, partial refresh, listviewgridview

Source: Internet
Author: User

Listview, gridview partial refresh, partial refresh, listviewgridview

As we all know, the way to refresh the Listview and Gridview interfaces is to call adapter. notifyDataSetChanged () to refresh the interfaces.

However, this method has its drawbacks. It refreshes all the data on the interface, regardless of whether the data has changed. If the listview loads a lot of data (for example, 100 rows)

During refresh, a large amount of system overhead will be incurred. How can I just refresh one refresh like the personal dynamics in the QQ space:

Principle:

Refreshes an item of listview.

1. Obtain the current index position and data of the item to be refreshed

2. Reset the obtained data

3. Place the reset data to the original position of the dataset in the adapter (refresh a piece of data in the original dataset based on position)

4. Obtain the view of the child item to be refreshed in listview.

5. obtain new data from the updated data set and update the data in viwe (Operations in handler to refresh the interface)

Function:

Public class MainActivity extends Activity {private ArrayList <MyListItem> list = null; private ListView lv; private MyListAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); intitData (); lv = (ListView) findViewById (R. id. listView1); adapter = new MyListAdapter (list, getApplicationContext (); adap Ter. setListView (lv); lv. setAdapter (adapter); lv. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// obtain the data of the clicked item in listview. MyListItem item = (MyListItem) parent. getItemAtPosition (position); Log. I ("eee", item. getData () + "=" + item. getPosition (); // update the data item. setData ("update item" + position); // update the adapter. updateItemData (item) ;}}) ;}/ ***** initialize data */private void intitData () {list = new ArrayList <MyListItem> (); for (int I = 0; I <20; I ++) {MyListItem item = new MyListItem (); item. setData ("item" + I); item. setPosition (I); list. add (item) ;}/ *** custom item data type */class MyListItem {/*** data id */private int dataId; /*** data */private String data; public int getPosition () {return dataId;} public void setPosition (int position) {this. dataId = position;} public String getData () {return data;} public void setData (String data) {this. data = data ;}}}
Activity is called. The function operations are encapsulated in the adapter as follows:

Public class MyListAdapter extends BaseAdapter {/*** dataset in listview */private ArrayList <MyListItem> mDataList; private Context mContext; private ListView mListView; public MyListAdapter (ArrayList <MyListItem> list, context cont) {this. mDataList = list; this. mContext = cont;}/*** sets the listview object ** @ param lisv */public void setListView (ListView lisv) {this. mListView = lisv;}/*** update listview single data ** @ param item new data object */public void updateItemData (MyListItem item) {Message msg = Message. obtain (); int ids =-1; // compare the data to obtain the position of the corresponding data in the list. for (int I = 0; I <mDataList. size (); I ++) {if (mDataList. get (I ). getPosition () = item. getPosition () {ids = I ;}} msg. arg1 = ids; // update the data in the mDataList location. set (ids, item); // handle refresh the page han. sendMessage (msg) ;}@ SuppressLint ("HandlerLeak") private Handler han = new Handler () {public void handleMessage (android. OS. message msg) {updateItem (msg. arg1) ;};};/*** refresh the position of the specified item ** @ param index item in listview */private void updateItem (int index) {if (mListView = null) {return;} // obtain the currently visible item Position int visiblePosition = mListView. getFirstVisiblePosition (); // If headerview is added, firstview indicates hearderview. // All indexes + 1. Obtain the first view. // View view = listview. getChildAt (index-visiblePosition + 1); // obtain the clicked view = mListView. getChildAt (index-visiblePosition); TextView txt = (TextView) view. findViewById (R. id. textView1); // get mDataList. set (ids, item); updated data MyListItem data = (MyListItem) getItem (index); // reset the data display txt. setText (data. getData () ;}@ Override public int getCount () {// TODO Auto-generated method stub return mDataList. size () ;}@ Override public Object getItem (int position) {// TODO Auto-generated method stub return mDataList. get (position) ;}@ Override public long getItemId (int position) {// TODO Auto-generated method stub return position ;}@ Override public View getView (int position, View convertView, viewGroup parent) {// TODO Auto-generated method stub if (convertView = null) {convertView = LayoutInflater. from (mContext ). inflate (R. layout. list_item, null);} TextView txt = (TextView) convertView. findViewById (R. id. textView1); txt. setText (mDataList. get (position ). getData (); return convertView ;}}

Because listview is similar to the gridview function, the display mode is different, and the principle is the same. You can modify the function and try it by yourself.

Source code download

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.