居中滚动的scrollview

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

我们平时用的listview,scrollview都是在边界的时候滚动,可是我们某些时候可能需要把焦点固定在中间一行来进行滚动。

网上有很多这种实现,大多利用android的picker来实现,今天我们可以尝试另外一种实现思路,利用scrollview+listview来完成这种需求。

这里的demo只是监听了按键进行滚动,并没有监听手势。微笑

布局如下,在一个scrollview里面放一个linearlayout

<ScrollView
        android:id="@+id/scrollview"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_marginBottom="10dp" 
        android:layout_marginTop="100dp"
        android:scrollbars="none">
        
        <LinearLayout
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:gravity="center"/>
</ScrollView>

效果图如下,这时候listitem可以根据需要,在代码里面动态添加。

listitem布局如下

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_w_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"    //跑马灯效果
    android:focusable="true"
    android:clickable="true"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="#7b7b7b"
    android:textSize="20sp" />

下面这几行代码就可以把listitem动态的添加。

ListView = (LinearLayout) findViewById(R.id.list);
mScrollView = (ScrollView) findViewById (R.id.scrollview);
TextView view = (TextView)layoutInflater.inflate(R.layout.listitem, null);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 120);
mListView.addView(view, lp);
mViewItems.add(view);

上面的代码就可以简单的实现一个到边界滚动的scrollview。

为了让scrollview在焦点在中间的listitem就开始滚动,我们需要监听事件,并且判断当前焦点的位置。

这里的demo只是监听上下按键,

private View.OnKeyListener mOnKeyListener=new View.OnKeyListener() {
		
		@Override
		public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
			switch(arg2.getAction()){
				case KeyEvent.ACTION_DOWN:
					 moveFocus(arg1,arg0);
					 break;
					 
			}
			return false;
		}
		
		private void moveFocus(int direction,View focus) {
			int index=findIndexInlist(((TextView)focus).getText().toString(), items);
			if(index<items.size()-1&&direction==KeyEvent.KEYCODE_DPAD_DOWN)//根据按键寻找下一个焦点
				mFocus=mViewItems.get(index+1);
			else if(index>0&&direction==KeyEvent.KEYCODE_DPAD_UP)
				mFocus=mViewItems.get(index-1);
			else
				mFocus=focus;
			focus.setSelected(false);
			mFocus.setSelected(true);
			int[] loc = new int[2];
			mScrollView.getLocationOnScreen(loc);   //获取scrollview的屏幕坐标

			int[] screenLocation = new int[2];
			mFocus.getLocationOnScreen(screenLocation);   //获取当前焦点的屏幕坐标
			
			int height = ((TextView)mFocus).getHeight();//获取listitem的高度
			
			if (screenLocation[1] < loc[1] + 2*height) {     //这里就可以自定义什么时候滚动,我这里是有5行,这样设置刚好到中间就滚动。
				mScrollView.scrollTo(0, mScrollView.getScrollY() - height);
			}
			if (screenLocation[1] > loc[1] + 2*height) {
				mScrollView.scrollTo(0, mScrollView.getScrollY() + height);
			}
		}
	};

这里也并没有实现循环滚动,假如有人需要的话,下一次再更新。大笑

猜你喜欢

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