android adapterview怎么监听滑动到底部
2016-05-24 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517183
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
main.xml如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
listviewitem.mxl如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="80dip"
/>
</RelativeLayout>
MainActivity如下:
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView mListView;
private SimpleAdapter mSimpleAdapter;
private HashMap<String, Object> mHashMap;
private ArrayList<HashMap<String, Object>> mArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mListView=(ListView) findViewById(R.id.listView);
mArrayList=new ArrayList<HashMap<String,Object>>();
addDataForListView();
mSimpleAdapter=new SimpleAdapter
(MainActivity.this, mArrayList, R.layout.listviewitem, new String []{"id"},new int []{R.id.textView});
mListView.setAdapter(mSimpleAdapter);
mListView.setOnScrollListener(new OnScrollListenerImple());
}
private void addDataForListView(){
System.out.println("====>Add data to ListView");
for (int i = 0; i < 10; i++) {
mHashMap=new HashMap<String, Object>();
mHashMap.put("id", ""+i);
mArrayList.add(mHashMap);
}
}
private class OnScrollListenerImple implements OnScrollListener{
@Override
public void onScroll(AbsListView listView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
System.out.println("Scroll to the listview last item");
View lastItemView=(View) listView.getChildAt(listView.getChildCount()-1);
if ((listView.getBottom())==lastItemView.getBottom()) {
System.out.println("========Scroll to the listview bottom =============");
addDataForListView();
mSimpleAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onScrollStateChanged(AbsListView listview, int scrollState) {
}
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
listviewitem.mxl如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="80dip"
/>
</RelativeLayout>
MainActivity如下:
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView mListView;
private SimpleAdapter mSimpleAdapter;
private HashMap<String, Object> mHashMap;
private ArrayList<HashMap<String, Object>> mArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mListView=(ListView) findViewById(R.id.listView);
mArrayList=new ArrayList<HashMap<String,Object>>();
addDataForListView();
mSimpleAdapter=new SimpleAdapter
(MainActivity.this, mArrayList, R.layout.listviewitem, new String []{"id"},new int []{R.id.textView});
mListView.setAdapter(mSimpleAdapter);
mListView.setOnScrollListener(new OnScrollListenerImple());
}
private void addDataForListView(){
System.out.println("====>Add data to ListView");
for (int i = 0; i < 10; i++) {
mHashMap=new HashMap<String, Object>();
mHashMap.put("id", ""+i);
mArrayList.add(mHashMap);
}
}
private class OnScrollListenerImple implements OnScrollListener{
@Override
public void onScroll(AbsListView listView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
System.out.println("Scroll to the listview last item");
View lastItemView=(View) listView.getChildAt(listView.getChildCount()-1);
if ((listView.getBottom())==lastItemView.getBottom()) {
System.out.println("========Scroll to the listview bottom =============");
addDataForListView();
mSimpleAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onScrollStateChanged(AbsListView listview, int scrollState) {
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询