居中+循环滚动的listview(2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012591964/article/details/50681689

接上上一篇所说,最后是一个自定义的listview:

package com.example.mylistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyListView extends ListView {
	private TextView mFocus;
	private MyListView instance;

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		instance = this;
		instance.setOnKeyListener(mOnKeyListener);
	}
	
private View.OnKeyListener mOnKeyListener=new View.OnKeyListener() {
		
		@Override
		public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
			// TODO 自动生成的方法存根
			switch(arg2.getAction()){
				case KeyEvent.ACTION_DOWN:
					 if(arg1==KeyEvent.KEYCODE_DPAD_UP||(arg1==KeyEvent.KEYCODE_DPAD_DOWN))
						 moveFocus(arg1,arg0);
					 break;
					 
			}
			
			return false;
		}
		
		private void moveFocus(int direction,View focus) {
			int index=instance.getSelectedItemPosition();
			//丢失焦点的时候返回。
			if(index==-1){	
				return;
			}
			//获取下一个焦点
			int position;
			if(index<instance.getAdapter().getCount()-1&&direction==KeyEvent.KEYCODE_DPAD_DOWN)
				position =index+1-getFirstVisiblePosition();
			else if(index>0&&direction==KeyEvent.KEYCODE_DPAD_UP)
				position =index-1-getFirstVisiblePosition();
			else
				position = index-getFirstVisiblePosition();
			mFocus=(TextView) instance.getChildAt(position);
			//焦点为空返回
			if(mFocus==null)
				return;
			int[] loc = new int[2];
			instance.getLocationOnScreen(loc);

			int[] screenLocation = new int[2];
			mFocus.getLocationOnScreen(screenLocation);
			
			//这里的高度需要有一个调整值,不知道为什么。
			int height = ((TextView)mFocus).getHeight()+2;
		
			if (screenLocation[1] < loc[1] + 8*height) {
				instance.smoothScrollBy(instance.getScrollY() - height, 0);
			}
			if (screenLocation[1] > loc[1] + 8*height) {
				instance.smoothScrollBy(instance.getScrollY() + height, 0);
			}
			
		}
	};
}

至此,就实现了基本的居中+循环滚动的listview,当然会存在不少bug,日后有机会改善奋斗

猜你喜欢

转载自blog.csdn.net/u012591964/article/details/50681689