您的位置:首页 > 移动开发 > Android开发

android 中listview滑动加载的简单demo

2011-11-05 00:39 495 查看
 在使用listview时,滑动加载可以提高效率,增加用户体验。

主要用到一个OnScrollListener
/**
* The view is not scrolling. Note navigating the list using the trackball counts as
* being in the idle state since these transitions are not animated.
*/
public static int SCROLL_STATE_IDLE = 0;

/**
* The user is scrolling using touch, and their finger is still on the screen
*/
public static int SCROLL_STATE_TOUCH_SCROLL = 1;

/**
* The user had previously been scrolling using touch and had performed a fling. The
* animation is now coasting to a stop
*/
public static int SCROLL_STATE_FLING = 2;

/**
* Callback method to be invoked while the list view or grid view is being scrolled. If the
* view is being scrolled, this method will be called before the next frame of the scroll is
* rendered. In particular, it will be called before any calls to
* {@link Adapter#getView(int, View, ViewGroup)}.
*
* @param view The view whose scroll state is being reported
*
* @param scrollState The current scroll state. One of {@link #SCROLL_STATE_IDLE},
* {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.
*/
public void onScrollStateChanged(AbsListView view, int scrollState);

/**
* Callback method to be invoked when the list or grid has been scrolled. This will be
* called after the scroll has completed
* @param view The view whose scroll state is being reported
* @param firstVisibleItem the index of the first visible cell (ignore if
*        visibleItemCount == 0)
* @param visibleItemCount the number of visible cells
* @param totalItemCount the number of items in the list adaptor
*/
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount);

package com.nico.listviewload;

import java.util.ArrayList;
import java.util.List;

import com.nico.bean.MsgInfo;

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.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

public class MainActivity extends Activity {

List<MsgInfo> infoList = null;
private LayoutInflater inflater = null;
private ListView listview = null;
private myAdapter adapter = null;
private LinearLayout ftview = null;

private Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){

case 1:
adapter.notifyDataSetChanged();
try {
Thread.sleep(2000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
setAdapter();
break;
}
}
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater = this.getLayoutInflater();

listview = (ListView) findViewById(R.id.listview);
ftview = (LinearLayout) inflater.inflate(R.layout.ftview, null);

listview.addFooterView(ftview, null, false);
listview.setAdapter(new myAdapter());
adapter = new myAdapter();

ftview.setVisibility(View.VISIBLE);
listview.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

if (scrollState == SCROLL_STATE_IDLE
&& view.getLastVisiblePosition() == view.getCount() - 1)

{
handler.sendEmptyMessage(1);
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

}
});
}

public List<MsgInfo> getList() {
List<MsgInfo> infoList = new ArrayList<MsgInfo>();
for (int i = 0; i < 7; i++) {
MsgInfo info = new MsgInfo();
info.id = i;
info.detailinfo = "这是第" + i + "条信息";
info.name = "第" + i + "项";
info.size = 1024 * i;
infoList.add(info);
}
return infoList;
}

public void setData() {

infoList = getList();
}

public void setAdapter() {
for (int i = 0; i < 7; i++) {
MsgInfo info = new MsgInfo();
info.id = i;
info.detailinfo = "这是第" + i + "条信息";
info.name = "第" + i + "项";
info.size = 1024 * i;
infoList.add(info);
}

adapter.notifyDataSetChanged();
}

public class myAdapter extends BaseAdapter {

@Override
public void notifyDataSetChanged() {
if(ftview.getVisibility()==View.VISIBLE)
{
ftview.setVisibility(View.GONE);
}
else if(ftview.getVisibility()==View.GONE)
{
ftview.setVisibility(View.VISIBLE);
}
super.notifyDataSetChanged();
}

TextView name, detail, size;

public myAdapter() {
setData();
}

@Override
public int getCount() {
return infoList.size();
}

@Override
public Object getItem(int position) {
return infoList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null, false);

}
name = (TextView) convertView.findViewById(R.id.name);
detail = (TextView) convertView.findViewById(R.id.detail);
size = (TextView) convertView.findViewById(R.id.size);
name.setText(infoList.get(position).name);
detail.setText(infoList.get(position).detailinfo);
size.setText(infoList.get(position).size + "");
return convertView;
}

}

public class LoadAsyTask extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... params) {

for (int i = 0; i < 7; i++) {
MsgInfo info = new MsgInfo();
info.id = i;
info.detailinfo = "这是第" + i + "条信息";
info.name = "第" + i + "项";
info.size = 1024 * i;
infoList.add(info);
}
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

ftview.setVisibility(View.GONE);
adapter.notifyDataSetChanged();
}

}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="fill_parent" android:gravity="center_horizontal"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
<ImageView android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/progressbar" android:id="@+id/pbdrawable" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="请稍后..." android:id="@+id/pbtxt" />
</LinearLayout>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
android:toDegrees="360">

<shape android:shape="ring" android:innerRadiusRatio="3"
android:thicknessRatio="8" android:useLevel="false">

<gradient android:type="sweep" android:useLevel="false"
android:startColor="#000000" android:centerColor="#FFFFFF"
android:centerY="0.50" android:endColor="#000000" />

</shape>

</rotate>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息