居中+循环滚动的listview(2)
来源:互联网 发布:java时间转换成毫秒 编辑:程序博客网 时间:2024/05/01 13:38
接上上一篇所说,最后是一个自定义的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() {@Overridepublic 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();elseposition = 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,日后有机会改善。
0 0
- 居中+循环滚动的listview(2)
- 居中+循环滚动的listview(1)
- 居中滚动的scrollview
- 居中滚动的Recyclerview
- Android ListView实现无限循环滚动
- android listview 的滚动
- Listview的滚动方式
- 横向滚动的ListView
- 不可滚动的ListView
- 循环滚动的图片
- UIScrollView的循环滚动
- ViewPager 的循环滚动
- listview滚动实现-2
- ListView居中
- 滚动视图的自动循环滚动
- 控制ListView的自动滚动
- 实现ListView的弹性滚动
- 横向ListView的自动滚动
- 基于TINY4412的Andorid开发-------简单的LED灯控制
- MySQL的基本命令
- cocos2dx学习之路----第一篇(cocos2dx版本3.9开发环境的搭建及HelloWold的见面)
- 上下文菜单的创建
- 修复和预防Bug的成本的量化对比
- 居中+循环滚动的listview(2)
- Java poi读取xls文件出现nvalid header signature异常
- 生成二维码
- stm8 rtc时钟
- RecyclerView初探
- 通用权限管理设计篇
- mybatis 插入数据返回主见
- Ftxxxx系列------如何在makefile里定义c文件使用的宏
- [FMS]adobe FMS配置实现(FMS/HLS/HDS)功能经验