Android custom controls (4) Pull and load more on the Netease client, android Netease

Source: Internet
Author: User

Android custom controls (4) Pull and load more on the Netease client, android Netease

In the previous article, the drawer mode of the webpage client was imitated. This article continues, to write a write to load more this function, and to load more through custom implementation, first:


Today we are implementing the following 20 loading... this function!

Let's start with the following ideas:

1. Add 20 loaded la s to the listview and hide them.

2. added the OnScrollListener listener to listen for Rolling events. When scrolling to the lowest end, the above layout is displayed.

3. implement more functions through interface callback

4. When data is loaded, the listview is notified that the loading is complete and the preceding layout file is hidden.

The following code is directly used:

1. Add 20 loaded la s to the listview and hide them.

LayoutInflater inflater = LayoutInflater.from(context);footView = inflater.inflate(R.layout.foot_layout, null);addFooterView(footView);footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);

2. added the OnScrollListener listener to listen for Rolling events. When scrolling to the lowest end, the above layout is displayed.

@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {if (lastItem == totalItemCount && scrollState == SCROLL_STATE_IDLE) {if (!isLoading) {isLoading=true;footView.findViewById(R.id.foot_layout).setVisibility(View.VISIBLE);isLoadingListener.onLoad();}}}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {lastItem = firstVisibleItem + visibleItemCount;this.totalItemCount = totalItemCount;}

3. implement more functions through interface callback

public void setOnLoadingListener(IsLoadingListener isLoadingListener){this.isLoadingListener=isLoadingListener;}public interface IsLoadingListener{public void onLoad();}

@ Overridepublic void onLoad () {handler. postDelayed (new Runnable () {@ Overridepublic void run () {list. add ("dad"); list. add ("mom"); list. add ("I"); adapter. yydatasetchanged (); listView. complateLoad () ;}}, 3000 );}


4. When data is loaded, the listview is notified that the loading is complete and the preceding layout file is hidden.

public void complateLoad(){isLoading=false;footView.findViewById(R.id.foot_layout).setVisibility(View.GONE);}
OK. These are the custom controls. The complete code is as follows:

Package com. sdufe. thea. guo. view; import com. sdufe. thea. guo. r; import android. content. context; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. widget. absListView; import android. widget. listView; import android. widget. absListView. onScrollListener; public class ListViewLoadMore extends ListView implements OnScrollListener {View footView; int lastIte M; // The Last int totalItemCount; // The total number of boolean isLoading = false at the moment; IsLoadingListener isLoadingListener; public ListViewLoadMore (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); initView (context);} public ListViewLoadMore (Context context, AttributeSet attrs) {super (context, attrs); initView (context );} public ListViewLoadMore (Context context) {super (context); initView (con Text);}/*** initialize footView ** @ param context */void initView (Context context) {LayoutInflater inflater = LayoutInflater. from (context); footView = inflater. inflate (R. layout. foot_layout, null); addFooterView (footView); footView. findViewById (R. id. foot_layout ). setVisibility (View. GONE); setOnScrollListener (this) ;}@ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {if (lastItem = t OtalItemCount & scrollState = SCROLL_STATE_IDLE) {if (! IsLoading) {isLoading = true; footView. findViewById (R. id. foot_layout ). setVisibility (View. VISIBLE); isLoadingListener. onLoad () ;}}@ Overridepublic void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {lastItem = firstVisibleItem + visibleItemCount; this. totalItemCount = totalItemCount;} public void setOnLoadingListener (IsLoadingListener isLoadingListener) {this. isLoadingListener = isLoadingListener;} public interface IsLoadingListener {public void onLoad ();} public void complateLoad () {isLoading = false; footView. findViewById (R. id. foot_layout ). setVisibility (View. GONE );}}

Main Interface layout:

<LinearLayout 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">    <com.sdufe.thea.guo.view.ListViewLoadMore        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:divider="@null"/></LinearLayout>

Main Interface code:

Package com. sdufe. thea. guo; import java. util. arrayList; import java. util. list; import com. sdufe. thea. guo. view. listViewLoadMore; import com. sdufe. thea. guo. view. listViewLoadMore. isLoadingListener; import android. OS. bundle; import android. OS. handler; import android. app. activity; import android. view. menu; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends Activity implements IsLoadingListener {private ListViewLoadMore listView; private List <String> list; private ArrayAdapter <String> adapter; private Handler handler = new Handler (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListViewLoadMore) findViewById (R. id. listview); initData (); adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, list); listView. setAdapter (adapter); listView. setOnLoadingListener (this);}/*** initialize list Value */private void initData () {list = new ArrayList <String> (); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789"); list. add ("123456789") ;}@ Overridepublic void onLoad () {handler. postDelayed (new Runnable () {@ Overridepublic void run () {list. add ("dad"); list. add ("mom"); list. add ("I"); adapter. yydatasetchanged (); listView. complateLoad (); }}, 3000 );}}

If you do not understand the message, try your best to answer you!


Csnd code: http://download.csdn.net/detail/elinavampire/8204105

Github: https://github.com/zimoguo/PullToRefreshLoadMore

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.