android 机顶盒 listview 获取焦点改变item背景
2016-06-02 18:59
323 查看
在网上搜索了很多文章都没有找到我想要的效果。 正所谓实践才是真理,于是我反复猜测和调试,最终达到了我想要的效果!下面分享代码,希望能帮助到需要的人!
要达到这个效果需要用到 setOnFocusChangeListener 监听方法。代码如下:
ListView的xml布局:
<ListView
android:id="@+id/a_listbody_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@null" >
</ListView>
ListView的item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/lt_height126"
android:gravity="center_vertical"
android:paddingLeft="@dimen/default_grap45" >
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_text16" />
</LinearLayout>
</LinearLayout>
布局文件就这样,很简单吧!
下面看看核心代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder holder = null;
// if (convertView == null) {
// holder = new ViewHolder();
convertView = getLayoutInflater().inflate(
R.layout.item_communityaddr_layout, null);
convertView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setBackgroundColor(R.color.bg_orange);
}
});
TextView mTextName = (TextView) convertView.findViewById(R.id.item_text);
// holder.mTextName = (TextView) convertView
// .findViewById(R.id.item_text);
// convertView.setTag(holder);
// } else {
// holder = (ViewHolder) convertView.getTag();
// }
监听每个item(convertView)的焦点获取情况。当该item获取到焦点时 就是设置其背景颜色。这样就达到我们的效果啦!是不是很简单呢?
要达到这个效果需要用到 setOnFocusChangeListener 监听方法。代码如下:
ListView的xml布局:
<ListView
android:id="@+id/a_listbody_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@null" >
</ListView>
ListView的item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/lt_height126"
android:gravity="center_vertical"
android:paddingLeft="@dimen/default_grap45" >
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_text16" />
</LinearLayout>
</LinearLayout>
布局文件就这样,很简单吧!
下面看看核心代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder holder = null;
// if (convertView == null) {
// holder = new ViewHolder();
convertView = getLayoutInflater().inflate(
R.layout.item_communityaddr_layout, null);
convertView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setBackgroundColor(R.color.bg_orange);
}
});
TextView mTextName = (TextView) convertView.findViewById(R.id.item_text);
// holder.mTextName = (TextView) convertView
// .findViewById(R.id.item_text);
// convertView.setTag(holder);
// } else {
// holder = (ViewHolder) convertView.getTag();
// }
监听每个item(convertView)的焦点获取情况。当该item获取到焦点时 就是设置其背景颜色。这样就达到我们的效果啦!是不是很简单呢?
相关文章推荐
- Android Uri获取不到视频文件路径的问题
- android开发步步为营之105:解决键盘弹起页面被顶上去问题
- Android NDK开发Crash错误定位
- DrawerLayout侧滑菜单
- Ndk(jni)调用第三方的so库
- android ScrollView中嵌套GridView,ListView只显示一行的解决办法
- Android Intent
- 布局演示和android下单位
- android studio上代码编译调试中遇到的一些异常记录
- android 工程搭建结构
- android/DisplayHotplug
- android 瀑布流效果(仿蘑菇街)
- Android 沉浸式状态栏攻略 让你的状态栏变色吧
- Android studio常用快捷键
- 获得android 应用的版本和当前android系统版本
- 快速查看Android Docs文档
- 打包apk生成签名
- Android内存优化之OOM
- Android内存优化总结【整理】
- RecycleView之使用心得【1】