我的活动有一个支持操作栏,下面是一个滑动标签布局,下面是相应标签的图像列表视图。我只想隐藏支撑操作栏而不是滑动标签条,我无法隐藏它。
这是我的活动:
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
public class NewGalleriActivity extends AppCompatActivity implements ViewTreeObserver.OnScrollChangedListener {
Toolbar toolbar;
ViewPager viewPager;
NewGalleriPagerAdapter viewPagerAdapter;
SlidingTabLayout slidingTabLayout;
CharSequence Titles[] = {"Wildlife", "Architecture", "Black & White", "Close Up", "Night"};
int numOfTabs = 5;
private float mActionBarHeight;
private ActionBar mActionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_galleri);
final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
new int[]{android.R.attr.actionBarSize});
mActionBarHeight = styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
//Initialise views
viewPager = (ViewPager) findViewById(R.id.pager);
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
int defaultValue = 0;
int page = getIntent().getIntExtra("ARG_PAGE", defaultValue);
viewPagerAdapter = new NewGalleriPagerAdapter(getSupportFragmentManager(), Titles, numOfTabs);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(page);
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return Color.parseColor("#FAC80A");
}
});
slidingTabLayout.setViewPager(viewPager);
if (android.os.Build.VERSION.SDK_INT >= 21)
{
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.parseColor("#263238"));
}
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.ddd);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NewGalleriActivity.this, ImageGridActivity.class);
startActivity(intent);
}
});
findViewById(R.id.parent).getViewTreeObserver().addOnScrollChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_home, menu);
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;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onScrollChanged() {
float y = findViewById(R.id.parent).getScrollY();
if(y >= mActionBarHeight && mActionBar.isShowing()) {
mActionBar.hide();
} else if(y==0 && !mActionBar.isShowing()) {
mActionBar.show();
}
}
}
以下是我的活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/black"
android:orientation="vertical">
<include layout="@layout/toolbar2" />
<com.pipipzz.simpleapp.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#231F20" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/drop_shadows" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="24dp"
android:layout_marginBottom="10dp"
android:src="@drawable/fab_icons"
app:borderWidth="@null"
app:elevation="4dp"
app:fabSize="normal" />
</FrameLayout>
</ScrollView>
</LinearLayout>
这是我的工具栏布局:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFC80A"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/tool_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/tool_logo"
android:gravity="center_horizontal"
android:src="@drawable/logo_tool" />
</RelativeLayout>
<ImageView
android:id="@+id/tool_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="8dp"
android:contentDescription="@string/tool_search"
android:src="@drawable/search" />
</android.support.v7.widget.Toolbar>
编辑:这是我在标签中显示的片段布局。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#231F20"
tools:context="com.pipipzz.simpleapp.ArchitectureFragment">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp"
android:scrollingCache="true"
android:smoothScrollbar="true" />
</FrameLayout>
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
请查看此库Android-ObservableScrollView
它有很多例子,在你的情况下使用它会容易得多。
有几个与工具栏一起使用的示例,您当前正在使用代码。
如果您生成所有源代码来运行它会更好,也许您在其他地方有问题。
findViewById
,这是非常重的方法。因此,当您每次onScrollViewChanged
执行此操作时,它会使您的应用程序滞后。 在onCreate
方法
mMainScrollView = findViewById(R.id.parent);
mMainScrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {@Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY(); //for verticalScrollView
// Hide or show toolbar here
}
});
为什么不直接在工具栏上调用hide而不获得支持 动作栏?尝试直接在工具栏上执行此操作。
如果你打电话给hide 直接在ActionBar / Toolbar上的方法是否有效?
尝试做类似的事情
隐藏工具栏
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
再次展示
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();