从导航抽屉移动到相同活动内的片段

时间:2017-01-10 11:36:32

标签: android fragment

我只有一个简单的导航抽屉,其中一些线性布局水平排列。

<?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_dashboard"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#23282e"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

 <LinearLayout
                    android:id="@+id/dashboard_layout"
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:background="#2e353d"
                    android:layout_height="35dp">

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_marginLeft="30dp"
                        android:src="@mipmap/dashboard"
                        android:padding="3dp"
                        android:layout_height="match_parent" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="8dp"
                        android:textColor="#fff"
                        android:text="Dashboard"/>

                </LinearLayout>
 </android.support.design.widget.NavigationView>


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

现在点击此仪表板布局。我想移动到仪表板片段。请注意,Navigationdrawer和Dialog片段应该在同一个活动中。这是我的活动类:

public class Home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    LinearLayout dashboard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        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.dashboard, 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

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        dashboard= (LinearLayout) findViewById(R.id.dashboard_layout);

        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

我只是想知道如何实现这一点。为清楚起见,这是我想要的屏幕截图。

1 个答案:

答案 0 :(得分:1)

<强> 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">
    <ReletiveLayout    
          android:layout_width="match_parent"
        android:layout_height="match_parent">
    <include
    android:id="@+id/appBar"
            layout="@layout/app_bar_dashboard"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
            <FrameLayout  
            android:id="@+id/contentFrame"
            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appBar"> 
            </FrameLayout>
    </RelativeLayout>


        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#23282e"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">

     <LinearLayout
                        android:id="@+id/dashboard_layout"
                        android:layout_width="match_parent"
                        android:orientation="horizontal"
                        android:background="#2e353d"
                        android:layout_height="35dp">

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_marginLeft="30dp"
                            android:src="@mipmap/dashboard"
                            android:padding="3dp"
                            android:layout_height="match_parent" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="8dp"
                            android:textColor="#fff"
                            android:text="Dashboard"/>

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

<强>类

public class Home extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

LinearLayout dashboard;

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

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
     frame = (FrameLayout) findViewById(R.id.content_frame);

    toggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close) 
    {            
        @SuppressLint("NewApi")
        public void onDrawerSlide(View drawerView, float slideOffset)
        {
            super.onDrawerSlide(drawerView, slideOffset);
            float moveFactor = (mDrawerList.getWidth() * slideOffset);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
            {
                frame.setTranslationX(moveFactor);
            }
            else
            {
                TranslateAnimation anim = new TranslateAnimation(lastTranslate, moveFactor, 0.0f, 0.0f);
                anim.setDuration(0);
                anim.setFillAfter(true);
                frame.startAnimation(anim);

                lastTranslate = moveFactor;
            }
        }
    };

    drawer.setDrawerListener(toggle);


    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.dashboard, 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

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    dashboard= (LinearLayout) findViewById(R.id.dashboard_layout);

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

  

如果你想移动它,请将工具放在框架内

相关问题