简体   繁体   English

导航抽屉中的选定项目

[英]Selected item in navigation drawer

I implemented the navigation drawer in my app ( http://developer.android.com/training/implementing-navigation/nav-drawer.html ). 我在应用程序中实现了导航抽屉( http://developer.android.com/training/implementing-navigation/nav-drawer.html )。

How can I change the display of the actually selected item? 如何更改实际选择的项目的显示? I want the selected item to be bold and a different color. 我希望所选项目为粗体且颜色不同。

In the official example, they use "setItemChecked", but how can I modify the aspect of the single checked item? 在官方示例中,它们使用“ setItemChecked”,但是如何修改单个选中项的外观? The "setOnItemSelectedListener" method doesn't work. “ setOnItemSelectedListener”方法无效。

So far, what I do is this piece of code when I open a fragment: 到目前为止,我要做的就是打开片段时的这段代码:

for (int i = 0; i < mDrawerList.getChildCount(); i++){
    TextView t = (TextView) mDrawerList.getChildAt(i);

    if(i == fragmentPosition) t.setTextColor(getResources().getColor(R.color.blue));
    else t.setTextColor(getResources().getColor(R.color.black));
}

It works, but on app launch, no item is selected, and I can't set any item because the listview isn't created yet (it's created when we open it the first time, I think). 它可以工作,但是在应用程序启动时,没有选择任何项目,并且我无法设置任何项目,因为还没有创建列表视图(我认为它是在我们第一次打开它时创建的)。

I tried creating a selector, but don't know how to set the different attributes correctly (item checked, item selected, item clicked??), searched the docs but I don't understand. 我尝试创建一个选择器,但不知道如何正确设置不同的属性(选中项目,选中项目,单击项目?),搜索了文档,但我不理解。

Thanks for any help you can provide! 感谢您的任何帮助,您可以提供!

I would do it in adapter. 我会在适配器中做到这一点。

mAdapter.setSelectedItem(position);

public View getView(...) {
    ...
    if (position == mSelectedItem) {
        text.setTypeface(...);
        text.setBackgroundColor(...);
    } else {
        text.setTypeface(...);
        text.setBackgroundColor(...);
    }
}

And if you don't need to set typeface you can use selector with 如果您不需要设置字体,则可以将选择器与

setTextColor(@drawable/my_selector).

Or put it to your xml file with TextView 或者使用TextView将其放入您的xml文件中

android:textColor="@drawable/my_selector"

Here's my solution to change the color and typeface of the selected item in the navigation drawer... 这是我更改导航抽屉中所选项目的颜色和字体的解决方案...

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
     setNavDrawerItemNormal();
     TextView txtview = ((TextView) view.findViewById(R.id.txtNav));
     txtview.setTypeface(null, Typeface.BOLD);
     txtview.setTextColor(R.color.Red);
}

public void setNavDrawerItemNormal()
{
    for (int i=0; i< mDrawerListView.getChildCount(); i++)
    {
        View v = mDrawerListView.getChildAt(i);
        TextView txtview = ((TextView) v.findViewById(R.id.txtNav));
        Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf");
        txtview.setTypeface(font);
        txtview.setTextColor(R.color.Red);
    }
}

And to bold the first item in the navigation drawer upon initialization of the app, I did that in the list adapter get view method... 为了在应用初始化时加粗导航抽屉中的第一项,我在列表适配器get view方法中做到了……

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.navdrawer_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.txtNav);
    textView.setText(values[position]);
    if (position == 0) 
    { 
       textView.setTypeface(null, Typeface.BOLD);
       textView.setTextColor(R.color.Red);
    }

    return rowView;
}

So, in here i checked if the position of the item is 0 (meaning its the first item), then make it bold. 因此,在这里,我检查了项目的位置是否为0(表示其为第一个项目),然后将其设为粗体。 This whole thing works perfectly for me!~ 这整个事情对我来说很完美!〜

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM