简体   繁体   English

TabLayout(Android支持设计)中的选项卡不会使内容膨胀

[英]Tab in TabLayout (Android Support Design) doesn't inflate content

I have a tablayout , from android design support library: 我有一个tablayout ,来自android设计支持库:

compile 'com.android.support:design:23.0.1'

With this, I want to populate my tabs. 有了这个,我想填充我的标签。 But I'm failing to do that. 但我没有那样做。 I can create the tabs, but they fail to inflate their respective content: 我可以创建标签,但是它们无法夸大各自的内容:

当前的tablayout,没有内容。

Where it should have entries from LinearListView , an object similar to a ListView imported from this framework: 它应该有来自LinearListView条目,一个类似于从该框架导入的ListView的对象:

compile 'com.github.frankiesardo:linearlistview:1.0.1@aar'

I tried a great number of examples, but I failed to populate each tab. 我尝试了很多例子,但是我没有填充每个标签。 Any suggestions? 有什么建议?

Code: 码:

JAVA: JAVA:
From main fragment: 从主要片段:

OverviewTabLayoutPagerAdapter adapter = new OverviewTabLayoutPagerAdapter(getActivity().getSupportFragmentManager(), productDataContent, getContext());
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);

OverviewTabLayoutPagerAdapter: OverviewTabLayoutPagerAdapter:

public class OverviewTabLayoutPagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 3;

    private String tabTitles[] = new String[] { "REVIEWS", "VIDEOS", "DEALS" };
    private SearchContent productDataContent;
    private Context context;

    public OverviewTabLayoutPagerAdapter(FragmentManager fm, SearchContent productDataContent, Context context) {
        super(fm);
        this.productDataContent = productDataContent;
        this.context = context;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        Log.i("TAB_POSITION", String.valueOf(position));

        if (position == 0) {
            return OverviewTab1Fragment.newInstance(position, productDataContent);
        } else if (position == 1) {
            return OverviewTab2Fragment.newInstance(position, productDataContent);
        } else if (position == 2) {
            return OverviewTab3Fragment.newInstance(position, productDataContent);
        }

        return OverviewTab1Fragment.newInstance(position, productDataContent);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

OverviewTab*Fragment: (the * means the same code structure applies for every fragment): OverviewTab * Fragment :( *表示相同的代码结构适用于每个片段):

public class OverviewTab*Fragment extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";
    public static final String PRODUCT_DATA_CONTENT = "PRODUCT_DATA_CONTENT";

    private int mPage;
    private SearchContent productDataContent;

    public static OverviewTab*Fragment newInstance(int page, SearchContent productDataContent) {
        OverviewTab*Fragment fragment = new OverviewTab*Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        args.putSerializable(PRODUCT_DATA_CONTENT, productDataContent);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
        productDataContent = (SearchContent) getArguments().getSerializable(PRODUCT_DATA_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.overview_tab_fragment, container, false);
        LinearListView tabContentListView = (LinearListView) view.findViewById(R.id.product_content_linear_list_view);

        populateOverviewTab*LinearLayout(tabContentListView, productDataContent);

        return view;
    }

    private void populateOverviewTab*LinearLayout(LinearListView tabContentListView, SearchContent productDataContent) {
        ArrayList<> productData = productDataContent.getContent();

        OverviewTab*ArrayAdapter overviewTab*ArrayAdapter = new OverviewVideosArrayAdapter(
                getContext(),
                tabContentListView,
                productData,
                getActivity()
        );

        tabContentListView.setAdapter(overviewTab*ArrayAdapter);
    ...

XML: XML:
From main fragment: 从主要片段:

...
<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/go_to_store_button"
                android:orientation="vertical">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tablayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabGravity="fill" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"/>

            </LinearLayout>
    ...

overview_tab_fragment.xml: overview_tab_fragment.xml:

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

    <com.linearlistview.LinearListView
        android:id="@+id/product_content_linear_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:showDividers="end"
        android:dividerPadding="5dp"
        app:dividerThickness="2dp">
    </com.linearlistview.LinearListView>

</RelativeLayout>

You can change: 你可以改变:

@Override
    public Fragment getItem(int position) {
        Log.i("TAB_POSITION", String.valueOf(position));

    if (position == 0) {
        return OverviewTab1Fragment.newInstance(position, productDataContent);
    } else if (position == 1) {
        return OverviewTab2Fragment.newInstance(position, productDataContent);
    } else if (position == 2) {
        return OverviewTab3Fragment.newInstance(position, productDataContent);
    }

    return OverviewTab1Fragment.newInstance(position, productDataContent);
}

for this: 为了这:

@Override
    public Fragment getItem(int position) {
        Log.i("TAB_POSITION", String.valueOf(position));

    if (position == 0) {
        return OverviewTab1Fragment.instantiate(context, productDataContent);
    } else if (position == 1) {
        return OverviewTab2Fragment.instantiate(context, productDataContent);
    } else if (position == 2) {
        return OverviewTab3Fragment.instantiate(context, productDataContent);
    }

    return OverviewTab1Fragment.instantiate(context, productDataContent);
}

UPDATE UPDATE

This is an example with Fragment in Array: 这是Fragment in Array的一个例子:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

public class TabsViewPagerAdapter extends FragmentStatePagerAdapter {
    private CharSequence titlesArray[]; // This will Store the Titles of the Tabs which are Going to be passed when TabsViewPagerAdapter is created
    private Fragment tabsArray[];
    private SparseArray<Fragment> registeredFragments;

    // Build a Constructor and assign the passed Values to appropriate values in the class
    public TabsViewPagerAdapter(FragmentManager fm, CharSequence titlesArray[], Fragment[] tabsArray) {
        super(fm);

        this.titlesArray = titlesArray;
        this.tabsArray = tabsArray;
        this.registeredFragments = new SparseArray<>();
    }

    //This method return the fragment for the every position in the View Pager
    @Override
    public Fragment getItem(int position) {
        return tabsArray[position];
    }

    // This method return the titles for the Tabs in the Tab Strip
    @Override
    public CharSequence getPageTitle(int position) {
        return titlesArray[position];
    }

    // This method return the Number of tabs for the tabs Strip
    @Override
    public int getCount() {
        return titlesArray.length;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);

        return fragment;
    }

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }
}

In the Java you create the adapter: 在Java中,您可以创建适配器:

String[] tabTitles = new String[]{"Tab1", "Tab2", "Tab3"};
Fragment[] tabsArray = new Fragment[]{new OverviewTab1Fragment(), new OverviewTab2Fragment(), new OverviewTab3Fragment()};

adapter = new TabsViewPagerAdapter(getSupportFragmentManager(), tabTitles, tabsArray);

And the fragment is something like this: 片段是这样的:

import android.support.v4.app.Fragment;

public class OverviewTab1Fragment extends Fragment {

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

        return v;
    }
}

I hope help you. 我希望能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TabLayout选项卡选择不会更改选项卡内容 - TabLayout tab selection doesn't change the tab content 更改TabLayout(android.support.design.widget)的选定选项卡指示器颜色 - change the selected tab indicator color of TabLayout(android.support.design.widget) Android支持设计库中的TabLayout中的IndexOutOfBoundsException - IndexOutOfBoundsException in TabLayout in android support design lib android.support.design.widget.TabLayout - 无法解析符号“设计” - android.support.design.widget.TabLayout - cannot resolve symbol 'design' 错误膨胀类 android.support.design.widget.TabLayout - Error inflating class android.support.design.widget.TabLayout 如何使用Swipe视图实现android TabLayout设计支持libarary - How to implement android TabLayout design support libarary with Swipe views 解决:SearchView不会过滤TabLayout的每个子标签 - Solved: SearchView doesn't filter in each child Tab of TabLayout 使用JSoup动态加载内容-数据不会膨胀 - Load content dynamically with JSoup - data doesn't inflate 从android.support.design.widget.FloatingActionButton膨胀浮动操作按钮 - Inflate Floating Action Button from android.support.design.widget.FloatingActionButton TabLayout.Tab的getCustomView()在设计库中不公开 - getCustomView() of TabLayout.Tab is not public in Design Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM