滚动时如何隐藏我的工具栏?

时间:2019-09-05 23:08:45

标签: android android-fragments kotlin android-toolbar

我正在努力做到这一点,以便在滚动包含回收站视图的片段(在框架布局中放大)时,我的工具栏将隐藏起来。

我已经尝试过更改最外面的relativelayout和coordinatorlayout并移动一些内容,以及添加滚动标签,但这些标签似乎都不起作用(我可能做错了它们)。

这是有关活动的布局:

class Vehicle:
  def __init__(self):
    self._color = 'blue'

  @property
  def color(self):
    return self._color

class BMW(Vehicle):
  def __init__(self):
    super().__init__()

  @property
  def color(self):
    return self._color + 'extra string from BMW'
car = Vehicle()
print(car.color) #blue

bmw = BMW()
print(bmw.color) #blue extra string from BMW'

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以使用折叠工具栏,您看到过吗? https://www.journaldev.com/13927/android-collapsingtoolbarlayout-example

在整合了collapsingtoolbarlayout之后,当您像这样滚动时,可以使用appbarlayout编程地运行时来隐藏工具栏

AppBarLayout appBarLayout;
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                //at toolbar position

            } else if (verticalOffset == 0) {
                //totally expanded

            } else {
                //somewhere in between
            }
        }
    });

科特林

appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
        if (Math.abs(verticalOffset) == appBarLayout.totalScrollRange) {
            //at toolbar position

        } else if (verticalOffset == 0) {
            //totally expanded

        } else {
            //somewhere in between
        }
    })

答案 1 :(得分:0)

您请求的功能称为Collapsingtoolbarlayout,您可以参考此

            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/photo"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<include layout="@layout/content_scrolling" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@android:drawable/ic_dialog_info" />

在此之后,您必须设置菜单项

<menu 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"
tools:context="com.journaldev.collapsingtoolbarlayout.ScrollingActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />

<item
    android:id="@+id/action_info"
    android:orderInCategory="200"
    android:title="info"
    app:showAsAction="ifRoom"
    android:icon="@android:drawable/ic_dialog_info"/>

在Java代码中执行此操作

公共类ScrollingActivity扩展了AppCompatActivity {

private Menu menu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scrolling);
    final Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    AppBarLayout mAppBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
    mAppBarLayout.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) {
                isShow = true;
                showOption(R.id.action_info);
            } else if (isShow) {
                isShow = false;
                hideOption(R.id.action_info);
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu_scrolling, menu);
    hideOption(R.id.action_info);
    return true;
}

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

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    } else if (id == R.id.action_info) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void hideOption(int id) {
    MenuItem item = menu.findItem(id);
    item.setVisible(false);
}

private void showOption(int id) {
    MenuItem item = menu.findItem(id);
    item.setVisible(true);
}

}

相关问题