gpt4 book ai didi

android - fragment 内的 Tablayout

转载 作者:搜寻专家 更新时间:2023-11-01 09:39:18 24 4
gpt4 key购买 nike

我正在尝试在我的一个 fragment 中添加 tablayout,但是尽管我在网络上找到了所有内容,但似乎没有任何东西可以正常工作。在其他 fragment 中,我不想看到选项卡布局。

主页 fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">


<android.support.design.widget.TabLayout
android:id="@+id/home_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabMode="scrollable"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
android:id="@+id/home_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</LinearLayout>

HomeFragment代码:

公共(public)类 HomeFragment 扩展 fragment {

private TabLayout _homeTabLayout;
private ViewPager _homeViewPager;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_fragment,container,false);

final TabLayout tabLayout = (TabLayout) v.findViewById(R.id.home_tabs);
final ViewPager viewPager = (ViewPager) v.findViewById(R.id.home_viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new SyncedEpisodesTabFragment(), "One");
adapter.addFragment(new AvailableEpisodesTabFragment(), "Two");

viewPager.setAdapter(adapter);

tabLayout.setupWithViewPager(viewPager);


return v;

}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter (getChildFragmentManager());
adapter.addFragment(new AvailableEpisodesTabFragment(), "PHOTOS");
adapter.addFragment(new SyncedEpisodesTabFragment(), "HI-FIVES");
viewPager.setAdapter(adapter);
}

private class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

应用栏主.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.follow.series.activities.MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
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" />

<!--I'm inserting the tab layout here-->


</android.support.design.widget.AppBarLayout>

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

在这种情况下,我只看到带有没有标签的文本的 viewpager当我在工具栏中添加选项卡布局时,我一直都能看到选项卡。

最佳答案

在 Muhammad Hafiq Iqmal 的回答下,我设法通过添加另一个包含工具提示和 fragment 容器的线性布局来解决这个问题

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.follow.series.activities.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
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.AppBarLayout>



<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


</LinearLayout>


<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

我没有注意到这一点,但现在我的工具栏有问题,它底部有一个阴影,即使它在选项卡布局下方也是如此。我尝试在选项卡布局和工具栏元素中使用 elivation 属性,但没有成功 strange shadow

关于android - fragment 内的 Tablayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40876603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com