TabLayout标头隐藏片段中ListView的第一项 [英] TabLayout header hides first item on a ListView in a fragment

查看:75
本文介绍了TabLayout标头隐藏片段中ListView的第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android技能有些生锈,我希望有人可以帮助我.我有一个带有ViewPagerTabLayout,可以用同一数据在ListView中滑动相同的Fragment.一切正常,但我遇到了问题.选项卡标题文本隐藏了Fragment的一部分,从而导致ListView的第一个元素被隐藏在后面.我确实试图找到解决此问题的方法,但是我不能,我敢肯定它一定是简单但没有解决方法.如果您需要更多信息,请询问. 我有这个活动布局:

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>

这是活动代码:

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);
    }

片段代码:

@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;
}

片段布局:

<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;
}

推荐答案

当您使用 CoordinatorLayout 时,它是从 ViewGroup 扩展而来的,因此当您放置ViewPager(或其他种类的Layout)在Z顺序中重叠(并且您会看到它们相互重叠),因此可以使用

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"

您告诉 CoordinatorLayout ,嘿,这有点不同!!!然后查看您的 AppBarLayout 并将此布局放在appbar下方.

You tell to CoordinatorLayout hey it's a bit different !!! and it look at your AppBarLayout and put this layout below appbar.

这篇关于TabLayout标头隐藏片段中ListView的第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆