我在我的项目中使用自定义列表视图。
当我点击ListView
中的项目时,该项目应该会突出显示。
这是listadapter
@Override
public View getView( final int position, View convertView, ViewGroup parent) {
View row = convertView;
SubCategoryHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new SubCategoryHolder();
holder.dishName = (TextView) row.findViewById(R.id.subcat_name);
row.setTag(holder);
}else {
holder = (SubCategoryHolder) row.getTag();
}
final SubcategoryGetSet sub = objects.get(position);
holder.dishName.setText(sub.getDishName());
holder.dishName.setTextColor(Color.parseColor("#000000"));
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(true);
v.setBackgroundColor(Color.parseColor("#0000e0"));
}
});
return row;
}
当我点击项目时,它会亮到蓝色。 问题是,当我点击另一个项目时,它仍然是高亮度的。 如何删除先前选定的背景颜色??
答案 0 :(得分:1)
您可以更改所选列表视图项的颜色。下面的代码可能会有所帮助。
首先将其放入listview
机器人:listSelector =" @可绘制/ list_selector"
然后在drawable中创建xml文件以控制不同的状态
@绘制/ list_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
@绘制/ list_item_bg_normal
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background"
android:endColor="@color/list_background"
android:angle="90" />
</shape>
@绘制/ list_item_bg_pressed
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background_pressed"
android:endColor="@color/list_background_pressed"
android:angle="90" />
</shape>
在ListView选择中
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
...
}
}
不要忘记将list_background_pressed和list_background添加到values / color.xml中,或者只是在每个文件中手动设置颜色。
而且我相信当你使用setSelection(int pos)时会自动使用你设置为选择的布局。
答案 1 :(得分:0)
试试这个,希望它有所帮助。
Integer lastSelectedItemPos;//to store last selected view position
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
SubCategoryHolder holder;
if (row == null) {
holder = new SubCategoryHolder();
//your inflating stuff and find views
//setOnClickListener for your layout
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = (int) view.getTag();
final SubcategoryGetSet sub = objects.get(pos);
sub.setSelected(true);//new item selected
if (lastSelectedItemPos != null) {
sub = objects.get(lastSelectedItemPos);
sub.setSelected(false);// dis select previous one
}
lastSelectedItemPos = pos;
notifyDataSetChanged();//refresh views
}
});
row.setTag(holder);
} else {
holder = (SubCategoryHolder) row.getTag();
}
holder.layout.setTag(position);//set position as tag to your layout
final SubcategoryGetSet sub = objects.get(pos);
//your other stuff
if (sub.isSelected())
row.setBackgroundColor(Color.RED);//selected color
else
row.setBackgroundColor(Color.GRAY);//not selected color
return row;
}
class SubCategoryHolder{
//other views
LinearLayout layout;
}
};
您需要在SubcategoryGetSet
class SubcategoryGetSet{
//your other objects, getters and setters
boolean selected;
public boolean isSelected() { return selected; }
public void setSelected(boolean selected) { this.selected = selected; }
}
您需要再添加一个额外视图作为根视图的子视图
<!-- root view (whatever your view is I am using LL )-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- add this view as sub child of root view and set listener for this -->
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--your other views/adapter design goes here -->
</LinearLayout>
</LinearLayout>
答案 2 :(得分:0)
我发现这对我来说是最简单的解决方案。 在您的XML文件的ListView描述中,只需添加 带有所需颜色的以下属性:
android:listSelector="@color/colorBlue"