如何在点击按钮时突出显示listview
中的特定行,我可以使用以下代码在onItemClick
操作中选择一个项目:view.setSelected(true);
但是,我想选择一个项目时点击button
listview
修改 我用了这段代码:
lvData.setSelection(4);
lvData.setSelected(true);
其中xml选择器是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/gray" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/gray" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@color/android:transparent" android:state_focused="false" android:state_pressed="false"/>
</selector>
并且行布局为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lyt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/txtDuration"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000" />
<TextView
android:id="@+id/txtDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000" />
</RelativeLayout>
,自定义适配器是:
package com.example.heartbeats;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class TrackAdapter extends ArrayAdapter<Track> {
private List<Track> values;
private Context context;
private int selectedItem = -1; // no item selected by default
public ArrayList<View> views = new ArrayList<View>();
public TrackAdapter(Context context, int textViewResourceId,
List<Track> values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
// Keeps reference to avoid future findViewById()
valuesViewHolder viewHolder;
final Track t = values.get(position);
if (v == null) {
LayoutInflater li = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.row_track, parent, false);
viewHolder = new valuesViewHolder();
viewHolder.tvTitle = (TextView) v.findViewById(R.id.txtTitle);
viewHolder.tvDuration = (TextView) v.findViewById(R.id.txtDuration);
Typeface type = Typeface.createFromAsset(context.getAssets(),
"fonts/futura.ttf");
viewHolder.tvTitle.setTypeface(type);
viewHolder.tvDuration.setTypeface(type);
v.setTag(viewHolder);
} else {
viewHolder = (valuesViewHolder) v.getTag();
}
if (t != null) {
viewHolder.tvTitle.setText(t.title);
viewHolder.tvDuration.setText(t.duration);
}
return v;
}
public void setSelectedItem(int selectedItem) {
this.selectedItem = selectedItem;
}
static class valuesViewHolder {
TextView tvTitle;
TextView tvDuration;
}
}
答案 0 :(得分:0)
你必须为此创建一个selector.xml
文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
在main.xml
文件中添加以下行ListView
:
android:listSelector="@drawable/selector"