问题
i've got my Android skills a bit rusty, and i hope someone can help me with this. I've got a TabLayout
with a ViewPager
to swipe the same Fragment
with different data in a ListView
. Everything is working ok, but I've got a problem. The tabs header text hides a part of the Fragment
, causing the first element of the ListView
being hiden behind. I really have tried to find a solution for this problem but i couldn't, I'm sure it must be something simple but no solution. If you need more info, please ask.
I've got this activity layout:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</android.support.design.widget.CoordinatorLayout>
This is the activity code:
public class ParadasActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<Cursor>{
...
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
while (cursor.moveToNext()) {
Tab tab =tabLayout.newTab();
tabLayout.addTab(tab);
}
Fragment code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_paradas, container, false);
paradasAdapter = new ParadasAdapter(getActivity(), null, 0);
mListView = (ListView) view.findViewById(R.id.listview_paradas);
mListView.setAdapter(paradasAdapter);
return view;
}
Fragment layout:
<FrameLayout 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"
android:id="@+id/fragment_paradas"
tools:context="com.pikohsoft.android.tussantander.ParadasActivityFragment"
tools:showIn="@layout/activity_paradas">
<ListView android:id="@+id/listview_paradas"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
And the code for the pager adapter:
public class SublineasPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public SublineasPagerAdapter(FragmentManager fm, int NumOfTabs, Bundle arguments) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
ParadasActivityFragment tab1 = new ParadasActivityFragment();
return tab1;
}
@Override
public int getCount() {
return mNumOfTabs;
}
回答1:
When you use CoordinatorLayout it's Extend from ViewGroup so when you put your ViewPager
(or other kinds of Layout
) in Z-order they overlap (and you see they come on top of each other) so by using
app:layout_behavior="@string/appbar_scrolling_view_behavior"
You tell to CoordinatorLayout hey it's a bit different !!! and it look at your AppBarLayout and put this layout below appbar.
来源:https://stackoverflow.com/questions/38334782/tablayout-header-hides-first-item-on-a-listview-in-a-fragment