I have used a DrawerLayout with a ListView to generate a sliding menu. I want to do the following things;
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?
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
<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
<?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
<?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"));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.