上下滑动的Listview

Listview的滑动实现过程,提供了对滑动状态(开始,滑动和停止)的监听。

自定义Listview:

public class RefreshListView extends ListView implements OnScrollListener {
	private Context context;
	// 按下的Y值
	private int startY;

	// 监听刷新状态的接口
	public interface OnRefreshState {
		// 开始滑动
		void onStart(int starty);

		// 正在滑动
		void onMove(int space);

		// 停止滑动
		void onStop();
	}

	private OnRefreshState onRefreshState;

	public OnRefreshState getOnRefreshState() {
		return onRefreshState;
	}

	public void setOnRefreshState(OnRefreshState onRefreshState) {
		this.onRefreshState = onRefreshState;
	}

	public RefreshListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		initView();
	}

	public RefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initView();
	}

	public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		initView();
	}

	public void initView() {
		this.setOnScrollListener(this);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	/**
	 * 设置 上边距;
	 * 
	 * @param topPadding
	 */
	private void topPadding(int topPadding) {
		this.setPadding(getPaddingLeft(), topPadding, getPaddingRight(),
				getPaddingBottom());
		invalidate();
	}

	/**
	 * 滑动
	 * 
	 * @param ev
	 */
	protected void onMove(MotionEvent ev) {
		int tempY = (int) ev.getY();
		int space = tempY - startY;
		topPadding(space);
		onRefreshState.onMove(space);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		switch (ev.getAction()) {
		// 往下
		case MotionEvent.ACTION_DOWN:
			startY = (int) ev.getY();
			onRefreshState.onStart(startY);
			break;
		// 移动
		case MotionEvent.ACTION_MOVE:
			onMove(ev);
			break;
		// 向上
		case MotionEvent.ACTION_UP:
			topPadding(0);
			onRefreshState.onStop();
			break;
		}
		return super.onTouchEvent(ev);
	}

}

xml:

<RelativeLayout 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.freshlistview.RefreshListView
        android:id="@+id/lv_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.freshlistview.RefreshListView>

</RelativeLayout>

活动:

public class MainActivity extends Activity {
	private MainActivity mContext;
	private List<String> list = new ArrayList<String>();
	private RefreshListView freshLV;
	private LocalAdapter adapter;

	protected void showToast(String s) {
		Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = this;

		freshLV = (RefreshListView) findViewById(R.id.lv_refresh);
		for (int i = 0; i < 10; i++) {
			list.add("");
		}
		adapter = new LocalAdapter();
		freshLV.setAdapter(adapter);
		freshLV.setOnRefreshState(new OnRefreshState() {

			@Override
			public void onStop() {
				// TODO Auto-generated method stub
				showToast("stop");
			}

			@Override
			public void onStart(int starty) {
				// TODO Auto-generated method stub
				showToast("start:" + starty);
			}

			@Override
			public void onMove(int space) {
				// TODO Auto-generated method stub
				showToast("space:" + space);
			}
		});
	}

	private class LocalAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item, null);
			}
			return convertView;
		}
	}
}

注意:

1.如果只想让按住Listview显示的部分,然后才可以滑动的话。设置Listview:

 <com.freshlistview.RefreshListView
        android:id="@+id/lv_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </com.freshlistview.RefreshListView>
2.下拉刷新的实现可以通过:

       <1>.重写Listview,在开始滑动的时候动态的添加了HeaderView。

        <2>.用布局包住Listview,里面添加一个headview,然后滑动整个布局。

3.其实对headview,在Activity中动态添加也可以:

 headerView = LayoutInflater.from(mcontext).inflate(R.layout.view_refresh_headview, null);
        mlvList = (RefreshListView).findViewById(R.id.lvBankCardList);
        mlvdList.addHeaderView(headerView);


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值