查看列表 - Android Studio =导航抽屉布局

时间:2016-11-10 11:50:27

标签: android listview android-studio android-fragments navigation-drawer

我是Android Studio的初学者,我需要帮助。我正在尝试在导航抽屉布局上制作应用程序。所以,当我点击导航抽屉菜单中的某些内容时,我找到了一个如何获得特定布局的教程

https://www.youtube.com/watch?v=ju837bQOBfg&list=PLQIC834Sj9LcamgQ8JhpsRqPD9NFNjG0Q

作品完美!

刚才我想在一个布局中创建ViewList,我是从主导航菜单中引用的,所以我找到了下一个教程(http://www.viralandroid.com/2016/04/start-new-activity-from-android-listview-onitem-clicked.html)。

我尝试将ListView Tutorial应用于一个全新的项目,它完美无缺!问题是当我想将这个应用到我的项目时,我已经在片段的特定布局上使用了导航。它说有一些重复。

这是我的MainActivity:

package com.example.martin.ultimateenglish;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        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.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

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

        if (id == R.id.list) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.content_frame
                            , new First_Layout())
                    .commit();

        } else if (id == R.id.nav_changelog) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.content_frame
                            , new Changelog())
                    .commit();

        }

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

你可以帮帮我吗?看来我不能多次使用一个班级的名字但是现在让这两个教程一起难以置信!我只想让菜单项的导航抽屉布局参考我要应用Viewlist的特定布局。

感谢您的每一条建议或答案。

Martin U.

0 个答案:

没有答案