居中滚动的scrollview
来源:互联网 发布:linux如何编辑文档 编辑:程序博客网 时间:2024/05/16 14:42
我们平时用的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() {@Overridepublic 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);elsemFocus=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);}}};
这里也并没有实现循环滚动,假如有人需要的话,下一次再更新。
0 0
- 居中滚动的scrollview
- 监听ScrollView的滚动
- 关于scrollview的滚动
- 居中滚动的Recyclerview
- 没有滚动条的ScrollView
- TextView 的ScrollView滚动效果
- 带有滚动视差的ScrollView
- TextView 的ScrollView滚动效果
- scrollview的无缝循环滚动
- scrollView的滚动和缩放
- 一个有两秒轮询滚动的scrollView
- ScrollView不能滚动的问题
- Android ScrollView的滚动事件
- ViewPager+scrollview的水平滚动
- 带有滚动监听的ScrollView
- 去掉ScrollView的滚动条
- ScrollView的滚动监听setOnScrollChangeListener
- NGUI 的ScrollView如何让item居中
- C语言 随机数
- 如何设计一个web容器
- 日经春秋 20160214
- 爬过的坑之Jquery篇
- library routine called out of sequence
- 居中滚动的scrollview
- leetcode85 maximal rectangle
- android中常用的finish()与onDestroy()的区别
- 用户画像数据建模方法
- CheckBoxDemo
- 简历的主线这样写,就会有面试通知!
- jQuery全屏滚动插件fullPage(转自https://github.com/powy1993/fullpage)
- TinyXML入门
- 《java设计模式》之观察者模式