使用SlidingMenu库时如何更改listview项目的颜色和图标?

时间:2015-03-24 11:46:04

标签: android listview slidingmenu

我正在使用SlidingMenu库在我的项目中创建侧面菜单。我需要在点击时更改所选侧边菜单项的背景颜色和图标。如何做到这一点?

这是我在侧边菜单中设置listview项目的类。

public class SampleListFragment extends ListFragment {
public String[] menus = { "Home","Offers", "History", "Profile"};

public Integer[] icons = { R.drawable.icon_a,R.drawable.icon_b,
        R.drawable.icon_c,R.drawable.d};

MainActivity fca;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list, null);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    fca = (MainActivity) getActivity();

    SampleAdapter adapter = new SampleAdapter(getActivity());

    for (int i = 0; i < menus.length; i++) {
        adapter.add(new SampleItem(menus[i], icons[i]));
    }
    setListAdapter(adapter);

}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    Fragment newContent = null;

    switch (position) {
    case 0:

        newContent = new HomeFragment();

        break;

    case 1:

        newContent = new OffersFragment();

        break;

    case 2:
        newContent = new HistoryFragment();

        break;

    case 3:
        newContent = new ProfileFragment();

        break;

    }
    if (newContent != null)
        switchFragment(newContent);
}

private class SampleItem {
    public String tag;
    public int iconRes;

    public SampleItem(String tag, int iconRes) {
        this.tag = tag;
        this.iconRes = iconRes;
    }
}

private void switchFragment(Fragment fragment) {
    if (getActivity() == null)
        return;

    if (getActivity() instanceof MainActivity) {

        fca.switchContent(fragment);

    }

}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

    public SampleAdapter(Context context) {
        super(context, 0);
    }

    public View getView(final int position, View view, ViewGroup parent) {
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.row,
                    null);
        }
        ImageView icon = (ImageView) view.findViewById(R.id.row_icon);
        icon.setImageResource(getItem(position).iconRes);
        final TextView title = (TextView) view.findViewById(R.id.row_title);


        title.setText(getItem(position).tag);

        return view;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个......

      <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
<item android:drawable="@color/black" /> <!-- default -->
  </selector> 

答案 1 :(得分:0)

  1. 在listview中添加列表选择器。

    机器人:listSelector

  2. 在可绘制文件夹list_selecter.xml
  3. 中创建一个选择器

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@color/android:transparent" /> <item android:drawable="@color/red_light" /> </selector>

    1. 在listview中设置列表选择器

      机器人:listSelector =&#34; @可绘制/ list_selecter&#34;