Android -- ListView pull-down refresh, android -- listview

Source: Internet
Author: User

Android -- ListView pull-down refresh, android -- listview

I sorted out the previously written small projects and refreshed the ListView drop-down list. Although it is small, I still want to remember it .. It is suitable for beginners to see...

:

 

Code: entity class
Package com. example. listviewrefreshdemo;/*** @ author ultra boy */public class ApkEntity {private String name; private String des; private String info; public String getName () {return name ;} public void setName (String name) {this. name = name;} public String getDes () {return des;} public void setDes (String des) {this. des = des;} public String getInfo () {return info;} public void setInfo (String info) {this.info = info ;}}

MainActivity:

Package com. example. listviewrefreshdemo; import java. util. arrayList; import com. example. listviewrefreshdemo. refreshListView. IReflashListener; import android. app. activity; import android. OS. bundle; import android. OS. handler;/*** @ author ultra boy */public class MainActivity extends Activity implements IReflashListener {ArrayList <ApkEntity> apk_list; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); setData (); showList (apk_list);} MyAdapter adapter; RefreshListView listview; private void showList (ArrayList <ApkEntity> apk_list) {if (adapter = null) {listview = (RefreshListView) findViewById (R. id. listview); listview. setIReflashListener (this); adapter = new MyAdapter (this, apk_list); listview. setAdapter (adapter);} else {adapter. onDateChange (apk_list) ;}} private void setData () {apk_list = new ArrayList <ApkEntity> (); for (int I = 0; I <10; I ++) {ApkEntity entity = new ApkEntity (); entity. setName ("default data"); entity. setDes ("This is a magic application"); entity. setInfo ("50w user"); apk_list.add (entity) ;}} private void setReflashData () {for (int I = 0; I <2; I ++) {ApkEntity entity = new ApkEntity (); entity. setName ("refresh data"); entity. setDes ("This is a magic application"); entity. setInfo ("50w user"); apk_list.add (I, entity) ;}@overridepublic void onReflash () {// TODO Auto-generated method stub \ Handler handler = new Handler (); handler. postDelayed (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stub // get the latest data setReflashData (); // showList (apk_list) is displayed on the notification page. // It notifies listview that data is refreshed. listview. reflashComplete () ;}}, 2000) ;}/ *** Return Method * // * public void onReflash () {// get the latest data setReflashData (); // showList (apk_list) is displayed on the notification page. // It notifies listview that the data has been refreshed. reflashComplete ();}*/}

RefreshListView:

Package com. example. listviewrefreshdemo; import java. text. simpleDateFormat; import java. util. date; import android. r. anim; import android. r. interpolator; import android. content. context; import android. util. attributeSet; import android. util. log; import android. view. layoutInflater; import android. view. motionEvent; import android. view. view; import android. view. viewGroup; import android. view. animation. rotateAnimati On; import android. widget. absListView; import android. widget. imageView; import android. widget. listView; import android. widget. absListView. onScrollListener; import android. widget. progressBar; import android. widget. textView;/*** @ author ultra boy */public class RefreshListView extends ListView implements OnScrollListener {LayoutInflater inflater; View header; int headerHeight; // the height of the top layout file int firstVisibleItem ;/ /The Position of the first visible item is int scrollState; // The current rolling status of listview boolean isRemark; // marker, which is the int startY at the top of listview; // int state of the Y value when the press is down; // final int NONE = 0; // normal state; final int PULL = 1; // The drop-down state is displayed; final int RELESE = 2; // prompt release status; final int REFLASHING = 3; // refresh status; IReflashListener reflashListener; public RefreshListView (Context context) {super (context ); initView (context);} public RefreshListView (Context context, Att RibuteSet attrs, int defStyle) {super (context, attrs, defStyle); initView (context);} public RefreshListView (Context context, AttributeSet attrs) {super (context, attrs ); initView (context);}/*** initialization interface, add the top layout file to ListView * @ param context */private void initView (Context context) {inflater = LayoutInflater. from (context); header = inflater. inflate (R. layout. header, null); measureView (header); headerHeight = hea Der. getMeasuredHeight (); Log. I ("tag", "headerHeight =" + headerHeight); topPadding (-headerHeight); // negative height, can you leave it blank. this. addHeaderView (header); this. setOnScrollListener (this);}/*** notifies the parent layout of the layout, which occupies a wide height. * @ Param view */private void measureView (View view) {ViewGroup. layoutParams p = view. getLayoutParams (); if (p = null) {p = new ViewGroup. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. WRAP_CONTENT);} // spec: header padding, padding, and childDimension. int width = ViewGroup. getChildMeasureSpec (0, 0, p. width); int height; int tempHeight = p. height; if (tempHeight> 0) {// The height is not It is null and needs to fill in the layout // EXACTLY (full). The parent element determines the exact size of the Self-element, and the child element will be limited to the given boundary while ignoring its own size; height = MeasureSpec. makeMeasureSpec (tempHeight, MeasureSpec. EXACTLY);} else {// UNSPECIFIED (UNSPECIFIED). The parent element does not apply any constraints to the child element, and the child element can obtain any desired size. height = MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED);} view. measure (width, height);}/*** sets the top margin of the header Layout * @ param topPadding */private void topPadding (int topPadding) {// header. setPaddi Ng (header. getPaddingLeft (), topPadding, header. getPaddingRight (), header. getPaddingBottom (); header. invalidate ();}/*** get the current rolling status */public void onScrollStateChanged (AbsListView view, int scrollState) {this. scrollState = scrollState;} @ Overridepublic void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {this. firstVisibleItem = firstVisibleItem; // the first visible I Location of tem}/*** touch event */public boolean onTouchEvent (MotionEvent ev) {switch (ev. getAction () {case MotionEvent. ACTION_DOWN: // drop-down if (firstVisibleItem = 0) {// isRemark = true at the top of the interface; // mark, startY = (int) ev at the top of listview. getY ();} break; case MotionEvent. ACTION_MOVE: onMove (ev); break; case MotionEvent. ACTION_UP: // lift if (state = RELESE) {// release state = REFLASHING; // refreshing // load the latest data; reflashViewByState (); ReflashListener. onReflash ();} else if (state = PULL) {// when the drop-down process is not a certain distance, state = NONE; // normal state; isRemark = false; // mark reflashViewByState ();} break;} return super. onTouchEvent (ev);}/*** operation for judging the movement process * @ param ev */private void onMove (MotionEvent ev) {if (! IsRemark) {// move return when it is not at the top;} // The status changes continuously in the process of moving int tempY = (int) ev. getY (); int sapce = tempY-startY; // The distance from int topPadding = sapce-headerHeight; // you can set topPaddingswitch (state) {case NONE: // normal status; if (sapce> 0) {state = PULL; // a prompt is displayed for the drop-down status reflashViewByState () ;}break; case PULL: // The drop-down status is displayed; topPadding (topPadding); // keep setting the top margin if (sapce> headerHeight + 30 & scrollState = SCROLL_STATE_TOUCH_SCROLL) {state = RELESE; // a message indicating the release status is displayed; reflashViewByState () ;}break; case RELESE: // prompts the release status; topPadding (topPadding); if (sapce 

Adapter:

Package com. example. listviewrefreshdemo; import java. util. arrayList; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. textView;/*** @ author ultra boy */public class MyAdapter extends BaseAdapter {ArrayList <ApkEntity> apk_list; LayoutInflater inflater; public MyAdapter (Context context, ArrayList <ApkEntity> apk_list) export this.apk _ list = apk_list; this. inflater = LayoutInflater. from (context);} public void onDateChange (ArrayList <ApkEntity> apk_list) Export this.apk _ list = apk_list; this. notifyDataSetChanged (); // update data} @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn apk_list.size () ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn apk_list.get (position) ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position ;} @ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubApkEntity entity = apk_list.get (position); ViewHolder holder; if (convertView = null) {holder = new ViewHolder (); convertView = inflater. inflate (R. layout. item_layout, null); holder. name_ TV = (TextView) convertView. findViewById (R. id. item3_apkname); holder. des_ TV = (TextView) convertView. findViewById (R. id. item3_apkdes); holder.info _ TV = (TextView) convertView. findViewById (R. id. item3_apkinfo); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. name_ TV .setText (entity. getName (); holder. des_ TV .setText (entity. getDes (); holder.info _ TV. setText (entity. getInfo (); return convertView;} class ViewHolder {TextView name_ TV; TextView des_ TV; TextView info_ TV ;}}

Activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.listviewrefreshdemo.MainActivity" >    <com.example.listviewrefreshdemo.RefreshListView        android:id="@+id/listview"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@color/white"        android:cacheColorHint="#00000000"        android:dividerHeight="5dip" /></RelativeLayout>

The layout file also contains header. xml and item_layout.xml, so you won't paste them. If you want to see them, you can go to my source code and download the link below ..

OK .. The code is very simple...

Demo source code download: http://download.csdn.net/detail/jycboy/9588195

Attached a good blog: Android pull-down refresh Framework Implementation: http://blog.csdn.net/leehong2005/article/details/12567757

Please indicate the source of forwarding: http://www.cnblogs.com/jycboy/p/5712620.html

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.