抽屉布局重叠 |在 Fragment 中覆盖完整空间
Posted
技术标签:
【中文标题】抽屉布局重叠 |在 Fragment 中覆盖完整空间【英文标题】:Drawer Layout Overlapping | Covering complete space in Fragment 【发布时间】:2018-04-03 03:27:21 【问题描述】:您好,我正在尝试在 Fragment 中实现 Feed 应用并实现 Navigation Drawer、Tab Layout 和 Recyclerview。
在此处查看屏幕截图:
Screenshot With out Drawer layout
Screenshot of app with Drawer layout
这是我的代码。
activity_main.xml
<LinearLayout 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:id="@+id/container"
android:layout_
android:layout_
android:orientation="vertical"
tools:context="com.example.sirajm.bottomnav.MainActivity">
<FrameLayout
android:id="@+id/content"
android:layout_
android:layout_
android:layout_weight="1">
<include
layout="@layout/content_main"
/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_
android:layout_
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:id="@+id/content_panel"
android:layout_>
</LinearLayout>
fragment_feed.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_
android:layout_>
<!-- TODO: Update blank fragment layout -->
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_
android:layout_
app:layout_scrollFlags="scroll|enterAlways"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
frame_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#FFFFFF">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_
android:layout_
android:layout_above="@+id/navigation" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements FeedFragment.OnFragmentInteractionListener
// private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
switch (item.getItemId())
case R.id.navigation_home:
// mTextMessage.setText(R.string.title_home);
Toast.makeText(MainActivity.this, "Home", Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_dashboard:
// mTextMessage.setText(R.string.title_dashboard);
Toast.makeText(MainActivity.this, "Dashboard", Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_notifications:
Toast.makeText(MainActivity.this, "Notification", Toast.LENGTH_SHORT).show();
// mTextMessage.setText(R.string.title_notifications);
return true;
return false;
;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content_panel, new FeedFragment())
.commit();
// mTextMessage = (TextView) findViewById(R.id.mtext);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
@Override
public void onFragmentInteraction(Uri uri)
FeedFragment.java
public class FeedFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titleList = new ArrayList<>();
private View rootView;
private ViewPager viewPager;
private TextTabsAdapter adapter;
private TabLayout tabLayout;
private OnFragmentInteractionListener mListener;
public FeedFragment()
// Required empty public constructor
private void initialise()
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
toolbar.setTitle("Simple Tabs Example");
DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
drawer.setBackgroundColor(getResources().getColor(R.color.whiteColor));
toggle.syncState();
NavigationView navigationView = (NavigationView) rootView.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
private void prepareDataResource()
addData(new FragmentOne(), "ONE");
addData(new FragmentTwo(), "TWO");
// addData(new FragmentThree(), "THREE");
private void addData(Fragment fragment, String title)
fragmentList.add(fragment);
titleList.add(title);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_feed, container, false);
initialise();
prepareDataResource();
adapter = new TextTabsAdapter(getActivity().getSupportFragmentManager(), fragmentList, titleList);
// Bind Adapter to ViewPager.
viewPager.setAdapter(adapter);
// Link ViewPager and TabLayout
tabLayout.setupWithViewPager(viewPager);
return rootView;
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
return false;
public interface OnFragmentInteractionListener
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
FragmentOne.java
class FragmentOne extends Fragment
private RecyclerView mrecyclerview;
private FeedsAdapter feedadapter;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
rootView = inflater.inflate(R.layout.fragment_one, container, false);
mrecyclerview = (RecyclerView) rootView.findViewById(R.id.recyclerView);
mrecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
populate();
return rootView;
public void populate()
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading Please wait");
progressDialog.show();
Call<FeedList> call = Apis.getApi().getFeedLists(101,"param");
call.enqueue(new Callback<FeedList>()
@Override
public void onResponse(Call<FeedList> call, Response<FeedList> response)
progressDialog.dismiss();
FeedList feedsdata = response.body();
Log.i(TAG, "onResponseFeeds: "+feedsdata.getFeeds());
FeedsStore.setFeedLists(feedsdata.getFeeds());
feedadapter = new FeedsAdapter(feedsdata.getFeeds(),getContext());
mrecyclerview.setAdapter(feedadapter);
如果不添加 Drawerlayout 和导航 Drawer,一切正常,但是当我在 FeedFragment 中包含 DrawerLayout 时,它会分配完整的空间。我没有错。
【问题讨论】:
【参考方案1】:通过将抽屉布局作为父级,将协调器布局作为子级来解决这个问题
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_
android:layout_>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_
android:layout_
app:layout_scrollFlags="scroll|enterAlways"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
【讨论】:
以上是关于抽屉布局重叠 |在 Fragment 中覆盖完整空间的主要内容,如果未能解决你的问题,请参考以下文章