gpt4 book ai didi

java - ListItem焦点改变

转载 作者:行者123 更新时间:2023-12-01 12:33:24 25 4
gpt4 key购买 nike

我有一个自定义 listView,其中包含一些 View (如 textView 和 imageView)。我想在列表项获得焦点时更改其背景颜色。我已经为我的转换 View 设置了 OnfocusChange 但它不起作用。这是我的持有者类别:

private class Holder {
ImageView iconImage;
TextView titleText ;
TextView costText ;
}

和 getView 方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Item item = item.get(position);

final Holder temp;

if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
temp = new Holder();
temp.iconImage = (ImageView) convertView
.findViewById(R.id.iconImageView);

temp.titleText = (TextView) convertView
.findViewById(R.id.titleTextView);
temp.costText = (TextView) convertView
.findViewById(R.id.costTextView);

convertView.setTag(temp);
} else {
temp = (Holder) convertView.getTag();
}
temp.titleText.setText(item.getItemTitle());
//this focus listener doesn't work.
convertView.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
temp.titleText.setBackground(context.getResources()
.getDrawable(
R.drawable.green_background));

} else {
temp.titleText.setBackground(context.getResources()
.getDrawable(R.drawable.black_background));
}
}
});

}

如何为 ListView 中的每个列表项设置 OnFocusChange 监听器?

最佳答案

您是否尝试过将 focusable 和 focusableintouchmode 设置为 true?

android:focusable="true"
android:focusableInTouchMode="true"

并尝试将textviews和imageview的focusable设置为false。

关于java - ListItem焦点改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25764327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com