列表选择器无法正常工作

时间:2015-08-07 09:30:03

标签: android listview

在我的应用程序中,我使用带有列表视图的导航抽屉。现在我想要的是,当用户选择列表项时,列表项的颜色应该更改,并且它应该保持选中。通过一些研究,我能够将列表项更改为所需的颜色,但不会保持选中状态。

列出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

<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>

代码onItemClick

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



        lvNavItems.setItemChecked(position, true);
        lvNavItems.setSelection(position);
        mDrawerLayout.closeDrawer(fragContainer);


    }

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>

请在我出错的地方帮帮我。

3 个答案:

答案 0 :(得分:0)

您需要引用您的选择器:

待办事项

android:listSelector="@drawable/yourSelector"

而不是

android:listSelector="@android:color/transparent"

答案 1 :(得分:0)

  <ListView
            android:id="@+id/lv_nav_items"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:listSelector="@drawable/listselector"
            android:paddingBottom="05dp"></ListView>

答案 2 :(得分:0)

我假设您点击任何项目后关闭抽屉并显示一些屏幕。当抽屉关闭时,listview会自行重置并在再次打开抽屉时重新渲染它。

解决方案是保存所选索引,并在getView方法中为该索引的项目设置所需的bg颜色。

  1. 创建一个类级变量

    public static int NAV_CURR_SELECTION = 2; // you have used 2 in init()
    
  2. 添加此

    NAV_CURR_SELECTION = position;
    

    onItemClick()方法

  3. 现在,在onDrawerOpened()

    中添加这些行
    lvNavItems.setItemChecked(NAV_CURR_SELECTION, true);
    lvNavItems.setSelection(NAV_CURR_SELECTION);
    
  4. 修改:而不是使用上面onDrawerOpened()中提到的两行来执行此操作。 (仅更改步骤3. 1和2需要保持相同)

    View v = lvNavItems.getChildAt(NAV_CURR_SELECTION);
    v.setBackgroundColor(0xFF00FF00); // or whatever color you need