When I used to like reading novels, I downloaded some reading software on my mobile phone. The software has a very dazzling function of adding bookmarks. The method of refreshing the information list on Weibo is also cool.
The following uses the listview drop-down refresh list as an example to describe how to add bookmarks and refresh listview by means of a drop-down or pull-up operation.
First paste:
In ListView, there are addHeaderView () and addFooterView () methods. You can place two different or identical custom views on the top and bottom of the listview as the item element of the ListView; to add these two views, you must. the setAdapter () method can be used before. After the layout at the top and bottom is completed, you need to implement the pull-down refresh function. Each View object has the setPadding () method to control the distance between the view object and other controls or screen borders around it, here, we need to change the distance from the top of the screen to control the Display Effect of the top view. An "Arrow" control is defined in the top view to identify the direction, rotateAnimation animation is used to control the rotation and orientation of the "Arrow. The addition of bookmarks is similar. You can add a view to the top of the defined layout file and use setPadding () to control the view when adding bookmarks.
First, create a layout file activity_main.xml for the main ListView class.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity" > <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:divider="@android:color/transparent"/></RelativeLayout>
Only one ListView control is displayed in the layout file. When the ListView slides, the background is transparent and the line before the Item is transparent. Then, the header. xml of the Top View File is defined.
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- ListView header --> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <! -- Content --> <RelativeLayout android: id = "@ + id/head_contentLayout" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: paddingLeft = "30sp"> <! -- Arrow image and progress bar --> <FrameLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_centerVertical = "true"> <! -- Arrow --> <ImageView android: id = "@ + id/head_arrowImageView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center" android: src = "@ drawable/ic_pulltorefresh_arrow"/> <! -- Progress bar --> <ProgressBar android: id = "@ + id/head_progressBar" style = "? Android: attr/progressBarStyleSmall "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_gravity =" center "android: visibility = "gone"/> </FrameLayout> <! -- Prompt, latest update --> <LinearLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: gravity = "center_horizontal" android: orientation = "vertical"> <! -- Prompt --> <TextView android: id = "@ + id/head_tipsTextView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "pull-down refresh" android: textColor = "#000000" android: textSize = "20sp"/> <! -- Latest update --> <TextView android: id = "@ + id/head_lastUpdatedTextView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Last Update" android: textColor = "# ff0000" android: textSize = "10sp"/> </LinearLayout> </RelativeLayout> </LinearLayout>
The layout defines the "Arrow", refresh the prompt, Update time, progress bar, and other content, which can be modified as needed. Then, the bottom View File bottom. xml is defined.
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: gravity = "center_horizontal" android: layout_height = "fill_parent" android: layout_width = "fill_parent" android: orientation = "vertical" android: paddingBottom = "13sp" android: paddingTop = "13sp"> <TextView android: gravity = "center_horizontal" android: id = "@ + id/more_id" android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: text = "view more... "android: textSize =" 25sp "/> <LinearLayout android: id =" @ + id/load_id "android: layout_height =" wrap_content "android: layout_width =" wrap_content "android: orientation = "horizontal" android: visibility = "gone"> <ProgressBar android: layout_gravity = "center_horizontal" android: layout_height = "wrap_content" android: layout_width = "match_parent"/> <TextView android: gravity = "Hangzhou" android: layout_height = "fill_parent" android: layout_marginLeft = "10sp" android: layout_width = "wrap_content" android: text = "loading... "android: textSize =" 20sp "/> </LinearLayout>
Now, you can use MainActivity. java to bind top files, master files, and bottom files together for refresh and pop-up effects.
Package com. example. pulldownrefresh; import java. util. arrayList; import java. util. date; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. OS. asyncTask; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. util. log; import android. view. layoutInflater; import android. view. menu; import android. view. motionEvent; Import android. view. view; import android. view. viewGroup; import android. view. view. measureSpec; import android. view. view. onTouchListener; import android. view. animation. linearInterpolator; import android. view. animation. rotateAnimation; import android. view. animation. translateAnimation; import android. widget. button; import android. widget. imageView; import android. widget. linearLayout; import android. widget. list View; import android. widget. progressBar; import android. widget. simpleAdapter; import android. widget. textView; /***** @ author tr * @ time 2014-2-28 * @ description pull-down refresh list */public class MainActivity extends Activity implements OnTouchListener {/** listview Instance Object */private ListView mListView; private int headViewWidth, headViewHeight; // RATIO of actual padding distance to interface offset private final static int RATIO = 3;/** rotation animation */privat E RotateAnimation animation, reverseAnimation;/** rotating object */private ImageView head_arrowImageView; private float startY; private View headView; private SimpleAdapter adapter; private List <Map <String, object> data; private static final String TAG = "listview"; private final static int RELEASE_To_REFRESH = 0; private final static int PULL_To_REFRESH = 1; private final static int REFRESHING = 2; private final sta Tic int DONE = 3; private final static int LOADING = 4; // It is used to ensure that the value of startY is recorded only once in a complete touch event. private int firstItemIndex; private int state; private boolean isBack; private TextView tipsTextview; private TextView lastUpdatedTextView; private ProgressBar progressBar; // view more private TextView moreTextView; // loading progress bar private LinearLayout loadProgressBar; // The number of data loaded by page. private int pageSi Ze = 10; private final int pageType = 1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mListView = (ListView) findViewById (R. id. listview); mListView. setOnTouchListener (this); LayoutInflater layoutInflater = getLayoutInflater (); animation = new RotateAnimation (0,180, RotateAnimation. RELATIVE_TO_SELF, 0.5f, RotateAnimati On. RELATIVE_TO_SELF, 0.5f); animation. setFillAfter (true); animation. setDuration (200); animation. setInterpolator (new LinearInterpolator (); reverseAnimation = new RotateAnimation (180, 0, RotateAnimation. RELATIVE_TO_SELF, 0.5f, RotateAnimation. RELATIVE_TO_SELF, 0.5f); reverseAnimation. setFillAfter (true); reverseAnimation. setDuration (200); reverseAnimation. setInterpolator (new LinearInterpolator (); headView = LayoutInflater. from (this ). inflate (R. layout. head, null); progressBar = (ProgressBar) headView. findViewById (R. id. head_progressBar); tipsTextview = (TextView) headView. findViewById (R. id. head_tipsTextView); lastUpdatedTextView = (TextView) headView. findViewById (R. id. head_lastUpdatedTextView); data = initValue (1, 15); adapter = new SimpleAdapter (this, data, android. r. layout. simple_expandable_list_item_2, new String [] {"title", "text"}, new int [] {android. r. id. text1, android. r. id. text2}); head_arrowImageView = (ImageView) headView. findViewById (R. id. head_arrowImageView); head_arrowImageView.setMinimumHeight (50); head_arrowImageView.setMinimumWidth (70); measureView (headView); addPageMore (); headViewWidth = headView. getMeasuredWidth (); headViewHeight = headView. getMeasuredHeight (); headView. setPadding (0,-1 * headVi EwHeight, 0, 0); headView. invalidate (); mListView. addHeaderView (headView, null, false); mListView. setAdapter (adapter);}/*** Add "load more" in ListView */private void addPageMore () {View view = LayoutInflater. from (this ). inflate (R. layout. bottom, null); moreTextView = (TextView) view. findViewById (R. id. more_id); loadProgressBar = (LinearLayout) view. findViewById (R. id. load_id); moreTextView. setOnClickListener (new Butt On. onClickListener () {@ Override public void onClick (View v) {// hide "load more" moreTextView. setVisibility (View. GONE); // display the progress bar loadProgressBar. setVisibility (View. VISIBLE); new refreshtask(cmd.exe cute () ;}}); mListView. addFooterView (view);}/*** load data on the next page * @ param pageStart * @ param pageSize */private void chageListView (int pageStart, int pageSize) {List <Map <String, Object> data = initValue (pageStart, pageS Ize); for (Map <String, Object> map: data) {this. data. add (map) ;}data = null ;}// this method is directly copied from a drop-down refresh demo on the network, here is the "Estimate" headView width and heightprivate void measureView (View child) {ViewGroup. layoutParams p = child. getLayoutParams (); if (p = null) {p = new ViewGroup. layoutParams (ViewGroup. layoutParams. FILL_PARENT, ViewGroup. layoutParams. WRAP_CONTENT);} int childWidthSpec = ViewGroup. getChildMeasureSpec (0, 0 + 0, p. width); int lpHeight = p. height; int childHeightSpec; if (lpHeight> 0) {childHeightSpec = MeasureSpec. makeMeasureSpec (lpHeight, MeasureSpec. EXACTLY);} else {childHeightSpec = MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED);} child. measure (childWidthSpec, childHeightSpec);}/*** simulate data paging loading, * @ param pageStart start Number * @ param pageSize the number displayed on each page * @ return */public static List <Map <String, Object> InitValue (int pageStart, int pageSize) {Map <String, Object> map; List <Map <String, Object> list = new ArrayList <Map <String, object >>> (); for (int I = 0; I <pageSize; I ++) {map = new HashMap <String, Object> (); map. put ("text", "define listview List"); map. put ("title", "+" + pageStart + "item"); ++ pageStart; list. add (map) ;}return list ;}@ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: if (firstItemIndex = 0 &&! IsRecored) {isRecored = true; startY = (int) event. getY (); Log. v (TAG, "record current position when down '" + startY);} break; case MotionEvent. ACTION_UP: if (state! = REFRESHING & state! = LOADING) {if (state = DONE) {// nothing else} if (state = PULL_To_REFRESH) {state = DONE; changeHeaderViewByState (); Log. v (TAG, "Refresh from drop-down to done status");} if (state = RELEASE_To_REFRESH) {state = REFRESHING; changeHeaderViewByState (); Log. v (TAG, "from release refresh status to done status") ;}} isRecored = false; isBack = false; break; case MotionEvent. ACTION_MOVE: int tempY = (int) event. getY (); if (! IsRecored & firstItemIndex = 0) {Log. v (TAG, "position recorded during move" + tempY); isRecored = true; startY = tempY;} if (state! = REFRESHING & isRecored & state! = LOADING) {// ensure that the current position is always in the head during the padding setting process. Otherwise, if the list exceeds the screen, when pushing, the list will scroll at the same time. // you can easily refresh the if (state = RELEASE_To_REFRESH) {mListView. setSelection (0); // pushed up to the extent that the screen is sufficient to cover up the head, but not to the full extent that if (tempY-startY) /RATIO The bookmarks function can be developed based on similar principles, and the source code will be provided later.
Source code, click to download