Android PullToRefresh implements automatic pull-down loading and refresh, and Android pulltorefresh

Source: Internet
Author: User

Android PullToRefresh implements automatic pull-down loading and refresh, and Android pulltorefresh

Yesterday, the tester raised a Bug saying that as long as the same Tab button is clicked back and forth, the loaded data will be repeated. After checking the code for a long time, no problems are found, therefore, the breakpoint debugging is triggered and the pullToRefreshListView is executed. setRefreshing (); the refresh operation that you think is performed (that is, the refresh operation, but the pull-up operation is a refresh operation, and the pull-down operation is also a refresh operation, we changed the pull-up refresh to load more). In fact, this control does not determine whether the current operation is pull-down refresh or pull-up loading, in this case, you need to customize a Boolean variable isAutoRefresh to identify whether it is a pull-down refresh action. The source code is provided below:

Public class extends Fragment implements listener, OnItemClickListener, OnRefreshListener <ListView>, listener {private int mType = 0; private callback listener; private ListView actualListView; private MyTopicListAdapter myTopicListAdapter; private Context mContext; private MyTopicLogic logic; private int currentPageCount = 0; // the current page pri of the record Vate int totalPageCount; // the total number of pages recorded. private boolean isRefreshAction; // indicates whether the private MyTopicEntity mMyTopicEntity is refreshed from the drop-down list; private LinearLayout promptView; // private LinearLayout mLoadingHint = null; private LinearLayout mAfterLodingHint = null; private ImageView mHintImage = null; private TextView mHintTxt = null; private boolean isAutoRefresh = false; @ Overridepublic View onCreateView (LayoutInfl Ater inflater, ViewGroup container, Bundle savedInstanceState) {Bundle args = this. getArguments (); mType = args. getInt ("type"); mContext = this. getActivity (); logic = new MyTopicLogic (); logic. addObserver (this); View rootView = inflater. inflate (R. layout. fragment_pulltorefresh_listview, container, false); initialize (rootView); return rootView;} private void initialize (View rootView) {pullToRefreshListView = (PullToRefreshListView) rootView. findViewById (R. id. fragment_pulltorefresh_listview); myTopicListAdapter = new MyTopicListAdapter (); actualListView = pullToRefreshListView. getRefreshableView (); registerForContextMenu (actualListView); actualListView. setAdapter (myTopicListAdapter); pullToRefreshListView. setMode (Mode. BOTH); pullToRefreshListView. getLoadingLayoutProxy (false, true ). setPullLabel (getString (R. s Tring. poll_refresh_down_loadmore); pullToRefreshListView. getLoadingLayoutProxy (false, true ). setRefreshingLabel (getString (R. string. poll_refresh_down_refreshing); pullToRefreshListView. getLoadingLayoutProxy (false, true ). setReleaseLabel (getString (R. string. poll_refresh_down_release); pullToRefreshListView. getLoadingLayoutProxy (false, true ). setLastUpdatedLabel (""); pullToRefreshListView. setOnRefreshList Ener (this); pullToRefreshListView. listener (this); pullToRefreshListView. setOnItemClickListener (this); mMyTopicEntity = HealthApplication. mAPPCache. getMyTopic (mType); if (mMyTopicEntity! = Null & mMyTopicEntity. getData ()! = Null) {myTopicListAdapter. refresh (mMyTopicEntity. getData (). getData (); currentPageCount = 1;} promptView = (LinearLayout) LayoutInflater. from (mContext ). inflate (R.layout.net _ request_prompt, null); pullToRefreshListView. setEmptyView (promptView); if (mType = 1) {// by default, all my published topics are displayed. sendEmptyMessageDelayed (1,500); // logic. getMyTopic (10, 1, mType, "time");}/** else if (mType = 2) {logic. getMyTopic (1 0, 1, mType, "time");} else * if (mType = 3) {logic. getMyTopic (10, 1, mType, "time");} * // mType = 1; // set to 1, as long as it is published by me, I have not clicked my reply and my favorites, and the refresh operation will execute my published topic} Handler mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {super. handleMessage (msg); switch (msg. what) {case 1: // pullToRefreshListView. setCurrentModeRefreash (); isAutoRefresh = true; pullToRefreshListView. setRefreshing (); break; d Efault: break ;}};@ Overridepublic void onPause () {logic. removeObserver (this); super. onPause () ;}@ Overridepublic void onResume () {if (logic. getObservers (). size () = 0) {logic. addObserver (this);} super. onResume () ;}@ Overridepublic void onItemClick (AdapterView <?> ListView, View itemView, int position, long itemViewId) {MyTopicDetail item = (MyTopicDetail) myTopicListAdapter. getItem (position-1); Intent intent = new Intent (mContext, TopicDetailActivity. class); intent. putExtra ("topicId", item. getId (); startActivityForResult (intent, 18) ;}@ Overridepublic void onLastItemVisible () {if (pullToRefreshListView. getMode () = Mode. PULL_FROM_START) {// Toast. makeText (mContext, "No more topics", Toast. LENGTH_SHORT ). show () ;}@ Overridepublic void onRefresh (PullToRefreshBase <ListView> refreshView) {if (refreshView. isHeaderShown () | isAutoRefresh) {isAutoRefresh = false; isRefreshAction = true; logic. getMyTopic (10, 1, mType, "time");} else {isRefreshAction = false; logic. getMyTopic (10, currentPageCount + 1, mType, "time") ;}} public void refresh (int type) {isRefreshAction = true; mType = type; mHandler. sendEmptyMessageDelayed (1,500); // logic. getMyTopic (10, 1, mType, "time");} private class MyTopicListAdapter extends BaseAdapter {private ArrayList <MyTopicDetail> mList; public MyTopicListAdapter () {mList = new ArrayList <MyTopicDetail> ();} public void refresh (ArrayList <MyTopicDetail> arrayList) {mList. clear (); mList = arrayList; notifyDataSetChanged ();} public void add (ArrayList <MyTopicDetail> arrayList) {mList. addAll (arrayList); notifyDataSetChanged ();} public void clear () {mList. clear (); notifyDataSetChanged () ;}@ Overridepublic int getCount () {return mList. size () ;}@ Overridepublic Object getItem (int position) {return mList. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHolder viewHolder = null; if (convertView = null) {LayoutInflater inflater = LayoutInflater. from (mContext); convertView = inflater. inflate (R. layout. mytopic_list_item, null); viewHolder = new ViewHolder (); viewHolder. topicName = (TextView) convertView. findViewById (R. id. TV _topic_name); viewHolder. topicContent = (TextView) convertView. findViewById (R. id. TV _topic_content); viewHolder. coloumnName = (TextView) convertView. findViewById (R. id. column); viewHolder. topicTime = (TextView) convertView. findViewById (R. id. sendtime); viewHolder. topicGoodCnt = (TextView) convertView. findViewById (R. id. TV _good); viewHolder. topicReplyCnt = (TextView) convertView. findViewById (R. id. TV _replay); convertView. setTag (viewHolder);} else {viewHolder = (ViewHolder) convertView. getTag ();} MyTopicDetail item = (MyTopicDetail) getItem (position); if (item. getTitle (). length ()> 30) {String str = item. getTitle (). substring (0, 30); viewHolder. topicName. setText (str + "... ");} else {viewHolder. topicName. setText (item. getTitle ();} if (item. getContent (). length ()> 80) {String temp = (item. getContent (). substring (0, 80); viewHolder. topicContent. setText (temp + "... ");} else {viewHolder. topicContent. setText (item. getContent ();} if (item. getSectname (). length ()> 8) {String temp = (item. getSectname (). substring (0, 6); viewHolder. coloumnName. setText (temp + "... ");} else {viewHolder. coloumnName. setText (item. getSectname ();} viewHolder. topicTime. setText (item. getCreate_time (); viewHolder. topicGoodCnt. setText (item. getGoodcnt () + ""); viewHolder. topicReplyCnt. setText (item. getDiscusscnt () + ""); return convertView;} private class ViewHolder {public TextView topicName; // topic name public TextView topicContent; // topic content public TextView coloumnName; // column name public TextView topicTime; // topic time public TextView topicGoodCnt; // Number of likes public TextView topicReplyCnt; // number of replies }}@ Overridepublic void onGetMyTopicSuccess (MyTopicEntity result) {pullToRefreshListView. onRefreshComplete (); if (result. getData () = null) {showPromptView (false, getString (R. string. topic_nodata), 1); myTopicListAdapter. clear (); pullToRefreshListView. setMode (Mode. BOTH); currentPageCount = 0; HealthApplication. mAPPCache. setMyTopic (mType, ""); showPromptView (false, getResources (). getString (R. string. temp_no_data), 1);} totalPageCount = result. getData (). getPagecount (); if (isRefreshAction) {myTopicListAdapter. refresh (result. getData (). getData (); currentPageCount = 1; String label = DateUtils. formatDateTime (mContext, System. currentTimeMillis (), DateUtils. FORMAT_SHOW_TIME | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_ABBREV_ALL); pullToRefreshListView. getLoadingLayoutProxy (true, false ). setLastUpdatedLabel (label); if (1 = totalPageCount) {pullToRefreshListView. setMode (Mode. PULL_FROM_START);} else {pullToRefreshListView. setMode (Mode. BOTH) ;}} else {myTopicListAdapter. add (result. getData (). getData (); currentPageCount ++; if (currentPageCount> = totalPageCount) {pullToRefreshListView. setMode (Mode. PULL_FROM_START) ;}}@ Overridepublic void onGetMyTopicFailed (int errorCode, String msg) {pullToRefreshListView. onRefreshComplete (); if (errorCode = 17) {showPromptView (false, getString (R. string. topic_nodata), 1); myTopicListAdapter. clear (); pullToRefreshListView. setMode (Mode. BOTH); currentPageCount = 0; HealthApplication. mAPPCache. setMyTopic (mType, ""); showPromptView (false, getResources (). getString (R. string. temp_no_data), 1);} else {HaloToast. show (mContext, msg); showPromptView (false, getResources (). getString (R. string. req_fail_click_refresh), 2 );}} /*** prompts after loading and loading ** @ param isProgressShow * Whether to display the text in the Progress box * @ param text * @ param type * to control which layout to display: 0: The ID is being loaded. 1: The request is successful, but no data is available. 2: The request failed. */private void showPromptView (boolean isProgressShow, String text, int type) {mAfterLodingHint = (LinearLayout) promptView. findViewById (R. id. after_loding_hint_layout); mLoadingHint = (LinearLayout) promptView. findViewById (R. id. loding_hint); mHintImage = (ImageView) promptView. findViewById (R. id. hint_image); mHintTxt = (TextView) promptView. findViewById (R. id. hint_txt); View progressBar = promptView. findViewById (R.id.net _ request_prompt_progressBar); TextView promptTextView = (TextView) promptView. findViewById (R.id.net _ request_prompt_textView); promptView. setClickable (false); if (type = 0) {mLoadingHint. setVisibility (View. VISIBLE); mAfterLodingHint. setVisibility (View. GONE); if (isProgressShow) {progressBar. setVisibility (View. VISIBLE);} else {progressBar. setVisibility (View. GONE);} promptTextView. setText (text);} else if (type = 1) {mLoadingHint. setVisibility (View. GONE); mHintImage. setClickable (false); mHintImage. setImageResource (R. drawable. no_data); mHintTxt. setText (text); mAfterLodingHint. setVisibility (View. VISIBLE);} else if (type = 2) {promptView. setClickable (true); mLoadingHint. setVisibility (View. GONE); mHintImage. setImageResource (R. drawable. click_refresh_selector); mHintTxt. setText (text); mHintImage. setClickable (true); mHintImage. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {logic. getMyTopic (10, 1, mType, "time") ;}}); mAfterLodingHint. setVisibility (View. VISIBLE) ;}@ Overridepublic void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = 18 & resultCode = getActivity (). RESULT_ OK) {isRefreshAction = true; logic. getMyTopic (10, 1, mType, "time");} super. onActivityResult (requestCode, resultCode, data );}}


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.