Android实现菜单切换背景,更改NavigationView的单个特定菜单项的背景颜色

更改单个特定菜单项的背景颜色

AFAIK使用菜单,您无法创建自定义菜单navigationView

当您用来BackgroundColorSpan为菜单项设置背景时,仅将背景设置为菜单项标题,而不是整个视图

输出使用 BackgroundColorSpan

46949039a7d87919321d85e694b62d96.png

使用以下方式尝试 RecyclerView

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

tools:openDrawer="start">

layout="@layout/app_bar_main"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android:id="@+id/nav_view"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_gravity="start"

android:fitsSystemWindows="true">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/navRecyclerView"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

主要活动

public class MainActivity extends AppCompatActivity

implements NavigationView.OnNavigationItemSelectedListener {

RecyclerView navRecyclerView;

LinearLayoutManager layoutManager;

ArrayList arrayList = new ArrayList<>();

NavigationAdapter adapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(

this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

drawer.addDrawerListener(toggle);

toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setNavigationItemSelectedListener(this);

navRecyclerView = findViewById(R.id.navRecyclerView);

navRecyclerView.setHasFixedSize(true);

layoutManager = new LinearLayoutManager(this);

navRecyclerView.setLayoutManager(layoutManager);

initArray();

adapter = new NavigationAdapter(this, arrayList);

navRecyclerView.setAdapter(adapter);

}

private void initArray() {

NavigationDataModel model = new NavigationDataModel();

model.setColor(ContextCompat.getColor(this, R.color.colorPrimary));

model.setIcon(R.drawable.ic_menu_gallery);

model.setTitle("Item 1");

arrayList.add(model);

NavigationDataModel model2 = new NavigationDataModel();

model2.setColor(ContextCompat.getColor(this, R.color.colorRed));

model2.setIcon(R.drawable.ic_menu_camera);

model2.setTitle("Item 2");

arrayList.add(model2);

NavigationDataModel model3 = new NavigationDataModel();

model3.setColor(ContextCompat.getColor(this, R.color.colorGreen));

model3.setIcon(R.drawable.ic_menu_send);

model3.setTitle("Item 3");

arrayList.add(model3);

NavigationDataModel model4 = new NavigationDataModel();

model4.setColor(ContextCompat.getColor(this, R.color.colorPink));

model4.setIcon(R.drawable.ic_menu_share);

model4.setTitle("Item 4");

arrayList.add(model4);

}

@Override

public void onBackPressed() {

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

if (drawer.isDrawerOpen(GravityCompat.START)) {

drawer.closeDrawer(GravityCompat.START);

} else {

super.onBackPressed();

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

@SuppressWarnings("StatementWithEmptyBody")

@Override

public boolean onNavigationItemSelected(MenuItem item) {

// Handle navigation view item clicks here.

int id = item.getItemId();

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

drawer.closeDrawer(GravityCompat.START);

return true;

}

}

导航适配器

public class NavigationAdapter extends RecyclerView.Adapter {

Context context;

ArrayList arrayList = new ArrayList<>();

public NavigationAdapter(Context context, ArrayList arrayList) {

this.context = context;

this.arrayList = arrayList;

}

@NonNull

@Override

public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, parent, false);

return new ViewHolder(view);

}

@Override

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.navIcon.setImageResource(arrayList.get(position).getIcon());

holder.rootView.setBackgroundColor(arrayList.get(position).getColor());

holder.navTitle.setText(arrayList.get(position).getTitle());

}

@Override

public int getItemCount() {

return arrayList.size();

}

public class ViewHolder extends RecyclerView.ViewHolder {

ImageView navIcon;

TextView navTitle;

LinearLayout rootView;

public ViewHolder(View itemView) {

super(itemView);

rootView = itemView.findViewById(R.id.rootView);

navIcon = itemView.findViewById(R.id.navIcon);

navTitle = itemView.findViewById(R.id.navTitle);

}

}

}

NavigationDataModel

public class NavigationDataModel {

private int icon, color;

private String title;

public int getIcon() {

return icon;

}

public void setIcon(int icon) {

this.icon = icon;

}

public int getColor() {

return color;

}

public void setColor(int color) {

this.color = color;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

}

输出值

d6bda1e35ec51f4a60c0647d7f1c6a1f.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值