【发布时间】:2017-10-23 13:48:27
【问题描述】:
假设,我有 N 个来自我的 REST 的类别。我想为所有这些类别添加标签。让我们看看例如假设在我的 REST 响应中只有 2 个类别,它只创建两个选项卡。如果它有 5 个类别,则有 5 个选项卡,依此类推。 并在每个类别选项卡中为所选选项卡(类别)加载我的项目列表。 目前我已经使用 Fragment 用静态数据实现了它。每个片段都使用一个列表视图来加载项目。但我认为这不是正确的方法。
您能否建议我应该使用什么或如何根据我的类别添加标签并为每个类别加载我的列表?我在 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