【问题标题】:Add tabs based on categories in Xamarin.Android在 Xamarin.Android 中添加基于类别的选项卡
【发布时间】:2017-10-23 13:48:27
【问题描述】:

假设,我有 N 个来自我的 REST 的类别。我想为所有这些类别添加标签。让我们看看例如假设在我的 REST 响应中只有 2 个类别,它只创建两个选项卡。如果它有 5 个类别,则有 5 个选项卡,依此类推。 并在每个类别选项卡中为所选选项卡(类别)加载我的项目列表。 目前我已经使用 Fragment 用静态数据实现了它。每个片段都使用一个列表视图来加载项目。但我认为这不是正确的方法。

Here is an example

您能否建议我应该使用什么或如何根据我的类别添加标签并为每个类别加载我的列表?我在 Xamarin.android 中没有找到 TabLayout。

我的主视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">

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

这是我的主要活动:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.MainView);              


            ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

            AddTab(" Favorite", Resource.Drawable.FavoritesIcon, new FavoriteItemFragment());
            AddTab(" Ice", Resource.Drawable.IceCreamIcon, new IceCreamFragment());
            AddTab(" Shushi", Resource.Drawable.SushiIcon, new ShushiFragment());
            AddTab(" Burger", Resource.Drawable.VeggieLoversIcon, new BurgerFragment());
            AddTab(" Biriyani", Resource.Drawable.BiriyaniIcon, new BiriyaniFragment());
            AddTab(" Pasta", Resource.Drawable.PastaIcon, new PastaFragment());
            AddTab(" Pizza", Resource.Drawable.PizzaIcon, new PizzaFragment());
            AddTab(" Sandwich", Resource.Drawable.SandwichIcon, new SandwichFragment());
            AddTab(" Coffee", Resource.Drawable.CoffeeIcon, new CoffeeFragment());



            //no_of_categories = allCategories.Count;   

        }

        private void AddTab(string tabText, int iconResourceId, Fragment view)
        {
            var tab = this.ActionBar.NewTab();
            tab.SetText(tabText);
            tab.SetIcon(iconResourceId);


            tab.TabSelected += delegate (object sender, ActionBar.TabEventArgs e)
            {
                var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);

                if (fragment != null)
                {
                    e.FragmentTransaction.Remove(fragment);
                }
                e.FragmentTransaction.Add(Resource.Id.fragmentContainer, view);
            };

            tab.TabUnselected += delegate (object sender, ActionBar.TabEventArgs e)
            {
                e.FragmentTransaction.Remove(view);
            };
            this.ActionBar.AddTab(tab);
        }

我的片段视图之一:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:background="#FCD972"
    android:minHeight="25px">
  <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/itemListView" />
</LinearLayout>

还有我的片段类:

public override void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);
            FindViews();
            HandleEvents();

            items = itemDataService.GetItemsForCategory(4);

            listView.Adapter = new ItemListAdapter(this.Activity, items);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.MyFragment, container, false);
        }

谢谢

【问题讨论】:

    标签: android-fragments xamarin xamarin.android android-viewpager android-tablayout


    【解决方案1】:

    您能否建议我应该使用什么或如何根据我的类别添加标签并为每个类别加载我的列表?我在 Xamarin.android 中没有找到 TabLayout。

    我不能说你使用的方法是错误的,但是如果你想在 Xamarin.Android 中使用TabLayout,你需要安装Xamarin.Android.Support.Design 包和一些包如Xamarin.Android.Support.v4

    然后你可以设计你的布局,例如:

    <LinearLayout 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">
        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            style="@style/TabLayoutThemes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/colorPrimary"
            app:tabGravity="fill"
            app:tabMode="fixed" />
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    

    更多信息可以参考本教程:Xamarin Android: Working with material design TabLayout and ViewPager

    【讨论】:

    • 感谢您的建议。我已经尝试过您的方法,但主要问题是无法将 Xamarin.Android.Support.Design 组件添加到我的项目中。它抛出一个错误:无法安装包“Xamarin.Android.Support.Design 26.0.2”。您正在尝试将此包安装到以“MonoAndroid,Version=v6.0”为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件。有关详细信息,请联系包作者。有没有其他选项可以代替 TabLayout?
    • @Russell,你项目的目标版本是什么?
    • 使用 Android 版本编译(目标框架)Android 6.0(Marshmallow)最低 Android 版本:Android 5.0(API 级别 21-Lollipop)
    • @Russell,此软件包需要 Android 6.0 及更高版本。请更改项目的目标版本。
    • @Grace Feng - MSFT 对不起兄弟!我将目标版本更改为 Android 6.0/Android 7.0。但它显示相同的错误。
    猜你喜欢
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2023-03-06
    • 2015-07-01
    • 2019-07-12
    相关资源
    最近更新 更多