Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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="wrap_content">
- <TextView
- android:id="@+id/adapter_expandable_listview_header_textview"
- android:layout_width="match_parent"
- android:text="Header"
- android:textStyle="bold"
- android:textSize="14sp"
- android:textColor="@color/textColorSecondary"
- android:layout_height="wrap_content"
- android:layout_margin="16dp" />
- </LinearLayout>
- <?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"
- android:layout_height="wrap_content"
- android:minWidth="25px"
- android:minHeight="25px">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/relativeLayout1"
- android:layout_marginLeft="16dp"
- android:layout_marginRight="16dp"
- android:layout_marginTop="16dp"
- android:layout_marginBottom="20dp"
- android:descendantFocusability="blocksDescendants">
- <TextView
- android:text="Rüttenscheider Stern"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/adapter_expandable_listview_listitem_textview1"
- android:textSize="16sp"
- android:textColor="@color/textColorPrimary" />
- <TextView
- android:text="Essen"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/adapter_expandable_listview_listitem_textview1"
- android:id="@+id/adapter_expandable_listview_listitem_textview2"
- android:textSize="14sp" />
- <TextView
- android:text="Rüttenscheiderstraße"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/adapter_expandable_listview_listitem_textview2"
- android:id="@+id/adapter_expandable_listview_listitem_textview3"
- android:textSize="14sp" />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:id="@+id/adapter_expandable_listview_listitem_overflow_button"
- android:src="@drawable/ic_dots_vertical_grey600_18dp"
- android:background="@null" />
- </RelativeLayout>
- <View
- android:layout_width="match_parent"
- android:layout_marginLeft="16dp"
- android:layout_height="1dp"
- android:background="@color/material_grey_300" />
- </LinearLayout>
- namespace YourNamespace
- {
- /// <summary>
- /// Used to pass groups of objects to the adapter.
- /// </summary>
- internal class ExpandableItem
- {
- public string HeaderTitle;
- public List<FMNavigationListItem> ChildItems;
- }
- /// <summary>
- /// Used internally by the adapter.
- /// </summary>
- internal class SectionedHeaderListItem
- {
- public string HeaderTitle;
- public FMNavigationListItem Item;
- public SectionedHeaderListItem(string headerTitle, FMNavigationListItem item)
- {
- HeaderTitle = headerTitle;
- Item = item;
- }
- }
- /// <summary>
- ///
- /// </summary>
- class SectionedHeaderListViewAdaper : BaseAdapter
- {
- const int TypeItem = 0;
- const int TypeSeperator = 1;
- LayoutInflater mLayoutInflater;
- List<SectionedHeaderListItem> mDataItems;
- List<int> mSectionPositions;
- /// <summary>
- ///
- /// </summary>
- /// <param name="context"></param>
- /// <param name="items"></param>
- public SectionedHeaderListViewAdaper(Context context, List<ExpandableItem> items)
- {
- mLayoutInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
- mSectionPositions = new List<int>();
- // Sort each item via header title.
- List<ExpandableItem> dataItems = items;
- dataItems.Sort(delegate (ExpandableItem e1, ExpandableItem e2)
- {
- return e1.HeaderTitle.CompareTo(e2.HeaderTitle);
- });
- // Sort each child item alphabetically.
- foreach (var d in dataItems)
- {
- d.ChildItems.Sort(delegate (FMNavigationListItem i1, FMNavigationListItem i2)
- {
- return i1.DisplayAttributes[0].CompareTo(i2.DisplayAttributes[0]);
- });
- }
- // Merge all items into one big list.
- mDataItems = new List<SectionedHeaderListItem>();
- int index = 0;
- foreach (var expandableItem in dataItems)
- {
- // Represents a section
- mDataItems.Add(new SectionedHeaderListItem(
- expandableItem.HeaderTitle,
- null));
- mSectionPositions.Add(index);
- index++;
- // Now add the child items for the section
- foreach (var dataItem in expandableItem.ChildItems)
- {
- mDataItems.Add(new SectionedHeaderListItem(
- expandableItem.HeaderTitle,
- dataItem));
- index++;
- }
- }
- }
- public override Java.Lang.Object GetItem(int position)
- {
- return position;
- }
- public override long GetItemId(int position)
- {
- return position;
- }
- public FMNavigationListItem ItemAt(int position)
- {
- return mDataItems[position].Item;
- }
- /// <summary>
- /// One view for the header and another for
- /// the normal cells.
- /// </summary>
- public override int ViewTypeCount
- {
- get
- {
- return 2;
- }
- }
- public override int GetItemViewType(int position)
- {
- if (mSectionPositions.Contains(position))
- return TypeSeperator;
- return TypeItem;
- }
- public override int Count
- {
- get
- {
- return mDataItems.Count;
- }
- }
- public override View GetView(int position, View convertView, ViewGroup parent)
- {
- SectionedHeaderListViewAdaperViewHolder holder = null;
- int rowType = GetItemViewType(position);
- // Inflate the correct layout
- if (convertView == null)
- {
- holder = new SectionedHeaderListViewAdaperViewHolder();
- switch(rowType)
- {
- case TypeItem:
- convertView = mLayoutInflater.Inflate(
- Resource.Layout.adapter_expandable_listview_listitem,
- null);
- break;
- case TypeSeperator:
- convertView = mLayoutInflater.Inflate(
- Resource.Layout.adapter_expandable_listview_header,
- null);
- break;
- }
- convertView.Tag = (SectionedHeaderListViewAdaperViewHolder)convertView.Tag;
- }
- else
- {
- holder = (SectionedHeaderListViewAdaperViewHolder)convertView.Tag;
- }
- //Populate UI components
- SectionedHeaderListItem item = mDataItems[position];
- switch (rowType)
- {
- case TypeItem:
- // Horrible code incomming
- holder.DispAttsTextViews = new List<TextView>();
- holder.DispAttsTextViews.Add(convertView.FindViewById<TextView>(Resource.Id.adapter_expandable_listview_listitem_textview1));
- holder.DispAttsTextViews.Add(convertView.FindViewById<TextView>(Resource.Id.adapter_expandable_listview_listitem_textview2));
- holder.DispAttsTextViews.Add(convertView.FindViewById<TextView>(Resource.Id.adapter_expandable_listview_listitem_textview3));
- holder.DispAttsTextViews[0].Text = item.Item.DisplayAttributes[0];
- holder.DispAttsTextViews[1].Text = item.Item.DisplayAttributes[1];
- holder.DispAttsTextViews[2].Text = item.Item.DisplayAttributes[2];
- break;
- case TypeSeperator:
- holder.HeaderTextView = convertView.FindViewById<TextView>(Resource.Id.adapter_expandable_listview_header_textview);
- holder.HeaderTextView.Text = item.HeaderTitle;
- break;
- }
- return convertView;
- }
- }
- class SectionedHeaderListViewAdaperViewHolder : Java.Lang.Object
- {
- public TextView HeaderTextView { get; set; }
- public List<TextView> DispAttsTextViews { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement