簡體   English   中英

CollapsingToolbarLayout和工具欄操作按鈕

[英]CollapsingToolbarLayout and toolbar action buttons

如果有人可以給我一些解決我最新問題的建議,我將不勝感激。

我有一個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>

圖片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

}

在此處輸入圖片說明

更新! -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

嘗試這個。

在你的活動中

 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

}

您的代碼有兩個問題,都與您要使用的按鈕有關。

首先,使用主頁按鈕(或向上按鈕),需要先使用actionBar激活它,然后再使用它來處理事件。添加以下內容:

getSupportActionBar().setHomeButtonEnabled(true);

setupToolbar方法中。 現在,您可以使用在case android.R.id.home:編寫的代碼訪問該按鈕。

第二個按鈕有相同的問題。 您尚未添加任何邏輯來處理按鈕單擊。 假設您右邊的按鈕具有id="@+id/more" 現在為它定義一個動作,您需要將一個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);
    }

單擊偵聽器上的“后退”按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM