如何更改所选 SlidingMenu 行的背景颜色并保持不变,直到选择其他行?
Posted
技术标签:
【中文标题】如何更改所选 SlidingMenu 行的背景颜色并保持不变,直到选择其他行?【英文标题】:How to change the background color of the selected SlidingMenu row and keep it the same till some other row is selected? 【发布时间】:2014-06-16 18:00:42 【问题描述】:我使用了带有 ListView 的 DrawerLayout 来生成滑动菜单。我想做以下事情;
我想将所选列表行的颜色更改为黑色并保持不变,直到我选择其他行,即当我单击滑动菜单中的 mytask 行时,该行应突出显示为黑色并且当我再次打开滑动菜单时,我希望该行仍然突出显示,直到我选择其他行。我该怎么做?
我希望在单击该行时显示橙色视图。目前我的视图变得可见,但是当我选择其他行时,前一行视图仍然可见。如何解决?
我的代码如下。请指导我一步一步。
activity_home.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_
android:layout_>
<FrameLayout
android:id="@+id/content_frame"
android:layout_
android:layout_ />
<ListView
android:id="@+id/left_drawer"
android:layout_
android:visibility="visible"
android:layout_
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
>
<LinearLayout
android:id="@+id/itemLayout"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_marginTop="0dp"
>
<RelativeLayout
android:layout_
android:layout_
>
<ImageView
android:id="@+id/drawer_icon"
android:layout_
android:layout_
android:layout_marginLeft="25dp"
android:layout_marginTop="7dp" />
<TextView
android:id="@+id/drawer_itemName"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/menushade"
android:visibility="visible" />
</RelativeLayout>
<View
android:layout_
android:layout_
android:background="@android:color/black"
android:layout_marginBottom="0.01dp"
android:layout_marginTop="0.01dp"
></View>
</LinearLayout>
<View
android:id="@+id/vieworangelist"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone"
android:background="@color/orange" />
</RelativeLayout>
可绘制文件夹中的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>
适配器类
@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;
【问题讨论】:
【参考方案1】:使用此函数并在每次要更改行颜色时调用它。该函数的作用是将所有其他行的背景更改为默认颜色,并将按下行的背景更改为新颜色。
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"));
【讨论】:
以上是关于如何更改所选 SlidingMenu 行的背景颜色并保持不变,直到选择其他行?的主要内容,如果未能解决你的问题,请参考以下文章