列表视图中的TextView和按钮xamarin c#

时间:2015-01-05 12:37:04

标签: android-listview

我有一个列表视图,其中我使用c#在Xamarin中连续使用textview,图像和按钮。点击按钮,我需要打开一个新的布局:

这是listview的axml:名为“Friends”

<?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">   
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="378.2dp"
        android:id="@+id/listView1" />
</LinearLayout>

这是listview中每行的axml:名为“Row1”

<?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">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="81.1dp"
        android:id="@+id/linearLayout1">
        <ImageView
            android:src="@drawable/Icon"
            android:layout_width="91.7dp"
            android:layout_height="match_parent"
            android:id="@+id/imageView5" />
        <TextView
            android:text="Text"
            android:layout_width="184.9dp"
            android:layout_height="35.6dp"
            android:id="@+id/textView1"
            android:gravity="center"
            android:textSize="20dp" />
        <Button
            android:text="Chat"
            android:layout_width="62.3dp"
            android:layout_height="37.8dp"
            android:id="@+id/chat1"
            android:layout_marginTop="32dp" />
    </LinearLayout>
</LinearLayout>

这是listview的活动:名为“FriendsActivity”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;   
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace EJIK_Network
{
    [Activity (Label = "FriendsActivity")]          
    public class FriendsActivity : Activity
    {
        ListView listview;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.Friends);

            listview = FindViewById<ListView> (Resource.Id.listView1);

            View rootInAnotherLayout = this.LayoutInflater.Inflate (Resource.Layout.Row1,null);
            Button chat =  rootInAnotherLayout.FindViewById<Button> (Resource.Id.chat1);
            chat.Click += (sender, e) => {           
                var CA = new Intent(this, typeof(ChatingActivity));
                StartActivity (CA); 
            };
        }
    }
}

鳕鱼运行,但当我点击聊天按钮时,它没有打开我在“ChatingActivity”中定义的新布局??????

1 个答案:

答案 0 :(得分:2)

您必须在GetView方法中创建Custom Adapter

public override View GetView(int position, View convertView, ViewGroup parent) {

   var view = (convertView ??
                        this._context.LayoutInflater.Inflate(
                            Resource.Layout.Row1,
                            parent,
                            false)) as LinearLayout;
 Button chat =  view.FindViewById<Button> (Resource.Id.chat1);
    chat.Click += (sender, e) => {           
                var CA = new Intent(this, typeof(ChatingActivity));
                StartActivity (CA); 
            };
}

因此,点击每个列表项的聊天按钮,ChatingActivity将开始。