Java 导航抽屉-带有ListView的标题视图
我目前正在创建和定义一个导航抽屉。我现在想要一个标题视图,就像谷歌应用程序上的标题视图一样,位于项目行上方。我只找到了RecyclerViews的例子,我不想使用它。我已经完成了ListView和所有其他内容。也许有人可以帮我:)提前谢谢你可以使用android设计支持库创建Java 导航抽屉-带有ListView的标题视图,java,android,navigation-drawer,Java,Android,Navigation Drawer,我目前正在创建和定义一个导航抽屉。我现在想要一个标题视图,就像谷歌应用程序上的标题视图一样,位于项目行上方。我只找到了RecyclerViews的例子,我不想使用它。我已经完成了ListView和所有其他内容。也许有人可以帮我:)提前谢谢你可以使用android设计支持库创建NavigationView,而不用费力创建listview或RecyclerView,这都是由android创建的 要将其添加到您的项目中,您需要将android设计支持库添加到您的项目中,请在build.gradle中添
NavigationView
,而不用费力创建listview或RecyclerView,这都是由android创建的
要将其添加到您的项目中,您需要将android设计支持库添加到您的项目中,请在build.gradle中添加以下行
compile 'com.android.support:design:22.2.0
查看android设计支持功能
首先创建一个头(header.xml)
app:headerLayout
是我们在步骤1中创建的header.xml。
app:menu
是菜单资源项,即drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/first1"
android:checked="false"
android:icon="@drawable/icon1"
android:title="@string/string1" />
<item
android:id="@+id/second2"
android:checked="false"
android:icon="@drawable/icon2"
android:title="@string/string2" />
</menu>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
初始化NavigationView并调用setNavigationItemSelectedListener以获取单击事件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment
case R.id.first1:
SomeFragment fragment = new SomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
return true;
...................
创建navigationview go的分步过程
它会是什么样子:
使用支持设计库中的导航抽屉非常感谢,但我还有一个问题。现在我有一个actionbar,没有定义我自己的工具栏,但是现在我需要一个appcompat样式,但是当我使用某个父对象时,我得到一个错误:“尝试调用虚拟方法‘android.content.Context android.app.actionbar.getThemedContext()’,该方法在设置适配器的导航抽屉中调用。”。你有什么解决问题的建议吗?:)或者我应该创建工具栏吗?你能把它作为一个单独的问题发布吗,因为需要更多的细节来解决它。它也会吸引更多的注意力,它们也会有帮助。创建工具栏后,你可以将问题发布在这里…问题是:如果我的答案对你有帮助,你可以标记我的答案…我会研究其他答案问题我也把我对你另一个问题的回答贴出来了。请检查一下。如果答案对你有帮助,别忘了给它加上分数
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {
............................................
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment
case R.id.first1:
SomeFragment fragment = new SomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
return true;
...................