我遵循了android指南并完成了如下布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id= "@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/fragmentContainer"/>
</LinearLayout>
<LinearLayout
android:id="@+id/left_drawer_view"
android:layout_width="250dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/wall"
android:layout_margin="0dp"
android:padding="0dp"
android:scaleType="center"
/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/placeholder"
android:layout_margin="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IGuessI'mUsername?"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="vertical"
android:background="#fff"
android:id="@+id/left_drawer_item"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/side_item_home"
android:text="Home"
android:drawablePadding="5dp"
android:padding="5dp"
android:gravity="center_vertical"
android:drawableLeft="@drawable/ic_menu_home"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activities"
android:drawablePadding="5dp"
android:id="@+id/side_item_activities"
android:padding="5dp"
android:gravity="center_vertical"
android:drawableLeft="@drawable/ic_menu_search_holo_light"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:drawablePadding="5dp"
android:id="@+id/side_item_settings"
android:padding="5dp"
android:gravity="center_vertical"
android:drawableLeft="@drawable/ic_action_settings"
/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
仅供参考,我没有使用Android.R.Id
,而是使用我自己的R.id.fragmentContainer
加载片段。但显然,抽屉布局位于FrameLayout中加载的片段下。如果我单击DrawerLayout上的项目,片段会接收到手势,而不是DrawerLayout,尽管它仍然可以正确显示。
修改
这是请求的代码。
public class ClipMe_main extends ActionBarActivity implements View.OnClickListener {
private HashMap<String, Stack<Fragment>> mStacks;
SlidingTabFragment homeFragment;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private String[] mTitle;
Fragment settingsFragment;
private ArrayList<DrawerItemModel> drawerData;
android.support.v7.app.ActionBar bar;
ActionBarDrawerToggle mDrawerToggle;
SlidingTabLayout mSlidingTabLayout;
public static DisplayMetrics displayMetrics;
FragmentManager fm;
FragmentPagerAdapter adapterViewPager;
int visiblePage;
TextView sideBarHome;
TextView sideBarActivities;
TextView sideBarSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
registerReceiver(receiverDownloadComplete, new IntentFilter(DownloadService.ACTION_DOWNLOAD_COMPLETE));
sideBarHome = (TextView) findViewById(R.id.side_item_home);
sideBarActivities = (TextView) findViewById(R.id.side_item_activities);
sideBarSettings = (TextView) findViewById(R.id.side_item_settings);
sideBarSettings.setOnClickListener(this);
sideBarHome.setOnClickListener(this);
sideBarActivities.setOnClickListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
new Toolbar(ClipMe_main.this),
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(getTitle());
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(getTitle());
}
};
setUpView();
setUpHomeFragment();
setUpSettingFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.swipe_refresh_2));
setSupportActionBar(toolbar);
mDrawerLayout.setDrawerListener(mDrawerToggle);
bar = this.getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
bar.setHomeButtonEnabled(true);
bar.setDisplayShowTitleEnabled(true);
}
private void setUpSettingFragment() {
settingsFragment = new SettingsFragment();
}
void setUpView() {
setContentView(R.layout.layout_main_activity);
}
void setUpHomeFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
homeFragment = new SlidingTabFragment();
transaction.add(R.id.fragmentContainer, homeFragment, "home");
transaction.commit();
}
public void pushFragments(String tag, Fragment fragment, boolean shouldAnimate) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (shouldAnimate)
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
ft.replace(R.id.fragmentContainer, fragment, tag).addToBackStack(null);
ft.commit();
Log.d("FragmentManager", "Push " + tag);
}
public void getFragmentAndUpdate(int page) {
PopularFragment currentFragment = homeFragment.getCurrentFragment();
currentFragment.requestMoreData(page);
Log.d("FragmentPage", String.valueOf(page));
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@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_clipme_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.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public int findVisibleFragment(){
Fragment homeFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("home");
Fragment activitiesFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("activities");
Fragment settingsFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("settings");
Fragment likeFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("like");
Fragment commentFragment = (Fragment) getSupportFragmentManager().findFragmentByTag("comment");
if(homeFragment!= null && homeFragment.isVisible()){
return 1;
} else if(activitiesFragment!= null && activitiesFragment.isVisible()){
return 2;
} else if (settingsFragment!= null && settingsFragment.isVisible()){
return 3;
} else if (likeFragment!= null && likeFragment.isVisible()){
return 4;
} else if (commentFragment!= null && commentFragment.isVisible()){
return 5;
} else
return 0;
}
@Override
public void onClick(View v) {
if (v == sideBarHome){
if (findVisibleFragment() == 1){
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.closeDrawers();
pushFragments("activities", homeFragment, true);
}
}
if (v == sideBarActivities){
if (findVisibleFragment() == 2){
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.closeDrawers();
pushFragments("activities", new ActivitiesFragment(), true);
}
}
if (v == sideBarSettings){
if (findVisibleFragment() == 1){
mDrawerLayout.closeDrawers();
} else {
mDrawerLayout.closeDrawers();
pushFragments("activities", settingsFragment, true);
}
}
}
}
答案 0 :(得分:0)
我发现的问题是在onClick()方法中。尝试更改以下功能
public Fragment findVisibleFragment() {
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);
return fragment;
}
编辑:
将同一个侦听器设置为多个视图。
sideBarHome.setOnClickListener(clickListener);
sideBarSettings.setOnClickListener(clickListener);
将此clicklistener对象引用到视图,您可以使用相同的逻辑来区分视图
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view.getId() == R.id.side_item_home) {
// mDrawerLayout.closeDrawers();
if (!(findVisibleFragment() instanceof SlidingTabFragment)) {
pushFragments("activities", homeFragment, true);
}
}
else if() {
//rest click logic
}
}
};