我需要在导航抽屉中禁用第二个元素,直到某个任务完成。以下是我禁用第二个元素的方法:
nav_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/dark_green"
android:state_pressed="true" />
<item android:drawable="@android:color/transparent"
android:state_enabled="true" />
<item android:drawable="@android:color/darker_gray"
android:state_enabled="false" />
</selector>
nav_fragment.xml(包装在DrawerLayout中)
<ListView
android:id="@+id/left_drawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:cacheColorHint="#0000"
android:background="@drawable/gradient_radial_backround"
android:choiceMode="singleChoice"
android:divider="@color/green_light"
android:dividerHeight="1dp" />
nav_list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nav_item_selector"
android:orientation="horizontal"
android:padding="@dimen/standard_padding">
<ImageView
android:id="@+id/nav_list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/nav_list_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:minHeight="@dimen/listPreferredItemHeightSmall"
android:paddingLeft="@dimen/activity_horizontal_small_margin"
android:text="List Item"
android:textSize="@dimen/text_nav_list_entry"
android:textColor="@color/dark_green" />
</LinearLayout>
的活动:
public class NavItemAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
private String[] listItems;
public NavItemAdapter(Context context, int textViewResourceId, String[] listItems) {
super(context, textViewResourceId, listItems);
this.listItems = listItems;
inflater = LayoutInflater.from(context);
}
@Override
public boolean isEnabled(int position)
{
if(position == 1)
return false;
else
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = new NavItemView(getContext());
NavItemView navItemView = (NavItemView) convertView;
switch (position) {
case 0:
navItemView.setUpItem(R.drawable.ic_nav_1_checked, listItems[position], R.drawable.ic_nav_1);
break;
case 1:
navItemView.setUpItem(R.drawable.ic_nav_2_checked, listItems[position], R.drawable.ic_nav_2);
break;
case 2:
navItemView.setUpItem(R.drawable.ic_nav_3_checked, listItems[position], R.drawable.ic_nav_3);
break;
case 3:
navItemView.setUpItem(R.drawable.ic_nav_4_checked, listItems[position], R.drawable.ic_nav_4);
break;
case 4:
navItemView.setUpItem(R.drawable.ic_nav_5_checked, listItems[position], R.drawable.ic_nav_5);
break;
case 5:
navItemView.setUpItem(R.drawable.ic_nav_6_checked, listItems[position], R.drawable.ic_nav_6);
break;
}
return convertView;
}
}
class NavItemView extends LinearLayout implements Checkable {
private View v;
private ImageView iconView;
private TextView textView;
private Boolean checked = false;
private int mImageChecked;
private int mImage;
public NavItemView(Context context) {
super(context);
LayoutInflater inflater = LayoutInflater.from(context);
v = inflater.inflate(R.layout.list_item_navdrawer, this, true);
assert v != null;
iconView = (ImageView) v.findViewById(R.id.nav_list_icon);
textView = (TextView) v.findViewById(R.id.nav_list_text);
}
@Override
public boolean isChecked() {
return checked;
}
@Override
public void setChecked(boolean checked) {
this.checked = checked;
if (isChecked()) {
setBackgroundColor(getResources().getColor(R.color.transparent));
textView.setTextColor(getResources().getColor(R.color.light_pink));
iconView.setImageResource(mImageChecked);
} else {
//Reset to default
setBackgroundColor(getResources().getColor(R.color.transparent));
textView.setTextColor(getResources().getColor(R.color.green_lighter));
iconView.setImageResource(mImage);
}
}
@Override
public void toggle() {
checked = !checked;
}
private void setUpItem(int imageChecked, String title, int image) {
mImageChecked = imageChecked;
mImage = image;
textView.setText(title);
iconView.setImageResource(image);
}
}
项目被禁用,但选择器没有启动(它保持相同的颜色)。不仅如此,那个物品的分隔物消失了,甚至更糟!这里发生了什么?
答案 0 :(得分:1)
我实在无法相信这一点,但唯一的解决方案似乎是一个奇怪的黑客。这个问题有几个主题,但我发现的唯一可靠答案来自用户Jason Lin located here。
似乎缺少两个拼图,第一个是我想要在isEnabled()中禁用的项目返回false只会使项目无法点击且无法对焦。要实际禁用它,我需要在getView()中执行相同的检查,并在项目上调用setEnabled(false)...
if(position == 1)
convertView.setEnabled(false);
和强>
在行的线性布局上设置android:duplicateParentState="true"
。
这是最重要的部分,因为显然我首先尝试在getView()中调用setEnabled(false),但是我花了一整天时间才知道我需要所有这三件事的组合 - 在isEnabled中返回false (),在getView()中将enabled设置为false,并将duplicateParentState设置为true:)