Android中Listview的平滑滚动(smoothScrollToPositionFromTop方法)

项目中有一个需求

用户点击某一个按钮,listview滚动到指定的位置的item,需要的效果是平滑的滑动

研究了一下listview,在真机和模拟器上都试了一下(小米4,系统为6.0)模拟器系统为(4.0.3)

代码如下

package org.dengzh;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * 
 * @author dengzh
 * 
 */
public class MainActivity extends Activity implements View.OnClickListener {

	private ListView lv;
	private ImageView iv_back_top;
	private ArrayList<String> list = new ArrayList<String>();
	//private boolean isBackTop = false;
	private Animation fadeInAnim;
	private Animation fadeOutAnim;
	private Button position_1, position_2, position_3;

	//记录当前要滚动的目标位置
	private int curScrollToPosition = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		fadeInAnim = AnimationUtils.loadAnimation(this, R.anim.anim_fade_in);
		fadeInAnim.setFillAfter(true);
		fadeOutAnim = AnimationUtils.loadAnimation(this, R.anim.anim_fade_out);
		fadeOutAnim.setFillAfter(true);

		position_1 = (Button) findViewById(R.id.position_1);
		position_2 = (Button) findViewById(R.id.position_2);
		position_3 = (Button) findViewById(R.id.position_3);

		lv = (ListView) findViewById(R.id.lv);
		iv_back_top = (ImageView) findViewById(R.id.iv_back_top);

		for (int i = 0; i < 999; i++) {
			list.add(i + "");
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, list);
		lv.setAdapter(adapter);

		lv.setOnScrollListener(new AbsListView.OnScrollListener() {

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

				//处理滚动条滚动停止时
				if (scrollState == SCROLL_STATE_IDLE) {
					Log.d("android", "SCROLL_STATE_IDLE");
					//判断是否滚动到了第0项,如果是,就隐藏 右下角的 图标,
                                        //如果不是,那么显示
					if (lv.getFirstVisiblePosition() == 0) {
						if (iv_back_top.getVisibility() == View.VISIBLE) {
							iv_back_top.startAnimation(fadeOutAnim);
							iv_back_top.setVisibility(View.GONE);
						}
					} else {
						if (iv_back_top.getVisibility() == View.GONE) {
							iv_back_top.setVisibility(View.VISIBLE);
							iv_back_top.startAnimation(fadeInAnim);
						}
					}
					
					//如果当前滚动的目标位置不为-1,也就是说这是由点击按钮触发的自动滚动,
                                        //那么最后定位到对应的curScrollPosition
					if (curScrollToPosition != -1) {
						lv.setSelection(curScrollToPosition);
					}
					//最后把curScrollToPosition置为-1, 这样不这样做,那么当自动定位结束后,
                                        //再去用手指滑动listview,当滑动结束后,listview在这里仍然会自动定位到上一个curScrollPosition去
					curScrollToPosition = -1;
				}
			}

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

			}
		});
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.d("android", arg3 + "");
			}
		});

		iv_back_top.setOnClickListener(this);

		position_1.setOnClickListener(this);
		position_2.setOnClickListener(this);
		position_3.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		int position = 0;
		switch (v.getId()) {
		case R.id.iv_back_top:
			//返回顶端
			//smoothScrollToPositionFromTop我在手机(小米4)和模拟器(4.0.3)上测了很久
			//手机上完全没有问题,但是模拟器上的话,如果position的位置很大,比较要滚动到900,那么有时候会滚动到一半停止了,并没有最终滚动到900的位置
			//所以这里用curScrollPosition保存了滚动的目标位置,在onScrollStateChanged监听中,当滚动样状态为停止时,再调用lv.setSelection(curScrollToPosition)来最终再一次将listview的位置定位到目标位置
			//smoothScrollToPositionFromTop中的第二个参数,我看文档中的意思为Desired distance in pixels of position from the top of the view when scrolling is finished
			//理解为当滚动停止时,当前定位的这个item,距离top的距离是多少,我在这儿设置为0
			lv.smoothScrollToPositionFromTop(0, 0, 5000);
			break;
		case R.id.position_1:
			position = Integer.parseInt(position_1.getTag().toString());
			curScrollToPosition = position;
			lv.smoothScrollToPositionFromTop(position, 0, 5000);
			break;
		case R.id.position_2:
			position = Integer.parseInt(position_2.getTag().toString());
			curScrollToPosition = position;
			lv.smoothScrollToPositionFromTop(position, 0, 5000);
			break;
		case R.id.position_3:
			position = Integer.parseInt(position_3.getTag().toString());
			curScrollToPosition = position;
			lv.smoothScrollToPositionFromTop(position, 0, 5000);

			break;
		}

	}

}

smoothScrollToPositionFromTop我在手机(小米4)和模拟器(4.0.3)上测了很久

手机上完全没有问题,但是模拟器上的话,如果position的位置很大,比较要滚动到900,那么有时候会滚动到一半停止了,并没有最终滚动到900的位置

所以这里用curScrollPosition保存了滚动的目标位置,在onScrollStateChanged监听中,当滚动样状态为停止时,再调用lv.setSelection(curScrollToPosition)来最终再一次将listview的位置定位到目标位置

smoothScrollToPositionFromTop中的第二个参数,我看文档中的意思为Desired distance in pixels of position from the top of the view when scrolling is finished

理解为当滚动停止时,当前定位的这个item,距离top的距离是多少,我在这儿设置为0

效果图如下


Android中Listview的平滑滚动(smoothScrollToPositionFromTop方法)
 

相关推荐