导航抽屉中的ViewPager - 请提供一些建议

时间:2017-07-19 20:28:46

标签: java android android-layout android-fragments navigation

所以总的想法是在第一个(主页面)有滑动画廊我做了但是当我从导航菜单中选择选项我有寻呼机布局女巫是我的导航抽屉布局及其空白cuz只包含 android.support.v4.view.ViewPager

CONTENT_MAIN.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/pager"

app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.marcj.justtest.drawer"
>
</android.support.v4.view.ViewPager>

但现在我想去我的导航抽屉选择那里的选项,并有其他刷卡库..但我不知道怎么做,因为当我选择选项例如已售出我的布局空白。

enter image description here

drawer.java文件:

package com.example.marcj.justtest;

import java.math.BigDecimal;

public class drawer extends AppCompatActivity implements         
NavigationView.OnNavigationItemSelectedListener {

ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);

    viewPager = (ViewPager) findViewById(R.id.pager);
    PageAdapter padapter = new PageAdapter(getSupportFragmentManager());
    viewPager.setAdapter(padapter);





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

    View headerView = navigationView.getHeaderView(0);

    String username = getIntent().getStringExtra("username");

    TextView text2 = (TextView) headerView.findViewById(R.id.TVusername);
    text2.setText(username);



}








@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.navigation, 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;

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

        Toast.makeText(this, "Contact Form", Toast.LENGTH_SHORT).show();


        Intent i = new Intent(drawer.this, AcContact.class);
        startActivity(i);

        contact contactForm = new contact();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.pager,
                contactForm, contactForm.getTag()).commit();

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

    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.nav_homepage) {

        Intent i= new Intent (drawer.this, drawer.class );
        startActivity(i);

} else if (id == R.id.nav_clicked) {
        Toast.makeText(this, "Clicked Items", Toast.LENGTH_SHORT).show();

        clicked clickedItems = new clicked();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.pager,
                clickedItems, clickedItems.getTag()).commit();


    } else if (id == R.id.nav_won) {
        Toast.makeText(this, "Won Items", Toast.LENGTH_SHORT).show();

        won wonItems = new won();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.pager,
                wonItems, wonItems.getTag()).commit();

    } else if (id == R.id.nav_selling) {
        Toast.makeText(this, "Item that you Selling", 
   Toast.LENGTH_SHORT).show();

        selling sellingItems = new selling();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.pager,
                sellingItems, sellingItems.getTag()).commit();

    } else if (id == R.id.nav_sold) {
        Toast.makeText(this, "Sold Items", Toast.LENGTH_SHORT).show();

        sold soldItems = new sold();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.pager,
                soldItems, soldItems.getTag()).commit();

    } else if (id == R.id.nav_topup) {
        Toast.makeText(this, "Top-Up Clicks", Toast.LENGTH_SHORT).show();

        topup topupItems = new topup();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.pager,
                topupItems, topupItems.getTag()).commit();

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





    }

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

PageAdapter.java

package com.example.marcj.justtest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class PageAdapter extends FragmentPagerAdapter {

public PageAdapter (FragmentManager fm){

    super(fm);
}

public Fragment getItem (int arg0){

    switch (arg0){

        case 0:
            return new FragmentOne();

        case 1:
            return new FragmentTwo();

        case 2:
            return new FragmentThree();


        default:
            break;
    }

    return null;
}



public int getCount(){

    return 3;
}
}

Activity_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
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">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

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

    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_drawer_menu" />

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

app_bar_main.xml

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

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme" />

</android.support.design.widget.AppBarLayout>

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



</android.support.design.widget.CoordinatorLayout>

activity_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
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">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

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

    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_drawer_menu" />

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

1 个答案:

答案 0 :(得分:0)

activity_drawer文件:

android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_drawer_menu" />

<强> content_main

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/pager"

app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.marcj.justtest.drawer"
>