Как выбрать (выделить) элемент в списке при нажатии на кнопку
Как выделить конкретную строку в 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;
}
}
1 ответ
Вы должны создать selector.xml
file for that, like this:
<?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
file add below line to ListView
:
android:listSelector="@drawable/selector"