繁体   English   中英

每个活动中带有NavigationDrawer的ViewPager + TabLayout - 问题

[英]ViewPager + TabLayout with NavigationDrawer in every Activity - Issue

我遇到了一个我无法解决的问题:我有一个MainActivity ,其中有一个NavigationDrawer ,它允许我去三个不同的活动。 那些扩展了MainActivity,所以我在每个活动中都得到了抽屉。 在相同的MainActivity中,我将TabLayout与三个选项卡Fragments放在一起。

我面临的问题是,每当我从抽屉布局中进入三个活动中的一个时,我都没有将布局xml附加到Activity1,而是我再次获得带有碎片的TabLayout。 我怎么解决这个问题?

结果应该看起来像Google Play应用。

这是我的MainActivity

public class MainActivity extends AppCompatActivity {

DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_layout);

    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    mTitle.setText(R.string.app_name);
    setSupportActionBar(toolbar);

    assert getSupportActionBar() != null;

    this.getSupportActionBar().setDisplayShowTitleEnabled(false);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

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

    Button button1 = (Button)findViewById(R.id.button1); //this is inside the drawer layout
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Activity1.class);
            startActivity(intent);
        }
    });

    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            null,
            R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View v) {
            super.onDrawerClosed(v);
            invalidateOptionsMenu();
            syncState();
        }

        public void onDrawerOpened(View v) {
            super.onDrawerOpened(v);
            invalidateOptionsMenu();
            syncState();
        }
    };

    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.setDrawerIndicatorEnabled(false); 
    mDrawerToggle.syncState();

    mCollectionPagerAdapter = new CollectionPagerAdapter(
            getSupportFragmentManager());


    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);

    TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
    tabs.setupWithViewPager(mViewPager);

}

public class CollectionPagerAdapter extends FragmentPagerAdapter {

    //final int NUM_ITEMS = 3; // number of tabs

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int position)
    {

        switch (position) {
            case 0:
                return new Tab1();
            case 1:
                return new Tab2();
            case 2:
                return new Tab3();
        }

        return null;
    }

    @Override
    public int getCount()
    {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position)
        {
            case 0:
                return getString(R.string.tab1);
            case 1:
                return getString(R.string.tab2);
            case 2:
                return getString(R.string.tab3);
        }
        return null;
    }
}




@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home: {
            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
            return true;
        }

        default:
            return super.onOptionsItemSelected(item);
    }
}

Activity1

public class Activity1 extends MainActivity { //extends MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.classe1);  //setContentView before super.onCreate(savedInstanceState) allows me to get drawer in each activity
    super.onCreate(savedInstanceState);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_layout);

    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    mTitle.setText("Activity 1");
    setSupportActionBar(toolbar);

    assert getSupportActionBar() != null;

    this.getSupportActionBar().setDisplayShowTitleEnabled(false);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

}

Tab1 (片段):

public class Tab1 extends Fragment {
View view;

public Tab1() {
}

@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.tab1, null);

    return view;
}

和我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.v4.view.ViewPager
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_marginBottom="60dp"
        android:id="@+id/pager">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:overScrollMode="never">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:orientation="vertical">

            </LinearLayout>

        </ScrollView>

    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="bottom">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/tabs"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@color/colorPrimaryDark"
            app:tabTextColor="@color/tab_text"
            app:tabIndicatorColor="@android:color/transparent"
            app:tabBackground="@drawable/selected_tab_color"
            style="@style/MyCustomTabLayout"/>

        <include layout="@layout/toolbar" android:id="@+id/toolbar_layout"/>

    </LinearLayout>

</RelativeLayout>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="304dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:clickable="true"
    android:background="#ffffff">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/uno"
                android:text="Button to Activity1"/>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

classe1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <FrameLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_marginBottom="30dp"
        android:id="@+id/content_frame">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:overScrollMode="never">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="CLASSE 1"
                    android:textSize="35sp"
                    android:gravity="center"/>

            </LinearLayout>

        </ScrollView>

    </FrameLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom">

        <include layout="@layout/toolbar" android:id="@+id/toolbar_layout"/>

    </RelativeLayout>

</RelativeLayout>

你的问题是, super.onCreate()的调用Activity1被调用setContentView()再次MainActivity ,这是完全替代Activity1的布局,它的呼叫设置setContentView()

由于您需要MainActivity选项卡而不是其他活动,因此您的其他活动不应扩展MainActivity 相反,您应该使用DrawerLayout创建一个基本Activity ,您的所有活动都会扩展,包括MainActivity ,然后在各个子类中添加您需要的View

在基本Activity ,我们将覆盖setContentView()方法,首先设置基本布局,设置抽屉和切换,然后将子类的布局膨胀到DrawerLayout的内容View 请注意,我们不会在基本ActivityonCreate()方法中调用setContentView()

public abstract class BaseActivity extends AppCompatActivity {

    protected Toolbar toolbar;
    protected DrawerLayout mDrawerLayout;
    protected ActionBarDrawerToggle mDrawerToggle;
    protected TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base);

        toolbar = (Toolbar) findViewById(R.id.toolbar_layout);

        mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        mTitle.setText(R.string.app_name);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(false);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //this is inside the drawer layout
        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(BaseActivity.this, Activity1.class);
                    startActivity(intent);
                }
            });

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this,
                                                  mDrawerLayout,
                                                  null,
                                                  R.string.drawer_open,
                                                  R.string.drawer_close) {
            public void onDrawerClosed(View v) {
                super.onDrawerClosed(v);
                invalidateOptionsMenu();
                syncState();
            }

            public void onDrawerOpened(View v) {
                super.onDrawerOpened(v);
                invalidateOptionsMenu();
                syncState();
            }
        };

        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        mDrawerToggle.syncState();

        getLayoutInflater().inflate(layoutResID,
                                    (ViewGroup) findViewById(R.id.content));
    }
}

基本布局几乎相同,只是删除了MainActivity特有的所有内容。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <include layout="@layout/toolbar" android:id="@+id/toolbar_layout"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clickable="true"
        android:background="#ffffff">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/button1"
                    android:text="Button to Activity1"/>

            </LinearLayout>

        </ScrollView>

    </LinearLayout>

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

MainActivity ,我们不再需要设置抽屉和切换。

public class MainActivity extends BaseActivity {

    private CollectionPagerAdapter mCollectionPagerAdapter;
    private ViewPager mViewPager;

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

        mCollectionPagerAdapter = new CollectionPagerAdapter(
            getSupportFragmentManager());


        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mCollectionPagerAdapter);

        TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
        tabs.setupWithViewPager(mViewPager);
    }
    ...
}

MainActivity的布局现在基本上只是ViewPagerTabLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/pager">

        ...

    </android.support.v4.view.ViewPager>

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:id="@+id/tabs"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/tab_text"
        app:tabIndicatorColor="@android:color/transparent"
        app:tabBackground="@drawable/selected_tab_color"
        style="@style/MyCustomTabLayout" />

</LinearLayout>

然后,要完成您发布的代码正在执行的Activity1中的所有内容,我们所需要的就是这一点,因为Toolbar和标题TextView现在位于BaseActivity

public class Activity1 extends BaseActivity {

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

        mTitle.setText("Activity 1");
    }
}

并且Activity1的布局可以显着减少:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:overScrollMode="never">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="CLASSE 1"
            android:textSize="35sp"
            android:gravity="center"/>

    </LinearLayout>

</ScrollView>

您可以在Activity1 onCreate方法中调用setContentView两次,第一次使用R.layout.classe1,第二次使用R.layout.activity_main(当您调用super.onCreate时)。 最后一个setContentView获胜,你的问题就在这里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM