列表选择器工作不正常 [英] List selector not working properly
问题描述
在我的应用我使用与列表导航抽屉view.Now我想要的是,当用户选择一个列表项列表项的颜色应该改变,也应该保持selected.With一些研究,我能够改变列表项到所需的颜色,但它不保持选定
In my application i am using a navigation drawer with list view.Now what i want is that when user selects a list item the color of the list item should change and also it should remain selected.With some research i am able to change the list item to desired color but it does not remains selected.
列表selector.xml
List selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@color/pink" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@color/pink" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="@color/pink" />
<item
android:drawable="@android:color/white" />
</selector>
ListView.xml
ListView.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#fff"
android:layout_weight="3">
<ListView
android:id="@+id/lv_nav_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@android:color/transparent"
android:paddingBottom="05dp"></ListView>
</LinearLayout>
code onItemClick
Code onItemClick
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lvNavItems.setItemChecked(position, true);
lvNavItems.setSelection(position);
mDrawerLayout.closeDrawer(fragContainer);
}
DrawerClass
DrawerClass
public class NavigationDrawerFrag extends MasterFragment implements AdapterView.OnItemClickListener {
private View view;
private View fragContainer;
private ActionBarDrawerToggle drawerToggle;
private DrawerLayout mDrawerLayout;
private List<NavItems> listNavItems;
private int[] icons = new int[]{R.drawable.ic_home_r, R.drawable.ic_profile, R.drawable.ic_celebrities, R.drawable.ic_verify, R.drawable.ic_about_us, R.drawable.ic_tc, R.drawable.ic_home, R.drawable.ic_home, R.drawable.ic_cancellation, R.drawable.ic_logout};
private int[] iconsColored = new int[]{R.drawable.ic_home, R.drawable.ic_profile_r, R.drawable.ic_celebrities_r, R.drawable.ic_verify_r, R.drawable.ic_about_us_r, R.drawable.ic_tc_r, R.drawable.ic_home_r, R.drawable.ic_home_r, R.drawable.ic_cancellation_r, R.drawable.ic_logout_r};
private String[] items;
private ListView lvNavItems;
private NavItemAdapter adapter;
public NavigationDrawerFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.navigation_drawer_frag, container, false);
return view;
}
public void setUp(DrawerLayout drawerLayout, Toolbar toolbar, int fragId) {
fragContainer = getActivity().findViewById(fragId);
mDrawerLayout = drawerLayout;
drawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.open, R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(drawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
drawerToggle.syncState();
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init();
}
private void init() {
lvNavItems = (ListView) view.findViewById(R.id.lv_nav_items);
items = getActivity().getResources().getStringArray(R.array.NavItems);
adapter = new NavItemAdapter(getActivity(), getListItems());
lvNavItems.setAdapter(adapter);
lvNavItems.setItemChecked(2, true);
lvNavItems.setSelection(2);
lvNavItems.setOnItemClickListener(this);
}
public List<NavItems> getListItems() {
listNavItems = new ArrayList<>();
for (int i = 0; i < icons.length && i < items.length; i++) {
NavItems navItems = new NavItems(items[i], icons[i]);
listNavItems.add(navItems);
}
return listNavItems;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CommonFunctions.replaceFragment(getActivity(), new ProfileFrag(), true);
lvNavItems.setItemChecked(position, true);
lvNavItems.setSelection(position);
mDrawerLayout.closeDrawer(fragContainer);
}
}
自定义列表项
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_selector"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_nav_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:src="@drawable/ic_home" />
<TextView
android:id="@+id/tv_nav_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/iv_nav_icon"
android:layout_toRightOf="@+id/iv_nav_icon"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="New Text"
android:textColor="#000"
android:textSize="@dimen/regular" />
</RelativeLayout>
请帮我在哪里出了错。
please do help me out where i went wrong.
推荐答案
我想点击任何项目后,你关上抽屉,并表现出一定的屏幕。当抽屉被关闭列表视图重置本身,当抽屉再次打开重新渲染它。
I assume after click on any item you close the drawer and show some screen. When drawer is closed listview resets itself and re-render it when drawer is opened again.
解决方案将节省 getView
方法选定的索引并设置在该索引的项目所需的背景色。
Solution would be to save the selected index and in getView
method you set the desired bg color on the item for that index.
-
创建一个类级别的变量
Create a class level variable
public static int NAV_CURR_SELECTION = 2; // you have used 2 in init()
添加此
Add this
NAV_CURR_SELECTION = position;
在 onItemClick()
法
现在,加入 onDrawerOpened这些行()
lvNavItems.setItemChecked(NAV_CURR_SELECTION, true);
lvNavItems.setSelection(NAV_CURR_SELECTION);
修改:除了使用上面提到的两行 onDrawerOpened()
做到这一点。 (仅改变第3步1和2需要保持不变)
Edit: Rather than using the two lines mentioned above in onDrawerOpened()
do this. (Only change step 3. 1 and 2 need to remain same)
View v = lvNavItems.getChildAt(NAV_CURR_SELECTION);
v.setBackgroundColor(0xFF00FF00); // or whatever color you need
这篇关于列表选择器工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!