[英]Listview in Relative layout
我可能在這里缺少一些簡單的東西,但是我看不到...
因此,我有一個普通的ListView,一個自定義適配器和一個相對布局。 當前,列表視圖中的項目不顯示。 但是,只要listview不在相對布局中(例如:框架布局),自定義適配器就可以工作。 另一方面,只要不使用自定義適配器,我就可以在相對布局中使用listview。
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
ListView list = new ListView (this);
list.SetBackgroundColor (Android.Graphics.Color.Red);
RelativeLayout rl = new RelativeLayout (this);
rl.SetBackgroundColor (Android.Graphics.Color.Blue);
TextView tv = new TextView (this);
tv.Text = "Some text";
tv.SetBackgroundColor (Android.Graphics.Color.Green);
rl.AddView (tv, new RelativeLayout.LayoutParams (400, 100));
List<RelativeLayout> views = new List<RelativeLayout>();
views.Add (rl);
testAdapter adapter = new testAdapter (views, this);
//ArrayAdapter<string> arrayAdapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleListItem1);
//arrayAdapter.Add ("cell 1");
list.Adapter = adapter;
RelativeLayout listContainer = new RelativeLayout (this);
listContainer.AddView (list, new ViewGroup.LayoutParams (400, 600));
listContainer.SetBackgroundColor (Android.Graphics.Color.LightGray);
this.AddContentView (listContainer, new ViewGroup.LayoutParams (ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
}
}
class testAdapter:BaseAdapter{
List<RelativeLayout> views = new List<RelativeLayout>();
Android.Content.Context context;
public testAdapter(List<RelativeLayout> inView, Android.Content.Context cntx){
views = inView;
context = cntx;
}
#region implemented abstract members of BaseAdapter
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)
{
RelativeLayout temp;
if (convertView != null) {
temp = (RelativeLayout)convertView;
temp.SetBackgroundColor (Android.Graphics.Color.Green);
} else {
temp = new RelativeLayout (context);
}
temp.RemoveAllViews ();
if (position < views.Count) {
RelativeLayout refView = views [position];
View refParent = (View)refView.Parent;
if (refParent != null)
((ViewGroup)refParent).RemoveView (refView);
temp.AddView (refView);
temp.SetBackgroundColor (Android.Graphics.Color.Green);
}
return (View)temp;
}
public override int Count {
get {
return views.Count;
}
}
#endregion
}
此時,我只需要將listContainer
從RelativeLayout更改為FrameLayout即可,一切都很好。 或者,我可以保留相對布局,然后將adapter
更改為我注釋掉的arrayAdapter
。 我知道這是可行的,但我沒有使用通常會引起此類問題的任何wrap content
。
其他:getView返回的單元永遠不會獲得任何尺寸。 但是,如果我要強制設置某些尺寸(例如,通過在temp.SetBackgroundColor (Android.Graphics.Color.Green);
下面添加temp.SetBackgroundColor (Android.Graphics.Color.Green);
類似temp.LayoutParameters = new AbsListView(300, 100);
),則綠色單元格實際上會出現,但沒有內容。
對不起,但由於聲譽欠佳,我無法發表評論,只能回復。 也許這不是您要找的解決方案,但它可能會起作用:您是否嘗試過將列表保留在FrameLayout中,然后將FrameLayout放置在RelativeLayout中?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.