简体   繁体   English

如何更改选定的SlidingMenu行的背景颜色并保持不变,直到选择了其他行?

[英]How to change the background color of the selected SlidingMenu row and keep it the same till some other row is selected?

I have used a DrawerLayout with a ListView to generate a sliding menu. 我使用了带有ListView的DrawerLayout来生成滑动菜单。 I want to do the following things; 我想做以下事情;

  1. I want to change the color of my selected listrow to black and keep it the same till I select some other row, ie when I click on mytask row in the sliding menu, that row should be highlighted to black and when I open the sliding menu again I want that row still to be highlighted till I select some other row.how can I do it? 我想将所选列表行的颜色更改为黑色,并保持不变,直到选择其他行,即,当我单击滑动菜单中的mytask行时,该行应突出显示为黑色,并且当我打开滑动菜单时我再次希望该行仍然突出显示,直到选择其他行。我该怎么做?

  2. I want to make a orange color view visible when that row is clicked. 我想在单击该行时使橙色视图可见。 At present my view gets visible,but when I select some other row the previous rows view still is visible. 目前,我的视图可见,但是当我选择其他行时,以前的行视图仍然可见。 How to fix that? 如何解决?

My codes are as follows.please guide me step by step. 我的代码如下。请逐步指导我。

activity_home.xml activity_home.xml

  <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">


<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />




<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:visibility="visible"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="0dp"
     android:listSelector="@drawable/sel"
    android:background="#3e3e3e"/>


 </android.support.v4.widget.DrawerLayout>

custom_drawer.xml custom_drawer.xml

        <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" 
>

<LinearLayout
  android:id="@+id/itemLayout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:orientation="vertical"

  android:layout_marginTop="0dp"

 >

          <RelativeLayout

              android:layout_width="fill_parent"
              android:layout_height="60dp"


             >

              <ImageView
                  android:id="@+id/drawer_icon"
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:layout_marginLeft="25dp"
                  android:layout_marginTop="7dp" />

              <TextView
                  android:id="@+id/drawer_itemName"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerVertical="true"
                 android:textSize="14dp"
                  android:layout_marginLeft="24dp"
                  android:textColor="@android:color/white"
                  android:layout_toRightOf="@+id/drawer_icon"
                  android:text="TextView" />
               <View
  android:layout_width="2dp"
  android:layout_height="fill_parent"
  android:background="@android:color/black"
  android:layout_marginBottom="0.01dp"
  android:layout_marginTop="0.01dp"
  android:layout_marginLeft="9dp"



   ></View>

               <ImageView
                   android:id="@+id/imageView1"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_alignParentRight="true"
                   android:layout_alignTop="@+id/drawer_itemName"
                   android:layout_marginRight="18dp"
                   android:src="@android:drawable/arrow_down_float" />

               <View
                   android:id="@+id/vieworange"
                   android:layout_width="8dp"
                   android:layout_height="wrap_content"
                   android:layout_alignParentRight="true"
                   android:layout_alignParentTop="true"
                   android:background="@drawable/menushade"
                   android:visibility="visible" />

          </RelativeLayout>

           <View
  android:layout_width="match_parent"
  android:layout_height="0.01dp"
  android:background="@android:color/black"
  android:layout_marginBottom="0.01dp"
  android:layout_marginTop="0.01dp"



   ></View>

</LinearLayout>

<View
    android:id="@+id/vieworangelist"
    android:layout_width="8dp"
    android:layout_height="60dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:visibility="gone"
    android:background="@color/orange" />

 </RelativeLayout>

selector.xml in drawable folder 可绘制文件夹中的selector.xml

  <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@android:color/black" android:state_activated="false"/>
<item android:drawable="@android:color/black" android:state_activated="false" android:state_pressed="false"/>
<item android:drawable="@android:color/black" android:state_pressed="true"/>
<item android:drawable="@android:color/black" android:state_enabled="true" ></item>
<item android:drawable="@android:color/black" android:state_activated="true"/>



 </selector>

Adapter class 转接器类别

        @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    DrawerItemHolder drawerHolder;
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        drawerHolder = new DrawerItemHolder();
        String font="font/Dosis-SemiBold.otf";
        //final Typeface tp=Typeface.createFromAsset(.getAssets(), font);
        final Typeface tp=Typeface.createFromAsset(getContext().getAssets(), font);

        view = inflater.inflate(layoutResID, parent, false);
        drawerHolder.ItemName = (TextView) view
                .findViewById(R.id.drawer_itemName);
        drawerHolder.ItemName.setTypeface(tp);
        drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
  drawerHolder.vieworange=(View)view.findViewById(R.id.vieworangelist);
    //drawerHolder.vieworange.setVisibility(view.VISIBLE);

        view.setTag(drawerHolder);

    } else {
        drawerHolder = (DrawerItemHolder) view.getTag();

    }

    DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
            drawerHolder.ItemName.setText(dItem.getItemName());
    if(dItem.getImgResID()!=0)
    {
    drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
            dItem.getImgResID()));
    }
    else
    {
    drawerHolder.icon.setImageBitmap(dItem.bmp);
    }


    return view;
}

private static class DrawerItemHolder {
    TextView ItemName;
    ImageView icon;
    View vieworange;
}
}

Use this function and call it each time you want to change the row color. 每次要更改行颜色时,使用此函数并调用它。 What this function does is changing all the other row's background to it's default color, and the pressed row's background to the new color. 此功能的作用是将另一行的所有背景更改为默认颜色,并将按下的行的背景更改为新颜色。

public void colorMenuRow(ListView lv, int position)
{
    // Changing all rows to default background color
    for (int i = 0; i < lv.getCount(); i++) {
        View listRow = (View) lv.getChildAt(i);
        if(listRow != null)
        listRow.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    // Changing current row color
    View view = (View) lv.getChildAt(position);
    view.setBackgroundColor(Color.parseColor("#000000"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改“滑动”上的Listview行的颜色,并使其保持不变,直到选择了其他行? - how to change the color of a Listview row on Swipe and let it remain till some other row is selected? 如何更改表格行的选定背景色 - how to change the selected background color of table row 如何在Android中更改所选行的背景颜色? - How to change selected row background color in Android? 如何在Android中更改选定的ListView行颜色? - How to change selected listview row color in android? 如何设置Listview交替行颜色和所选项目背景颜色 - How to set Listview alternate row color and selected item background color 如何更改 Android Navigation Drawer 的选定行的颜色? - How to change the color of Android Navigation Drawer's Selected Row? 如何在 MaterialButtonToggleGroup 上更改具有相同不透明度的选定背景颜色 - How to change the selected background color with the same opacity on MaterialButtonToggleGroup 在列表视图中更改所选项目或行的背景颜色 - Change background colour of selected item or row in listview Android RadioButton,在另一行中选择了相同的值 - RadioButton Android, selected the same value in other row Android:如何为ListView的选定行中的2 TextView着色 - Android: How to color the 2 TextView in the selected row of a ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM