我有以下应用程序布局: 带有承载片段的线性布局的活动,片段承载线性布局中的列表视图。我已经模板化(不确定这是正确的术语 - 我来自 WPF)如何显示 ListView。有一个始终显示的 3 行部分,以及一个在单击项目时展开的 ViewStub(并且始终只有一个项目要展开)。
在第一次单击每个 ListView 项目时,存根会膨胀(适用于所有项目),然后配置详细信息和 myButton 控件。这适用于所有 ListView 项目 - 但是,对于最后一个项目,详细信息和 myButton 永远不会显示。最重要的是,如果另一个存根被展开,最后一个 ListView 项目将变得不可见——而不是向下移动为当前展开的项目腾出空间。
因此,如果我单击位置 myListView.Items.Count -1 上的 ListView 项目,我看不到任何扩展。如果我单击任何其他 ListView 项目,最后一个 ListView 项目就会消失。
这是片段布局
<?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"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="@string/active_calls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Calls_Header" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ActiveCallsList" />
</LinearLayout>
以及每个 ListView 项的布局
<?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">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tableLayout1"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow1">
<TextView
android:text="Caller Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/CallerName"
android:layout_column="0"
android:layout_span="2" />
</TableRow>
<TableRow
android:id="@+id/tableRow2">
<TextView
android:text="+41 12 345 6789"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/CallerNumber"
android:layout_column="0" />
<TextView
android:text="00:01:25"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/CallDuration"
android:layout_column="1" />
</TableRow>
<TableRow
android:id="@+id/tableRow3">
<TextView
android:text="Ringing"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/CallStatus"
android:layout_column="0" />
</TableRow>
</TableLayout>
<ViewStub
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/viewStub1"
android:inflatedId="@+id/CallDetails"
android:layout="@layout/CallDetails" />
</LinearLayout>
和要扩展的部分
<?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">
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/CallDetailsText" />
<Button
android:text="@string/endCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EndCallButton" />
</LinearLayout>
以及与我自己的从 BaseAdapter 派生的 ArrayAdapter 实现相关联的 ItemClick 处理程序。
void calls_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
if (inflatedView != null)
inflatedView.Visibility = ViewStates.Gone; // make sure only one item is inflated at all times
var obj = e.Parent.GetItemAtPosition(e.Position);
var listView = sender as ListView;
Model.Call t = Model.Calls.MyCalls[e.Position];
string text = t.CallerName + " " + t.CallState;
Log.Debug("SmartAppMobile", "Call " + text + " clicked");
//Toast.MakeText(this.Activity, text, ToastLength.Short).Show();
ViewStub myStub = e.View.FindViewById<ViewStub>(Resource.Id.viewStub1);
bool previouslyFound = false;
if (myStub != null)
{
inflatedView = myStub.Inflate();
}
else
{
Log.Debug("myapp", "View stub not found for " + text);
inflatedView = e.View.FindViewById<View>(Resource.Id.CallDetails);
inflatedView.Visibility = ViewStates.Visible;
previouslyFound = true;
}
TextView details = inflatedView.FindViewById<TextView>(Resource.Id.CallDetailsText);
if (details != null)
details.Text = "Call details go here... " + t.CallerNumber;
else
Log.Debug("myapp", "Call Details Text field not found for " + text);
Button myButton = inflatedView.FindViewById<Button>(Resource.Id.EndCallButton);
if (myButton != null)
{
if (!previouslyFound)
myButton.Click += (x, y) =>
{
Model.Calls.MyCalls.Remove(t);
//((ArrayAdapter)listView.Adapter).NotifyDataSetChanged();
inflatedView.Visibility = ViewStates.Gone;
};
}
}
此外,如果我单击 myButton,应用程序的反应就好像我按下了手机上的后退按钮一样 - 只有在我在代码中添加的 ListView 项才会被删除。
所以我想我必须添加我调用托管片段的活动的方式,以及 ListView 项目的绑定方式:
在我托管 ListView 的片段中:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View fragment = inflater.Inflate(Resource.Layout.CallsFragment, null);
calls = fragment.FindViewById<ListView>(Resource.Id.ActiveCallsList);
calls.Adapter = new ActiveCallsAdapter(this.Activity, Model.Calls.MyCalls);
calls.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(calls_ItemClick);
return fragment;
}
和我的通话数据模型
public class Calls
{
private static List<Call> myCalls;
private static object myLock = new object();
public static List<Call> MyCalls
{
get
{
lock (myLock)
{
if (myCalls != null)
return myCalls;
myCalls = new List<Call>();
myCalls.Add(new Call { CallerName = "some name", CallerNumber = "some phone number", CallState = Model.CallState.Init });
myCalls.Add(new Call { CallerName = "some other name", CallerNumber = "another number", CallState = CallState.Held });
return myCalls;
}
}
}
}
我在主要活动中使用的按钮将新项目添加到列表并显示列表:
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate
{
button.Text = string.Format ("{0} clicks!", count++);
Model.Calls.MyCalls.Add(new Model.Call { CallerName = "test " + count, CallerNumber = "" + count, CallState = Model.CallState.Active });
StartActivity(new Intent(this, typeof(CallsActivity)));
};
当然,CallsActivity
[Activity(Label = "My Activity")]
public class CallsActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.CallsActivity);
// Create your application here
}
}