2014-10-08 4 views
0

У меня есть пользовательский элемент управления ListView, который мне нужно отобразить в одной из 2-х вкладокДисплей ListView в Tabs- Android Xamarin

У меня есть Манина макет, как это (main.axml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/mainListView" /> 
</LinearLayout> 

ListItem идет как это (simplerow.xml)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#808080" 
    android:padding="5dip"> 
    <ImageView 
     android:id="@+id/leftImage" 
     android:layout_height="match_parent" 
     android:layout_width="5dip" 
     android:layout_alignParentLeft="true" 
     android:src="@drawable/ic_indicator_green" /> 
    <ImageView 
     android:id="@+id/rightImage" 
     android:layout_width="10dip" 
     android:layout_height="10dip" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dip" 
     android:src="@drawable/ic_alert" /> 
    <TextView 
     android:id="@+id/rowTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/leftImage" 
     android:layout_toLeftOf="@id/rightImage" 
     android:padding="10dp" 
     android:textSize="16sp" 
     android:textColor="#000000" /> 
</RelativeLayout> 

И я создал customlistadaptor (CustomAdaptor.cs)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.Graphics.Drawables; 

namespace uitest 
{ 
    public class CustomAdapter : BaseAdapter 
    { 
     Activity context; 

     public List<String> items; 

     public CustomAdapter(Activity context) //We need a context to inflate our row view from 
      : base() 
     { 
      this.context = context; 

      //For demo purposes we hard code some data here 
      this.items = new List<String>() { 
       "data 1", "data 2", "data 3", "data 4" 
      }; 

     } 

     public override int Count 
     { 
      get { return items.Count; } 
     } 

     public override Java.Lang.Object GetItem(int position) 
     { 
      return position; 
     } 

     public override long GetItemId(int position) 
     { 
      return position; 
     } 

     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      //Get our object for this position 
      var item = items[position];   

      //Try to reuse convertView if it's not null, otherwise inflate it from our item layout 
      // This gives us some performance gains by not always inflating a new view 
      // This will sound familiar to MonoTouch developers with UITableViewCell.DequeueReusableCell() 
      var view = (convertView ?? 
       context.LayoutInflater.Inflate(
        Resource.Layout.simplerow, 
        parent, 
        false)) as RelativeLayout; 

      var rowTextView = view.FindViewById(Resource.Id.rowTextView) as TextView; 
      rowTextView.SetText(item, TextView.BufferType.Normal); 

      //Find references to each subview in the list item's view 
      // var imageItem = view.FindViewById(Resource.id.imageItem) as ImageView; 
      // var textTop = view.FindViewById(Resource.id.textTop) as TextView; 
      // var textBottom = view.FindViewById(Resource.id.textBottom) as TextView; 

      //Assign this item's values to the various subviews 
      // imageItem.SetImageResource(item.Image); 
      // textTop.SetText(item.Name, TextView.BufferType.Normal); 
      // textBottom.SetText(item.Description, TextView.BufferType.Normal); 

      //Finally return the view 
      return view; 
     } 

     public String GetItemAtPosition(int position) 
     { 
      return items[position]; 
     } 
    } 
} 

MainActivity

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 

namespace uitest 
{ 
    [Activity (Label = "uitest", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity :ListActivity 
    { 


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

      var kittens = new [] { "Fluffy", "Muffy", "Tuffy" }; 

      //var adapter = new ArrayAdapter (
      // this, //Context, typically the Activity 
      // Android.Resource.Layout.SimpleListItem1, //The layout. How the data will be presented 
      // kittens //The enumerable data 
      //); 
      // this.ListAdapter = adapter; 

      var customAdapter = new CustomAdapter (this); 
      this.ListAdapter = customAdapter; 

     } 
    } 
} 

Теперь им пытается отобразить это содержимое в одном из 2 tabs.Im довольно нового для android.Should я создаю отдельные действия для создания представлений в вкладках или есть способ добавить эту вещь, которую я создал на вкладку простым способом.

ответ

0

Вы можете создавать вкладки или использовать фрагменты, а затем переключаться с вкладками действий. Вот Xamarin article explaining это.

Смежные вопросы