我正在我的android应用程序中实现类似谷歌播放的持久搜索视图。这是我的代码。
public class NavigationDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private EditText searchView;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawer;
private TextView appName;
private boolean isNavigationDrawerOpened;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CoordinatorLayout coordinatorLayout = (CoordinatorLayout)findViewById(R.id.mainLayout);
coordinatorLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(isNavigationDrawerOpened){
hideSearchAndEnableAppName();
}
return false;
}
});
searchView = (EditText) findViewById(R.id.searchView);
appName = (TextView) findViewById(R.id.appName);
searchView.setVisibility(View.GONE);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),"asdfasdfasdf",Toast.LENGTH_LONG).show();
return false;
}
});
drawer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "asdfasfasdf", Toast.LENGTH_SHORT).show();
}
});
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(NavigationDrawerActivity.this,"yayyy mani got it",Toast.LENGTH_LONG).show();
}
});
appName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isNavigationDrawerOpened) {
searchView.setVisibility(View.VISIBLE);
searchView.requestFocus();
appName.setVisibility(View.GONE);
isNavigationDrawerOpened = true;
animateDrawerIndicator(true);
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void hideSearchAndEnableAppName() {
animateDrawerIndicator(false);
isNavigationDrawerOpened = false;
searchView.setVisibility(View.GONE);
appName.setVisibility(View.VISIBLE);
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else if(isNavigationDrawerOpened){
hideSearchAndEnableAppName();
}else {
super.onBackPressed();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
drawer.openDrawer(GravityCompat.START);
Toast.makeText(getApplicationContext(),"asdfasdf",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void animateDrawerIndicator(boolean shouldAnimate) {
ValueAnimator anim;
if(shouldAnimate) {
anim = ValueAnimator.ofFloat(0, 1);
} else {
anim = ValueAnimator.ofFloat(1, 0);
}
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
// You should get the drawer layout and
// toggler from your fragment to make the animation
toggle
.onDrawerSlide(drawer,
slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
anim.start();
}
}
这里一切都很好,但当用户点击工具栏上的edittext(根据我的代码)时,我将汉堡图标改为后退按钮。现在,如果用户点击后退按钮,它应该动画返回汉堡,编辑文本应该隐藏(根据我的要求)
但是,要做到这一点,我需要处理汉堡点击。我试过在切换、动作切换等方面使用ClickListners,但没有成功,请帮忙。
当工具栏和抽屉都存在时
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu); //set your own
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
}else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
});
这是我在将汉堡图标与其他图标更改时在应用程序中使用的当设置图标时,点击图标不起作用,因为我们需要使用:
mDrawerToggle.setDrawerIndicatorEnabled(false);
但仅当工具栏(无导航抽屉)时:
toolbar = (Toolbar) findViewById(R.id.toolbar);
toggle = new ActionBarDrawerToggle(
this, null, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu); //set your own
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
}else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
});
然后将汉堡图标更改为其他图标:
toolbar.setNavigationIcon(R.drawable.ic_dehaze_black_24dp);
ActionBarDrawerToggle
的onDrawerSlide()
方法来动画切换是很麻烦的。一个更好的选择是将DrawerArrowDrawable
设置为切换的向上指示器,并自行设置动画,根据需要启用和禁用切换的抽屉指示器。
首先,我们在Activity
中声明一个DrawerArrowDrawable
字段。
private DrawerArrowDrawable searchToggle;
然后,我们在onCreate()
方法中对其进行初始化,将其设置为Up指示符,并定义导航OnClickListener
。
searchToggle = new DrawerArrowDrawable(this);
toggle.setHomeAsUpIndicator(searchToggle);
toggle.setToolbarNavigationClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideSearchAndEnableAppName();
}
}
);
在animateDrawerIndicator()
方法中,如果我们显示搜索EditText
,我们首先禁用抽屉指示器。这将导致切换恢复到"向上"指示器,然后我们将根据需要设置该指示器的动画,并在隐藏动画完成后切换回抽屉指示器。
public void animateDrawerIndicator(final boolean shouldAnimate) {
ValueAnimator anim;
if (shouldAnimate) {
anim = ValueAnimator.ofFloat(0, 1);
// Show the Up indicator instead
// of the drawer indicator
toggle.setDrawerIndicatorEnabled(false);
}
else {
anim = ValueAnimator.ofFloat(1, 0);
}
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
// Animate our Drawable instead of the drawer's
searchToggle.setProgress(slideOffset);
// If we're hiding and have finished,
// re-enable the drawer indicator
if(!shouldAnimate && slideOffset == 0) {
toggle.setDrawerIndicatorEnabled(true);
}
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
anim.start();
}