我使用ActionBarDrawerToggle
和DrawerLayout
手动制作导航抽屉,现在我希望所选项目保持高亮显示,当我打开抽屉或关闭它时,所选项目(片段)应使用某种颜色突出显示。我的抽屉里有一个ListView
。
这是drawer.xml(片段)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#34344d"
android:orientation="vertical" >
<ListView
android:id="@+id/drawerlist_1"
android:dividerHeight="0dp"
android:divider="#fffff7"
android:layout_weight="1"
android:listSelector="@drawable/list_view_scolor"
android:layout_width="fill_parent"
android:layout_height="0dp" >
</ListView>
</LinearLayout>
我有listSelector文件,它是list_view_scolor:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/default_color" />
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/default_color" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/list_view_listselector" />
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/list_view_listselector" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/list_view_listselector" />
</selector>
我有list_view_listselector.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/pressed_color" />
</shape>
我还没有发布single_row的代码,如果你需要看到这个文件,我可以更新它。如果需要,请发表评论。
列表视图的BaseAdapter: -
public class ListView_Adapter extends BaseAdapter {
Context context;
String[] list;
LayoutInflater inflater;
public ListView_Adapter(Context context , String[] list ) {
this.context = context;
this.list = list;
inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
view= inflater.inflate(R.layout.cust_row_listview, null);
TextView drawer_item = (TextView) view.findViewById(R.id.drawer_item);
String item = list[position];
drawer_item.setText(item);
Typeface type = Typeface.createFromAsset(context.getAssets(),
"robot_condensed_light.ttf");
drawer_item.setTypeface(type);
/* if(position==0){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_1)));
}
else if(position == 1){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_2)));
}
else if(position == 2){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_3)));
}
else if(position == 3){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_4)));
}
else if(position == 4){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_5)));
}*/
return view;
}
}
这是我如何浏览片段
的实现lv = (ListView) findViewById(R.id.drawerlist_1);
ListView_Adapter adapter1 = new ListView_Adapter(this, item1);
lv.setAdapter(adapter1);
Fragment_Home frag = new Fragment_Home();
fragManager = getSupportFragmentManager();
fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//lv.setItemChecked(position, true);
//v.setSelection(position);
lv.setSelected(true);
Fragment frag;
if (position == 0) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(getResources().getColor(R.string.color_list_2)));
getSupportActionBar().setTitle("Home");
frag = new Fragment_Home();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
} else if (position == 1) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_2))));
frag = new Fragment_PaytmWallet();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("PayTM Wallet");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_2)));
// }
} else if (position == 2) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_3))));
frag = new Fragment_Categories();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("Categories");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_3)));
// }
} else if (position == 3) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_4))));
frag = new Fragment_AskAQues();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("Ask a Question");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_4)));
// }
} else if (position == 4) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_5))));
frag = new Fragment_BeAnExpert();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("Be an Expert");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_5)));
// }
}
dl.closeDrawers();
}
});
答案 0 :(得分:1)
请按照以下步骤操作。
在你的适配器中创建一个像下面这样的变量和一个getter函数。
int mSelectedItem;
public void setSelectedItem(int selectedItem) {
this.mSelectedItem = selectedItem;
notifyDataSetChanged();
}
你的getView方法下的包括以下内容。
if (position == mSelectedItem) {
drawer_item.setTextColor.setBackgroundColor(ContextCompat.getColor(context, R.color.selected_color)); // Highlighting color
} else {
drawer_item.setTextColor(ContextCompat.getColor(context, android.R.color.normal_color)); // normal color
}
在你的mainactivity中,通过在onItemclick下传递位置来调用setSelectedItem。
adapter.setSelectedItem(position);
到目前为止,我已更改突出显示的文字颜色。您可以按照相同的方式将其设置为背景。检查它是否适合你。