我使用DrawerLayout
作为RecyclerView
菜单,因为我需要以编程方式控制和更新菜单。
我希望只通过滑动和按钮点击打开/关闭菜单但问题是,当我按住主要布局中的任何内容时,抽屉显示一点点,我点击按钮将失败。但是,当您快速按下时,点击工作正常。
顺便说一下,我不是Android用户。这是系统或Android默认行为还是其他什么行为?
如果我不清楚,这是演示
这是我布局的伪代码
<android.support.v4.widget.DrawerLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
...
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
... />
<android.support.v7.widget.RecyclerView
...
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
.../>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:1)
在布局标签的布局xml中添加可点击且可聚焦的属性
<android.support.constraint.ConstraintLayout 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"
tools:context="com.project.wordsdict.main.views.AboutActivity"
android:focusable="true"
android:clickable="true">
然后在onCreate方法的活动中,通过id
找到你的布局 ConstraintLayout mLayout = findViewById(R.id.yourLayoutId);
mLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
return true;
}
});
mLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
return false;
case MotionEvent.ACTION_UP:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
return true;
}
return false;
}
});
P.S使用您正在使用的任何布局。希望它的确有效,因为它确实是DrawerLayout的错误
答案 1 :(得分:0)
您可以拥有一个包含该菜单的活动,然后进行需要使用该菜单的其他活动来实现该活动。
例如。
BaseActivity - 包含菜单:
import android.app.ActivityOptions;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class BaseActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public static DrawerLayout drawer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks
int id = item.getItemId();
switch (id) {
.......
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks
Intent intent;
int id = item.getItemId();
ActivityOptions options =
ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.enter, R.anim.exit);
switch (id) {
..............
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
使用Recycler视图的活动:
public class YourActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.your_layout, null, false);
BaseActivity.drawer.addView(contentView, 0);
getSupportActionBar().setDisplayShowTitleEnabled(true);
// Lookup the recycler view in activity layout
RecyclerView recyclerView = findViewById(R.id.recyclerView);
// Initialize arrayList
ArrayList<....> arrayList= new ArrayList<>();
// Create adapter
YourAdapter adapter = new CategoryAdapter(...);
// Attach the adapter to the recycler view
recyclerView.setAdapter(adapter);
//Populate the recycler view
adapter.addData();
// Set layout manager to position the items
recyclerView.setLayoutManager(new GridLayoutManager(this, 2,
GridLayoutManager.VERTICAL, false));
}
your_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_column="2">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</GridLayout>
答案 2 :(得分:0)
好的,我最后通过为DrawerLayout
创建子类并覆盖onInterceptTouchEvent方法来完成它。这个想法是当按下时,强制关闭菜单。否则解锁它。
这是现在替换我的xml文件中的原始drawerlayout的子类。
public class CustomDrawer extends DrawerLayout {
public CustomDrawer(Context context) {
super(context);
}
public CustomDrawer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
} else {
setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
return super.onInterceptTouchEvent(event);
}
}
布局伪代码
<...CustomDrawer
android:layout_width="match_parent"
android:layout_height="match_parent">
...
...
</...CustomDrawer>