gpt4 book ai didi

android - 在抽屉导航中突出显示所选项目

转载 作者:行者123 更新时间:2023-11-29 01:18:27 25 4
gpt4 key购买 nike

我使用 ActionBarDrawerToggleDrawerLayout 手动制作了抽屉导航,现在我希望选中的项目在打开抽屉或关闭抽屉时保持突出显示( fragment )应该使用某种颜色突出显示。我的抽屉里有一个 ListView。这是 drawer.xml( fragment )

<?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的代码,如果你需要看这个文件,我可以更新那个。如果需要,请发表评论。

ListView 的 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;
}


}

这是我如何在 fragment 中导航的实现

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();

}
});

最佳答案

按照以下步骤操作。

在您的适配器中创建一个如下所示的变量和一个 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
}

在您的主 Activity 中,通过在 onItemclick 下传递位置来调用 setSelectedItem。

adapter.setSelectedItem(position);

到目前为止,我已经更改了突出显示的文本颜色。您可以按照相同的方式将其设置为背景。检查它是否适合您。

关于android - 在抽屉导航中突出显示所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com