简体   繁体   English

CollapsingToolbarLayout和工具栏操作按钮

[英]CollapsingToolbarLayout and toolbar action buttons

I would appreciate if someone could give me some pointers to solve my latest issue. 如果有人可以给我一些解决我最新问题的建议,我将不胜感激。

I have an activity with a CollapsingToolbarLayout. 我有一个CollapsingToolbarLayout活动。 In an un-collapsed state Im unable to get the buttons to work. 在未折叠状态下,我无法使按钮正常工作。 I do not know how to fix this issue. 我不知道如何解决此问题。 I have searched on stackoverflow before posting this, but I did not find any helpful tips. 发布此内容之前,我已经搜索了stackoverflow,但是没有找到任何有用的提示。

Im posting here hoping for an answer 我在这里发布以寻求答案

Thanks 谢谢

This is my code! 这是我的代码!

<?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 图片AAAActivity.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

}

在此处输入图片说明

UPDATE! 更新! - 2018-01-13 -2018-01-13

I figured out what is cousing the the issue but I still dont know fix this The problem is that if I remove this code then the tabs won't show up, so it feels as if this ***** with me 我想出了解决此问题的原因,但我仍然不知道解决此问题。问题是,如果我删除此代码,则选项卡将不会显示,因此感觉好像是我身上的*****

The issue 问题

//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

try this. 尝试这个。

in your activity 在你的活动中

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

in your xml 在你的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

} }

There are two issues with your code, both related to the buttons you're trying to get to work. 您的代码有两个问题,都与您要使用的按钮有关。

First, with the home button (or the up button you need to activate it with actionBar before handling events with it. Add the following: 首先,使用主页按钮(或向上按钮),需要先使用actionBar激活它,然后再使用它来处理事件。添加以下内容:

getSupportActionBar().setHomeButtonEnabled(true);

inside your setupToolbar method. setupToolbar方法中。 You can now access the button using the code you've written under the case android.R.id.home: . 现在,您可以使用在case android.R.id.home:编写的代码访问该按钮。

The second button has the same issue. 第二个按钮有相同的问题。 You haven't added any logic to handle the button click. 您尚未添加任何逻辑来处理按钮单击。 Let's say your button on the right has id="@+id/more" . 假设您右边的按钮具有id="@+id/more" Now to define an action for it, you need to put a case with its id inside switch like, 现在为它定义一个动作,您需要将一个ID为ID的箱子放在switch之内,

case R.id.more: 
   //The action needed for the button
   break;
 @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);
    }

Back Button on click listener. 单击侦听器上的“后退”按钮。

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

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