我的主要活动布局中有一个ListView控件。在我的ListView中的每个项目上,我有一个按钮,用于查看详细信息(打开新活动)和查看地图(打开外部谷歌地图应用程序)。我遇到的问题是我希望用户能够单击ListView项本身(而不是按钮),以便可以更新地图(位于ListView上方的片段内)。出于某种原因,当我单击ListView项时,我的ListView.itemclick事件不会执行。我是否需要在代码中添加一些内容才能使其正常工作?我已经在网上搜索过,并发现了一些建议,但没有任何作用。
主要活动布局
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="1">
<LinearLayout
android:id="@+id/linearLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_width="match_parent" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="300dp"
class="com.google.android.gms.maps.MapFragment" />
<ListView
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="#FFDAFF7F" android:focusable="false" />
</LinearLayout>
主要活动代码:
protected override void OnCreate(Bundle bundle)
{
try
{
// instantiate markList
this.markerList = new List<Marker>();
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.Main);
// set toolbar
var toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
this.SetActionBar(toolbar);
this.ActionBar.Title = "Customer Map";
// setup map
this.SetUpMap();
// get the current location
this.locationManager = (LocationManager)this.GetSystemService(LocationService);
var criteria = new Criteria { Accuracy = Accuracy.Coarse, PowerRequirement = Power.Medium };
this.provider = this.locationManager.GetBestProvider(criteria, true);
this.currentLocation = this.locationManager.GetLastKnownLocation(this.provider);
this.geoCoder = new Geocoder(this);
// set up background color
this.SetBackgroundColorOfLinearLayout();
// setup the list view
this.listView = this.FindViewById<ListView>(Resource.Id.List);
this.listView.NestedScrollingEnabled = true;
var customer = new CustomerService();
this.tableItems = customer.GetCustomerInfo("104", this.currentLocation.Latitude.ToString(), this.currentLocation.Longitude.ToString());
this.listView.ItemClick += this.ListView_ItemClick;
this.listView.Adapter = new ListOfLocationAdapter(this, this.tableItems);
}
catch (Exception ex)
{
var errorMessage = "An error occurred during processing the main activity";
if (ex.InnerException != null && ex.InnerException.ToString() != string.Empty)
{
errorMessage = ex.InnerException.ToString();
}
var errorOccurred = new Intent(this, typeof(ErrorOccurred));
errorOccurred.PutExtra("ErrorMessage", errorMessage);
this.StartActivity(errorOccurred);
}
}
private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var item = this.tableItems.ElementAt(e.Position);
var item1 = item.FullName;
Toast.MakeText(this, "delegate", ToastLength.Long).Show();
}
地点列表布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="70dp"
android:orientation="vertical"
android:background="#ffffff">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="1"
android:columnCount="2">
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/CustomerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20dip"
android:textStyle="italic"
android:paddingLeft="10dip" />
<TextView
android:id="@+id/Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#000000"
android:paddingLeft="10dip" />
<TextView
android:id="@+id/Distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#000000"
android:paddingLeft="10dip" />
<TextView
android:id="@+id/CustomerNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#000000"
android:paddingLeft="10dip"
android:visibility="invisible" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Details"
android:layout_width="60dp"
android:layout_height="38dp"
android:id="@+id/buttonDetails"
android:textSize="9dp"
android:layout_alignParentRight="true" />
<Button
android:text="Directions"
android:layout_width="80dp"
android:layout_height="38dp"
android:id="@+id/buttonOpenMap"
android:textSize="9dp"
android:layout_alignParentRight="true" />
</LinearLayout>
</GridLayout>
ListOfLocations Adapter
public override View GetView(int position, View convertView, ViewGroup parent)
{
this.view = convertView ?? this.context.LayoutInflater.Inflate(Resource.Layout.ListOfLocations, null);
// fill values into textview
var item = this.items[position];
this.view.FindViewById<TextView>(Resource.Id.CustomerName).Text = item.FullName;
this.view.FindViewById<TextView>(Resource.Id.Address).Text = this.ReturnFullAddress(item);
this.view.FindViewById<TextView>(Resource.Id.Distance).Text = this.FormatDistance(item.ApproxDistance);
this.view.FindViewById<TextView>(Resource.Id.CustomerNumber).Text = item.CustomerNumber;
// add click event to open map button
this.view.FindViewById<Button>(Resource.Id.buttonOpenMap).Click += delegate
{
var geoURL = "geo:0,0?q=" + this.ReturnFullAddress(item) + "&z=20";
var geoUri = Uri.Parse(geoURL);
var mapIntent = new Intent(Intent.ActionView, geoUri);
this.context.StartActivity(mapIntent);
};
// add click event to details button
this.view.FindViewById<Button>(Resource.Id.buttonDetails).Click += delegate
{
var activity = new Intent(this.context, typeof(CustomerDetail));
activity.PutExtra("CustomerNumber", item.CustomerNumber);
this.context.StartActivity(activity);
};
return this.view;
}
答案 0 :(得分:0)
我终于在这个网站上找到了这个问题的答案:
您必须将这些属性值添加到listview项目上的按钮。
var imageButton = view.FindViewById<ImageButton>(Resource.Id.imageButton1);
imageButton.Focusable = false;
imageButton.FocusableInTouchMode = false;
imageButton.Clickable = true;