Android NavigationView菜单切换按钮

时间:2016-03-19 21:07:45

标签: java android

我想在我的NavigationView中使用切换按钮或至少复选框来获取来自它的控制通知功能(开/关)。

在“menu.xml”文件的帮助下完成布局。

我尝试添加checkable="true",但它没有显示任何内容。 如何实施?

    

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/profile"
            android:icon="@drawable/ic_account_grey600_48dp"
            android:title="@string/profile" />
        <item
            android:id="@+id/tutorial"
            android:icon="@drawable/ic_help_grey600_48dp"
            android:title="@string/tutorial" />
<item
    android:id="@+id/checkable_menu"
    android:checkable="true"
    android:title="@string/haircut_notifications" />

    </group>

    <group
        android:id="@+id/menu_bottom"
        android:checkableBehavior="none">
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_share_variant_grey600_48dp"
            android:title="@string/share" />
        <item
            android:id="@+id/selfie_videos"
            android:icon="@drawable/ic_camera_front_variant_grey600_48dp"
            android:title="@string/selfie_videos" />
    </group>

</menu>

编辑:

Google M的一个很好的例子就像我想要实现切换一样。

有任何想法吗?

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以尝试使用app:actionLayout =&#34; @ layout / item&#34 ;, item.xml是您切换按钮的位置。

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

 <item android:title="@string/settings_info">
        <menu>

    <item android:id="@+id/nav_number"
          android:icon="@drawable/ic_room"
          android:title="@string/number"
          app:actionLayout="@layout/item"/>

    </item>

</menu>


/*How to handle the click event*/
@Override public boolean onNavigationItemSelected(MenuItem item) { 

// Handle navigation view item clicks. 
    switch (item.getItemId()){ 
        case R.id.nav_network: 
            startActivity(new Intent(this, NetworkActivity.class)); break; 
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
}

答案 1 :(得分:2)

而不是菜单使用回收器视图,以便您可以更好地控制项目。以下代码经过测试并正常运行。

这是输出: ScreenShot

第一步:创建项目布局:

item_normal.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="wrap_content"
android:orientation="horizontal">

<ImageView
    android:id="@+id/image"
    android:layout_marginLeft="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp"
    android:text="Title" />

</LinearLayout>

没什么好看的只是图像和文字

第2步:对于我们的切换布局,我们可以创建item_toggle.xml

item_toggle.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/textCustom"
    android:layout_marginLeft="16dp"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Notifications" />

<ToggleButton
    android:layout_marginRight="16dp"
    android:id="@+id/toggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:text="New ToggleButton" />
 </RelativeLayout>

步骤3:让我们的回收者视图创建适配器,它将在我们的导航视图中

CustomAdapter.java

public class CustomAdapter extends RecyclerView.Adapter {

String Tag = CustomAdapter.class.getSimpleName();

private final int NORMAL_VIEW = 88; // code for normal view type
private final int CUSTOM_VIEW = 99; //code for loader type

private Context context;
private ArrayList<Model> listingData = new ArrayList<>();

public CustomAdapter(Context context) {
    this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    if (viewType == NORMAL_VIEW) {

        View normalView = inflater.inflate(R.layout.item_normal, parent, false);
        return new NormalItem(normalView);

    } else if (viewType == CUSTOM_VIEW) {

        View customView = inflater.inflate(R.layout.item_toogle, parent, false);
        return new CustomItem(customView);
    } else return null; // unknown type.
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    if (holder instanceof NormalItem) {
        // normal list item.
        ((NormalItem) holder).textView.setText(listingData.get(position).getName());
        ((NormalItem) holder).icon.setImageResource(R.mipmap.ic_launcher);

    } else if (holder instanceof CustomItem)
    {
        ((CustomItem) holder).textViewToggle.setText("On");
        ((CustomItem) holder).textViewToggle.setText("Notifications");
    }
}

@Override
public int getItemCount() {
    return listingData.size();
}

@Override
public int getItemViewType(int position) {

    if (position == 3) return CUSTOM_VIEW;
    else return NORMAL_VIEW;
}

public class NormalItem extends RecyclerView.ViewHolder {

    ImageView icon;
    TextView textView;

    public NormalItem(View itemView) {
        super(itemView);

        icon = (ImageView) itemView.findViewById(R.id.image);
        textView = (TextView) itemView.findViewById(R.id.text);

    }
}

public class CustomItem extends RecyclerView.ViewHolder {

    ToggleButton toggleButton;
    TextView textViewToggle;

    public CustomItem(View itemView) {
        super(itemView);

        toggleButton = (ToggleButton) itemView.findViewById(R.id.toggleButton);
        textViewToggle = (TextView) itemView.findViewById(R.id.textCustom);

    }
}


/**
 * method to update the adapter and notify the adapter item by item
 *
 * @param data data to be updated.
 */
public void updateAdapter(ArrayList<Model> data) {

    try {
        for (Model item : data) {
            listingData.add(item);
            notifyItemInserted(getItemCount() - 1);
        }
    } catch (Exception e) {
        Log.e(Tag, e.toString() + "");
    }

}
}

第4步:创建activity_main.xml

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout             xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".main.MainActivity">


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="16dp"
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />


    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

第5步:让我们创建一个简单的类来保存我们的数据

Model.java

  public class Model {

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getItem() {
    return item;
}

public void setItem(int item) {
    this.item = item;
}

String name;
int item;
 }

第6步:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar        xmlns:android="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/android"
app:id="@+id/toolbar"
android:elevation="4dp"
app:layout_width="match_parent"
app:layout_height="wrap_content"
app:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimary"
/>

最后一步:创建MainActiivty.class

MainActivity.class

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;
private RecyclerView recyclerView;
private ArrayList<Model> data;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Custom Nav ");
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);

    ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
    drawerLayout.addDrawerListener(mToogle);
    mToogle.syncState();

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    LinearLayoutManager manager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(manager);

    CustomAdapter adapter = new CustomAdapter(this);
    recyclerView.setAdapter(adapter);

     data = new ArrayList<>();

    Model item1 = new Model();
    item1.setName("Option 1");
    item1.setItem(R.mipmap.ic_launcher);

    Model item2 = new Model();
    item2.setName("Option 2");
    item2.setItem(R.mipmap.ic_launcher);

    Model item3 = new Model();
    item3.setItem(R.mipmap.ic_launcher);
    item3.setName("Option 3");

    Model item4 = new Model();
    item4.setItem(R.mipmap.ic_launcher);
    item4.setName("Option 4");


    data.add(item1);
    data.add(item2);
    data.add(item3);
    data.add(item4);
    adapter.updateAdapter(data);
}

}

您可以根据需要添加标题并修改UI。希望能帮助到你。 快乐的编码。