Material ViewPager点击返回退出活动

时间:2016-02-10 05:41:39

标签: android android-viewpager toolbar material

使用Florent的ViewPager Library点击exit时无法back寻呼机活动

我这样叫PagerActivity

    public class MainActivity extends AppCompatActivity {

    @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) {
                Intent intent = new Intent(MainActivity.this, PagerActivity.class);
                startActivity(intent);
            }
        });
    }
}

此处(在 PagerActivity .java中),默认情况下,我在左上角一角获得back icon,但当我执行tap时它没有happens

像这样:

enter image description here

PagerActivity.java

public class PagerActivity extends AppCompatActivity {

    MaterialViewPager materialViewPager;
    View headerLogo;
    ImageView headerLogoContent;

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

        //4 onglets
        final int tabCount = 4;

        //les vues définies dans @layout/header_logo
        headerLogo = findViewById(R.id.headerLogo);
        headerLogoContent = (ImageView) findViewById(R.id.headerLogoContent);

        //le MaterialViewPager
        this.materialViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager);
        this.materialViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {                   

            .........
        this.materialViewPager.getViewPager().setOffscreenPageLimit(tabCount);
        //relie les tabs au viewpager
        this.materialViewPager.getPagerTitleStrip().setViewPager(this.materialViewPager.getViewPager());
    }

    ......

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                // onBackPressed();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

activity_pager.xml:

<RelativeLayout 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">

    <com.github.florent37.materialviewpager.MaterialViewPager
        android:id="@+id/materialViewPager"
        app:viewpager_color="@color/colorPrimary"
        app:viewpager_enableToolbarElevation="true"
        app:viewpager_headerAlpha="0.6"
        app:viewpager_headerHeight="200dp"
        app:viewpager_hideLogoWithFade="true"
        app:viewpager_hideToolbarAndTitle="true"
        app:viewpager_logo="@layout/header_logo"
        app:viewpager_pagerTitleStrip="@layout/material_view_pager_pagertitlestrip_newstand"
        app:viewpager_logoMarginTop="80dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

您必须在PagerActivity中添加以下代码。因为MaterialViewPager库包含ToolBar / Actionbar所以你必须先设置它。

this.materialViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager);
toolbar = materialViewPager.getToolbar();
if (toolbar != null) {
    setSupportActionBar(toolbar);

    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

之后使用以下方法

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

只需将此方法添加到PagerActivity

即可
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}