项目模板中带有ViewStub的ListView - 未显示最后一项的扩展控件

时间:2012-07-24 19:23:14

标签: android-listview xamarin.android viewstub

我有以下应用程序布局: 具有托管片段的LinearLayout的活动,片段在LinearLayout中托管ListView。我已经模仿(不确定它是正确的术语 - 我来自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
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个布局错误。当您对所选呼叫进行充气时,呼叫布局会变大但其父呼叫不会变大,因此底部呼叫布局会从边缘下降。这也是为什么看起来底部没有扩展的原因 - 扩展的内容从底部掉下来。如果您编辑CallDetails.axml以将按钮的layout_height设置为1dp,则可以看到此操作。

您可以通过将activeCallsList / layout_height(在CallsFragment.axml中)更改为match_parent或fill_parent而不是wrap_content来解决此布局问题。 activeCallsList将保留尽可能多的空间,而不是在第一次绘制时保留足够的空间来显示调用。这使您的调用视图按预期运行。

-Max