CollapsingToolbarLayout和工具栏操作按钮

时间:2018-01-12 05:18:33

标签: java android xml

如果有人能给我一些解决我最新问题的建议,我将不胜感激。

我有一个使用CollapsingToolbarLayout的活动。在一个未崩溃的状态,我无法让按钮工作。我不知道如何解决这个问题。我在发布之前已经搜索了stackoverflow,但我没有找到任何有用的提示。

我在这里发帖希望得到答案

由于

这是我的代码!

<?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:background="?attr/app_background"
    android:fitsSystemWindows="true"
    tools:context="com.company.walt.activities.photos.PhotosAAAActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

                <ImageView
                    android:id="@+id/backdrop"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/ip_photo_header"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/love_music"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="AAAAAA"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/ssi_txt_40sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="BBBBBBBB"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/ssi_txt_20sp" />

                </LinearLayout>

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginTop="@dimen/ssi_24dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabIndicatorColor="@color/white"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/white" />

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

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

PhotosAAAActivity.java

public class PhotosAAAActivity extends AppCompatActivity {

    //region WIDGETS
    private AppBarLayout bAppBarLayout;
    private CollapsingToolbarLayout bCollapsingToolbar;
    private Toolbar bToolbar;
    private TabLayout mTabLayout;
    //endregion

    //region VARS
    private ViewPager mViewPager;
    private PhotosPagerAdapter mPhotosPagerAdapter;
    SharedPreferencesManager mSharedPreferences;
    //endregion

    /* ******************************************************************************************* */
    //region THE ONCREATE
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        //region Load Preferences
        mSharedPreferences = new SharedPreferencesManager(this);
        //endregion

        //region Switching theme style
        if (mSharedPreferences.getNightModeState() == true) {
            setTheme(R.style.NightTheme);
        } else {
            setTheme(R.style.LightTheme);
        }
        //endregion

        //region Super onCreate
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photos);
        //endregion

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

        //region Calling Methods
        setUpCollapsingToolbar();
        setUpToolbar();
        setViewPager();
        //endregion
    }
    //endregion

    private void setUpCollapsingToolbar() {

        bCollapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        bCollapsingToolbar.setCollapsedTitleTextColor(getResources().getColor(R.color.white));

        bAppBarLayout = (AppBarLayout) findViewById(R.id.appbar);
        bAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

            boolean isShow = false;
            int scrollRange = -1;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (scrollRange == -1) {
                    scrollRange = appBarLayout.getTotalScrollRange();
                }
                if (scrollRange + verticalOffset == 0) {
                    bCollapsingToolbar.setTitle("Post photos");
                    isShow = true;
                } else if (isShow) {
                    bCollapsingToolbar.setTitle("");
                    isShow = false;
                }
            }
        });

    }

    /* ******************************************************************************************* */
    //region Used to create the toolbar on top
    private void setUpToolbar() {
        bToolbar = (Toolbar) findViewById(R.id.main_toolbar);
        setSupportActionBar(bToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
    }
    //endregion

    /* ******************************************************************************************* */
    //region Used to create the tab layout
    private void setViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mPhotosPagerAdapter = new PhotosPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mPhotosPagerAdapter);

        mTabLayout = (TabLayout) findViewById(R.id.tab);
        mTabLayout.setupWithViewPager(mViewPager);
    }
    //endregion

    /* ******************************************************************************************* */
    //region MATERIAL DRAWER
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.photo_category, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case android.R.id.home:

                finish();
                //Toast.makeText(PhotosAAAActivity.this, "GO BACK", Toast.LENGTH_SHORT).show();

                return true;

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

}

enter image description here

UPDATE! - 2018-01-13

我想出了什么是问题,但我仍然不知道解决这个问题 问题是,如果我删除此代码,那么选项卡将不会显示,所以感觉好像这个*****和我一起

问题

//region Used to create the tab layout
    private void setViewPager() {


        ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
        mPhotosPagerAdapter = new PhotosPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mPhotosPagerAdapter);

        mTabLayout = (TabLayout) findViewById(R.id.tab);
        mTabLayout.setupWithViewPager(mViewPager);

    }
    //endregion

3 个答案:

答案 0 :(得分:0)

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

        if (id == android.R.id.home) {
            finish();
        }

        //noinspection SimplifiableIfStatement

        return super.onOptionsItemSelected(item);
    }
单击侦听器上的

后退按钮。

答案 1 :(得分:0)

试试这个。

在您的活动中

 ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
你的xml中的

@Override
public boolean onOptionsItemSelected(MenuItem item) {

 switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
 //for menu you should give ids to that menu items in your menu file 
//R.menu.photo_category and after that if you have 2 menu items
// you have to write listener for every single items for onClick like.
        case R.id.item1:
            finish();
            break;
        case R.id.item2:
            finish();
            break;
}
 return true

}

答案 2 :(得分:0)

您的代码存在两个问题,两个问题都与您尝试开始工作的按钮有关。

首先,使用主页按钮(或向上按钮,您需要在使用actionBar处理事件之前使用它来激活它。添加以下内容:

getSupportActionBar().setHomeButtonEnabled(true);

setupToolbar方法中。您现在可以使用您在case android.R.id.home:下编写的代码访问该按钮。

第二个按钮有同样的问题。您还没有添加任何逻辑来处理按钮点击。我们说右边的按钮有id="@+id/more"。现在要为它定义一个动作,你需要将一个带有id的case放在switch中,如

case R.id.more: 
   //The action needed for the button
   break;