为什么我的 ListView 项目被选中,但点击后没有突出显示?
Why is my ListView item selected, but not highlighted after click?
我正在使用 Master-Detail Flow 并使用由 BaseAdapter
.[=23= 定义的自定义 ListView
]
用户触摸 列表项后,它不会保持选中状态。
我想我可以通过定义一个selector
,
来解决它
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected Item -->
<item android:state_selected="true"
android:drawable="@android:color/background_dark" />
<!-- Default Item -->
<item android:state_selected="false"
android:drawable="@android:color/white" />
</selector>
在我的 ListView
布局中:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/android:list"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:background="@layout/list_selected"
android:layout_height="fill_parent" />
特此我的list_item_layout
xml档案:
<?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="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:scaleType="fitCenter"
android:src="@drawable/datahora" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:fontFamily="sans-serif-light"
android:text="TextView"
android:textSize="20dp" />
</LinearLayout>
这是我的CustomAdapter
class
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItem;
CustomAdapter(Context context, List<RowItem> rowItem) {
this.context = context;
this.rowItem = rowItem;
}
@Override
public int getCount() {
return rowItem.size();
}
@Override
public Object getItem(int position) {
return rowItem.get(position);
}
@Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_layout, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageView1);
TextView txtTitle = (TextView) convertView.findViewById(R.id.textView1);
RowItem row_pos = rowItem.get(position);
// setting the image resource and title
imgIcon.setImageResource(row_pos.getIcon());
txtTitle.setText(row_pos.getTitle());
return convertView;
}
}
还有我的 ListFragment
class,其中用户点击似乎由 Callback
:
处理
public class ParametroListFragment extends ListFragment {
String[] menutitles;
TypedArray menuIcons;
CustomAdapter adapter;
private List<RowItem> rowItems;
/**
* The serialization (saved instance state) Bundle key representing the
* activated item position. Only used on tablets.
*/
private static final String STATE_ACTIVATED_POSITION = "activated_position";
/**
* The fragment's current callback object, which is notified of list item
* clicks.
*/
private Callbacks mCallbacks = sDummyCallbacks;
/**
* The current activated item position. Only used on tablets.
*/
private int mActivatedPosition = ListView.INVALID_POSITION;
/**
* A callback interface that all activities containing this fragment must
* implement. This mechanism allows activities to be notified of item
* selections.
*/
public interface Callbacks {
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(String id);
}
/**
* A dummy implementation of the {@link Callbacks} interface that does
* nothing. Used only when this fragment is not attached to an activity.
*/
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ParametroListFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
menutitles = getResources().getStringArray(R.array.titles);
menuIcons = getResources().obtainTypedArray(R.array.icons);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < menutitles.length; i++) {
RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(
i, -1));
rowItems.add(items);
}
adapter = new CustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView listView = (ListView) inflater.inflate(R.layout.list_layout,
null);
return listView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState
.getInt(STATE_ACTIVATED_POSITION));
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the dummy implementation.
mCallbacks = sDummyCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
view.setSelected(true);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(
activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
我认为这足以工作,但我不知道为什么毕竟没有被突出显示。有人可以帮忙吗?
在您的列表视图中添加以下行 xml。
android:drawSelectorOnTop="false"
android:divider="@null"
android:dividerHeight="0.0sp"
android:listSelector="@android:color/darker_gray"
尽管@jyomin 为我的问题提供了解决方案,但它可能会产生一些错误:
- 当用户滚动(向下或向上)足以隐藏所选项目时,它将return变为未选择状态。
- 如果用户触摸屏幕(在任何地方),它将移除
ListView
项目焦点。
话虽这么说(并且经过测试),我决定为我的旧代码找到另一种解决方法,因为我不仅想突出显示 但 激活 ListView
项。
我发现我在错误的地方设置了后台行为。之前应该设置在list_item_layout
:
里面的LinearLayout
<?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="horizontal"
android:background="@layout/list_selected" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:scaleType="fitCenter"
android:src="@drawable/datahora" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-light"
android:text="TextView"
android:textColor="@layout/text_selected"
android:textSize="22dp" />
</LinearLayout>
只是将 activated 状态添加到 View
:
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
view.setSelected(true);
view.setActivated(true);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
}
并定义了一个新的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected Item -->
<item android:state_selected="true"
android:drawable="@android:color/holo_blue_light" />
<item android:state_activated="true"
android:drawable="@android:color/holo_blue_light" />
<!-- Default Item -->
<item android:state_selected="false"
android:drawable="@android:color/white" />
</selector>
干得好,马查多。 :)
我正在使用 Master-Detail Flow 并使用由 BaseAdapter
.[=23= 定义的自定义 ListView
]
用户触摸 列表项后,它不会保持选中状态。
我想我可以通过定义一个selector
,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected Item -->
<item android:state_selected="true"
android:drawable="@android:color/background_dark" />
<!-- Default Item -->
<item android:state_selected="false"
android:drawable="@android:color/white" />
</selector>
在我的 ListView
布局中:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/android:list"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:background="@layout/list_selected"
android:layout_height="fill_parent" />
特此我的list_item_layout
xml档案:
<?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="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:scaleType="fitCenter"
android:src="@drawable/datahora" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:fontFamily="sans-serif-light"
android:text="TextView"
android:textSize="20dp" />
</LinearLayout>
这是我的CustomAdapter
class
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItem;
CustomAdapter(Context context, List<RowItem> rowItem) {
this.context = context;
this.rowItem = rowItem;
}
@Override
public int getCount() {
return rowItem.size();
}
@Override
public Object getItem(int position) {
return rowItem.get(position);
}
@Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_layout, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageView1);
TextView txtTitle = (TextView) convertView.findViewById(R.id.textView1);
RowItem row_pos = rowItem.get(position);
// setting the image resource and title
imgIcon.setImageResource(row_pos.getIcon());
txtTitle.setText(row_pos.getTitle());
return convertView;
}
}
还有我的 ListFragment
class,其中用户点击似乎由 Callback
:
public class ParametroListFragment extends ListFragment {
String[] menutitles;
TypedArray menuIcons;
CustomAdapter adapter;
private List<RowItem> rowItems;
/**
* The serialization (saved instance state) Bundle key representing the
* activated item position. Only used on tablets.
*/
private static final String STATE_ACTIVATED_POSITION = "activated_position";
/**
* The fragment's current callback object, which is notified of list item
* clicks.
*/
private Callbacks mCallbacks = sDummyCallbacks;
/**
* The current activated item position. Only used on tablets.
*/
private int mActivatedPosition = ListView.INVALID_POSITION;
/**
* A callback interface that all activities containing this fragment must
* implement. This mechanism allows activities to be notified of item
* selections.
*/
public interface Callbacks {
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(String id);
}
/**
* A dummy implementation of the {@link Callbacks} interface that does
* nothing. Used only when this fragment is not attached to an activity.
*/
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ParametroListFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
menutitles = getResources().getStringArray(R.array.titles);
menuIcons = getResources().obtainTypedArray(R.array.icons);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < menutitles.length; i++) {
RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(
i, -1));
rowItems.add(items);
}
adapter = new CustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView listView = (ListView) inflater.inflate(R.layout.list_layout,
null);
return listView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState
.getInt(STATE_ACTIVATED_POSITION));
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the dummy implementation.
mCallbacks = sDummyCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
view.setSelected(true);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(
activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
我认为这足以工作,但我不知道为什么毕竟没有被突出显示。有人可以帮忙吗?
在您的列表视图中添加以下行 xml。
android:drawSelectorOnTop="false"
android:divider="@null"
android:dividerHeight="0.0sp"
android:listSelector="@android:color/darker_gray"
尽管@jyomin 为我的问题提供了解决方案,但它可能会产生一些错误:
- 当用户滚动(向下或向上)足以隐藏所选项目时,它将return变为未选择状态。
- 如果用户触摸屏幕(在任何地方),它将移除
ListView
项目焦点。
话虽这么说(并且经过测试),我决定为我的旧代码找到另一种解决方法,因为我不仅想突出显示 但 激活 ListView
项。
我发现我在错误的地方设置了后台行为。之前应该设置在list_item_layout
:
LinearLayout
<?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="horizontal"
android:background="@layout/list_selected" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:scaleType="fitCenter"
android:src="@drawable/datahora" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-light"
android:text="TextView"
android:textColor="@layout/text_selected"
android:textSize="22dp" />
</LinearLayout>
只是将 activated 状态添加到 View
:
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
view.setSelected(true);
view.setActivated(true);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
}
并定义了一个新的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected Item -->
<item android:state_selected="true"
android:drawable="@android:color/holo_blue_light" />
<item android:state_activated="true"
android:drawable="@android:color/holo_blue_light" />
<!-- Default Item -->
<item android:state_selected="false"
android:drawable="@android:color/white" />
</selector>
干得好,马查多。 :)