java - CollapsingToolbarLayout 和工具栏操作按钮

标签 java android xml

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

我有一个带有 CollapsingToolbarLayout 的 Activity 。在未折叠状态下,我无法让按钮工作。我不知道如何解决这个问题。发帖前我在 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

更新! - 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 激活它,然后再使用它处理事件。添加以下内容:

getSupportActionBar().setHomeButtonEnabled(true);

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

第二个按钮有同样的问题。您还没有添加任何逻辑来处理按钮点击。假设您右边的按钮有 id="@+id/more"。现在为它定义一个 Action ,你需要把一个带有它的 id 的 case 放在 switch 里面,比如,

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

关于java - CollapsingToolbarLayout 和工具栏操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48220002/

相关文章:

java - 使用集合对象构建结果集

python - 删除 lxml 中的所有命名空间?

python - 用python解析xml文件

python - 在python中的elementTree中插入xml条目

java - 如何知道程序监听哪个端口

java - 在 Activemq 队列中搜索特定消息

java - 未在泛型类中实现泛型方法

android - 在 Activity 中使用 GlSurfaceview

android - 将 Firebase Firestore 和 Firebase Storage 调用合并到一个批处理中?

java - 我怎样才能在java中混合2个列表