如何访问抽屉应用程序中的布局

How to access a layout in a drawer application

我启动了一个带抽屉的应用程序,我想访问一个与初始化布局不同的特定布局

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private static final String TAG = "MainActivity";

    private View navHeader;


    @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.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();
        final StringBuffer sb = new StringBuffer("");
        ArrayList<Countries> country = new ArrayList<Countries>();

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        navHeader= (View) findViewById(R.id.content_main).gethea
        if (id == R.id.nav_camera) {

       //     setContentView(R.layout.content_main);
     //      ListView list = (ListView) findViewById(R.id.content_main)
            // Handle the camera action

            /**********************************************************************************************************/
        where i want to access that view to insert data in the listview

        /****************************************************************************************************************/

        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

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

此代码由 Android Studio 生成,如您所见,setContentView 方法是用 activity_main 初始化的,我想访问另一个布局 content_main,其中我放了一个 ListView 来显示数据。您能否告诉我如何访问该布局以与之交互?

我希望您能看到一些创建导航抽屉布局的示例。 https://www.journaldev.com/9958/android-navigation-drawer-example-tutorial

步骤简单

  • 创建片段
  • 将此代码写在 if(id == R.id.nav_camera) 中以从导航布局导航到该片段

    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.container,YOUR_FRAGMENT);
    transaction.addToBackStack(null); 
    transaction.commit();
    
  • 在片段内创建列表视图或 Recyclerview,以便列出项目。