我正在尋找使用可擴展列表視圖實現片段。搜索後,我發現一個類實現可擴展列表視圖片段https://gist.github.com/1316903。但我不知道如何使用它。請幫幫我。我試過列表片段,但我不知道如何使用帶有可擴展列表視圖的片段。Android可擴展列表視圖片段
回答
這是我在MonoDroid/C#中獲得的。我不得不去掉一些代碼(爲了保密),但它應該基本完成。
此實現可以支持不同的子視圖,其中每個組都包含一個特定的子類型,該子類型在整個組中都是相同的。但是,您可以在一個組中混合使用子類型,甚至可以使用不同的組類型(這實際上只改變了組標題;我對兩個組使用相同的組類型,並使用該組的位置來確定子組 - 類型 - 所以group [0]包含ExpandableListChild1的子項和ExpandableListChild2的組[1] - 就像打算使用的那樣)。
此處的子類型僅根據它們的背景顏色(爲了簡單起見)不同,但是這些視圖可以是任何您需要的視圖,包括自定義視圖。只需爲您需要的任何視圖創建相應的ExpandListChild子類即可。此外,基類ExpandListChildAbs可以是任何您需要的來適合您的應用程序。如果您只有一個子類型,則不需要基類,但是如果有兩個或更多基類,則需要某種基類,這兩個子類型都可以繼承以支持BaseExpandListListAdapter方法中的多態性getChild和相關方法。
[Activity(
Label = "ExpandableListView in a Fragment",
Theme = "@android:style/Theme.NoTitleBar",
MainLauncher = false,
ConfigurationChanges = ConfigChanges.KeyboardHidden,
WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden)]
public class Fragment1 : Fragment
{
private ViewGroup _thisView;
private Bundle _bundle;
private LayoutInflater _inflater;
private ViewGroup _container;
public override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_bundle = bundle;
_model = Model;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
base.OnCreateView(inflater, container, bundle);
_inflater = inflater;
_container = container;
_bundle = bundle;
Render();
return _thisView;
}
public override void OnAttach(Activity activity)
{
base.OnAttach(activity);
_dialogListener = (IDialogWindow)activity;
}
//public Context LocalContext { get; set; }
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
}
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
}
public override void Render()
{
_thisView = (ViewGroup)_inflater.Inflate(Resource.Layout.MainLayout, _container, false);
ExpandableListView elvParcelInfo = _thisView.FindViewById<ExpandableListView>(Resource.Id.elv_parcel_info);
List<ExpandListGroup> expandListItems = SetStandardGroups();
ExpandListAdapter expandListAdapter = new ExpandListAdapter(Activity.ApplicationContext, expandListItems);
elvParcelInfo.SetAdapter(expandListAdapter);
}
public List<ExpandListGroup> SetStandardGroups()
{
List<ExpandListChild1> childern1 = new List<ExpandListChild1>();
for (int i = 0; i < 20; i++)
{
ExpandListChild1 child1 = new ExpandListChild1();
child1.Name = "child1 #" + i.ToString();
child1.Tag = null;
childern1.Add(child1);
}
ExpandListGroup group1 = new ExpandListGroup();
group1.Name = "Comedy";
//group1.Items = childern1;
group1.Items = new List<ExpandListChildAbs>();
foreach (ExpandListChild1 child1 in childern1)
{
group1.Items.Add(child1);
}
/////////////////////////////////////////////////////////////
List<ExpandListChild2> childern2 = new List<ExpandListChild2>();
for (int i = 0; i < 20; i++)
{
ExpandListChild2 child2 = new ExpandListChild2();
child2.Name = "child2 #" + i.ToString();
child2.Tag = null;
childern2.Add(child2);
}
ExpandListGroup group2 = new ExpandListGroup();
group2.Name = "Action";
//group2.Items = childern2;
group2.Items = new List<ExpandListChildAbs>();
foreach (ExpandListChild2 child2 in childern2)
{
group2.Items.Add(child2);
}
/////////////////////////////////////////////////////////////
List<ExpandListGroup> groups = new List<ExpandListGroup>();
groups.Add(group1);
groups.Add(group2);
return groups;
}
public abstract class ExpandListChildAbs : Java.Lang.Object
{
public abstract String Name { get; set; }
public abstract String Tag { get; set; }
}
public class ExpandListChild1 : ExpandListChildAbs
{
public override String Name { get; set; }
public override String Tag { get; set; }
}
public class ExpandListChild2 : ExpandListChildAbs
{
public override String Name { get; set; }
public override String Tag { get; set; }
}
public class ExpandListGroup : Java.Lang.Object
{
public String Name { get; set; }
public List<ExpandListChildAbs> Items { get; set; }
}
public class ExpandListAdapter : BaseExpandableListAdapter
{
private enum ChildTypes { ChildType1, ChildType2 }
private Context context;
private List<ExpandListGroup> groups;
public ExpandListAdapter(Context context, List<ExpandListGroup> groups)
{
this.context = context;
this.groups = groups;
}
public void AddItem(ExpandListChildAbs item, ExpandListGroup group)
{
if (!groups.Contains(group))
{
groups.Add(group);
}
int index = groups.IndexOf(group);
List<ExpandListChildAbs> ch = groups[index].Items;
ch.Add(item);
groups[index].Items = ch;
}
public override bool HasStableIds
{
get { return true; }
}
public override bool IsChildSelectable(int arg0, int arg1)
{
return true;
}
//______________________________________________________________________________________________________
// Get Child Methods
//______________________________________________________________________________________________________
public override long GetChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public override int GetChildrenCount(int groupPosition)
{
List<ExpandListChildAbs> chList = groups[groupPosition].Items;
return chList.Count;
}
public override int ChildTypeCount
{
get { return Enum.GetNames(typeof(ChildTypes)).Length; }
}
public override int GetChildType(int groupPosition, int childPosition)
{
//return base.GetChildType(groupPosition, childPosition);
if (groupPosition == 0)
{
return (int)ChildTypes.ChildType1;
}
if (groupPosition == 1)
{
return (int)ChildTypes.ChildType2;
}
return 0;
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
List<ExpandListChildAbs> chList = groups[groupPosition].Items;
return chList[childPosition];
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View view, ViewGroup parent)
{
int ChildType = GetChildType(groupPosition, childPosition);
if (ChildType == (int)ChildTypes.ChildType1)
{
return GetChildView_ChildType1(groupPosition, childPosition, isLastChild, view, parent);
}
if (ChildType == (int)ChildTypes.ChildType2)
{
return GetChildView_ChildType2(groupPosition, childPosition, isLastChild, view, parent);
}
return null;
}
private View GetChildView_ChildType1(int groupPosition, int childPosition, bool isLastChild, View view, ViewGroup parent)
{
ExpandListChild1 child1 = (ExpandListChild1)GetChild(groupPosition, childPosition);
if (view == null)
{
LayoutInflater infalInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = infalInflater.Inflate(Resource.Layout.ExpandList_ChildItem1, null);
}
TextView tv = view.FindViewById<TextView>(Resource.Id.tvChild1);
tv.Text = child1.Name;
tv.Tag = child1.Tag;
return view;
}
private View GetChildView_ChildType2(int groupPosition, int childPosition, bool isLastChild, View view, ViewGroup parent)
{
ExpandListChild2 child2 = (ExpandListChild2)GetChild(groupPosition, childPosition);
if (view == null)
{
LayoutInflater infalInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = infalInflater.Inflate(Resource.Layout.ExpandList_ChildItem2, null);
}
TextView tv = view.FindViewById<TextView>(Resource.Id.tvChild2);
tv.Text = child2.Name;
tv.Tag = child2.Tag;
return view;
}
//________________________________________________________________________________________________________
//______________________________________________________________________________________________________
// Get Group Methods
//______________________________________________________________________________________________________
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override int GroupCount
{
get { return groups.Count; }
}
public override int GroupTypeCount
{
get { return base.GroupTypeCount; }
}
public override int GetGroupType(int groupPosition)
{
return base.GetGroupType(groupPosition);
}
public override Java.Lang.Object GetGroup(int groupPosition)
{
return groups[groupPosition];
}
public override View GetGroupView(int groupPosition, bool isLastChild, View view, ViewGroup parent)
{
ExpandListGroup group = (ExpandListGroup) GetGroup(groupPosition);
if (view == null)
{
LayoutInflater inf = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
view = inf.Inflate(Resource.Layout.ExpandList_GroupItem, null);
}
TextView tv = view.FindViewById<TextView>(Resource.Id.tvGroup);
tv.Text = group.Name;
return view;
}
//________________________________________________________________________________________________________
}
}
MainLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_left_parcel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF8F8D8F">
<ExpandableListView
android:id="@+id/elv_parcel_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFCFCDCF"
android:groupIndicator="@null"/>
</LinearLayout>
ExpandList_GroupItem
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:background="#FF00AA55"
android:orientation="vertical" >
<TextView
android:id="@+id/tvGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:textColor="#FF000000"
android:textSize="17dip" />
</LinearLayout>
ExpandList_ChildItem1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:background="#FF8800CC"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChild1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textSize="17dip" />
</LinearLayout>
ExpandList_ChildItem2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:background="#FFAA00FF"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChild2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textSize="17dip" />
</LinearLayout>
Android在ApiDemos項目中有很多工作代碼示例。你可以在eclipse中以New - >「Android Sample Project」的形式獲取它。 ApiDemos有一個可擴展列表的好例子。在使用更復雜和先進的示例之前,這是一個非常好的起點。
你好我試過簡單的可擴展列表視圖..但我必須與碎片整合..這是我的要求..請幫助我.. – Sunny
- 1. 可擴展列表視圖與片段
- 2. 類擴展片段和列表視圖
- 3. Android可擴展列表視圖
- 4. Android中的可擴展列表視圖
- 5. Android可擴展列表視圖 - 網頁
- 6. Android的可擴展列表視圖
- 7. Android:可擴展列表視圖實現
- 8. Android 2級可擴展列表視圖
- 9. 可擴展的列表視圖android json
- 10. Android可擴展列表視圖
- 11. Android可擴展列表視圖
- 12. 可擴展列表視圖android
- 13. 片段中的可擴展列表視圖
- 14. 可擴展列表視圖中的活動片段?
- 15. 可擴展列表視圖片段<實現問題>
- 16. 可擴展列表視圖
- 17. 可擴展列表視圖中的不同子視圖android
- 18. 可擴展列表視圖android中的樹視圖
- 19. 圖片clickable在一個可擴展列表視圖
- 20. Android擴展列表搜索視圖
- 21. Android使用擴展列表視圖
- 22. android中可擴展列表視圖中的圖像圖標
- 23. 可擴展列表視圖滾動到列表視圖中
- 24. 改變列表視圖(可擴展列表視圖)的backgroundColor
- 25. 刷新可擴展列表視圖
- 26. 可擴展列表視圖刷新
- 27. 可擴展的列表視圖問題
- 28. 拖放可擴展列表視圖
- 29. Windows Phone 8可擴展列表視圖
- 30. n級可擴展列表視圖
我得到了一個片段內工作,但所有的代碼是C#/ MonoDroid的,你要我將它張貼? – samosaris