gpt4 book ai didi

android - 菜单图标上的抽屉导航未打开单击工具栏

转载 作者:行者123 更新时间:2023-11-29 15:45:52 25 4
gpt4 key购买 nike

我想在菜单图标上单击打开抽屉,但它不起作用。看这张图片我想说什么....

enter image description here

以下代码适用于滑动但不适用于菜单图标点击。

请检查我的问题并帮助我...

感谢您的宝贵时间...

MainActivity.java

public class MainActivity extends AppCompatActivity implements
FragmentDrawerListener {

private Toolbar toolbar;
private FragmentDrawer drawerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_lay);

toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_launcher));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerFragment = (FragmentDrawer) getSupportFragmentManager()
.findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
}

@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 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}

fragment 抽屉.java

public class FragmentDrawer extends Fragment {

private static String TAG = FragmentDrawer.class.getSimpleName();

private static String[] titles = null;

private RecyclerView recyclerView;
private NavigationDrawerAdapter adapter;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private FragmentDrawerListener drawerListener;
private View containerView;

public FragmentDrawer() {

}

public void setDrawerListener(FragmentDrawerListener listener) {
this.drawerListener = listener;
}

public static List<NavDrawerItem> getData() {
List<NavDrawerItem> data = new ArrayList<>();

// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
NavDrawerItem navItem = new NavDrawerItem();
navItem.setTitle(titles[i]);
data.add(navItem);
}
return data;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating view layout
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new NavigationDrawerAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener(){

@Override
public void onClick(View view, int position) {
drawerListener.onDrawerItemSelected(view, position);
mDrawerLayout.closeDrawer(containerView);
}

@Override
public void onLongClick(View view, int position) {

}

}));

return layout;
}

/**
*
* @param fragmentId
* @param drawerLayout
* @param toolbar
*/
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {

containerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void setToolbarNavigationClickListener(
OnClickListener onToolbarNavigationClickListener) {
// TODO Auto-generated method stub
super.setToolbarNavigationClickListener(onToolbarNavigationClickListener);
Log.i("RAJAT", "I am here");
}

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
toolbar.setAlpha(1 - slideOffset / 2);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("RAJAT", "click ON OPTION ITEM");
if(mDrawerToggle.onOptionsItemSelected(item)) {
Log.d("RAJAT", "click here");
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

};

mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
});
}

static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

private ClickListener clickListener;
private GestureDetector gestureDetector;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
// TODO Auto-generated constructor stub
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if( child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean arg0) {

}

@Override
public void onTouchEvent(RecyclerView arg0, MotionEvent arg1) {

}

}

public static interface ClickListener {
public void onClick(View view, int position);

public void onLongClick(View view, int position);
}

public interface FragmentDrawerListener {
public void onDrawerItemSelected(View view, int position);
}

最佳答案

试试这个代码块

@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.
if (item.getItemId() == android.R.id.home) {
if(mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
}else{
mDrawerLayout.openDrawer(Gravity.LEFT);
}

}else if (item.getItemId()== R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

关于android - 菜单图标上的抽屉导航未打开单击工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33432924/

25 4 0