1

我有一个问题希望你能帮助我!

我有一个活动,我有recycler view一些项目,我使用谷歌融合位置 API 来获取从用户位置到recycler view. 我为 the 制作了一个 click 事件,recycler view并测试了recycler viewand click 事件的代码是否有效。但是当我从位置 API 添加功能时,点击事件停止工作。它不会触发事件,即使当我单击手机上的某个项目时它会发出咔嗒声,但OnItemClick永远不会调用该方法。位置 API 每 20 米更新一次用户的位置,并为此使用异步方法。当我单击输出日志中的其中一项时:

03-21 18:06:19.163 V/BoostFramework(22705): BoostFramework() : mPerf = com.qualcomm.qti.Performance@4b3d5f9

有谁知道我可以做些什么来使点击事件再次起作用?谢谢!

以下是主要活动中位置更新的代码:

[Activity (Label = "Events", MainLauncher = false, Icon = "@drawable/icon")]
    public class MainActivity : AppCompatActivity, GoogleApiClient.IConnectionCallbacks,
        GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener
    {
        GoogleApiClient apiClient;
        Android.Locations.Location location;
        int locationRequestCode = 1;

    RecyclerView rv;
    EventAdapter adapter;

    List<Event> allEvents;
    List<Event> eventsNearby;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);

        var name = FindViewById<TextView>(Resource.Id.eventName);            
        var date = FindViewById<TextView>(Resource.Id.eventDate);
        var desc = FindViewById<TextView>(Resource.Id.eventDesc);
        var city = FindViewById<TextView>(Resource.Id.eventCity);

        var webhandle = new utils.Webhandler();

        BuildAndConnectApi();

        rv = FindViewById<RecyclerView>(Resource.Id.recyclerView);
        rv.SetLayoutManager(new LinearLayoutManager(this));
        rv.SetItemAnimator(new DefaultItemAnimator());

        eventsNearby = new List<Event>();
        allEvents = new List<Event>();

        var json = JsonConvert.SerializeObject(Event.GetEvents());
        allEvents = JsonConvert.DeserializeObject<List<Event>>(json);

        adapter = new EventAdapter(eventsNearby);
        rv.SetAdapter(adapter); 

        adapter.ItemClick += OnItemClick;
    }

    private void OnItemClick(object sender, int position)
    {
        //Log.Info("Click Event:", allEvents[position].name);
        Toast.MakeText(this, "Item clicked!", ToastLength.Short).Show();
    }

    public void BuildAndConnectApi()
    {
        apiClient = new GoogleApiClient.Builder(this, this, this)
            .AddApi(LocationServices.API).Build();
        apiClient.Connect();
    }


    public async void RequestLocationAsync()
    {
        if (apiClient.IsConnected)
        {
            Log.Info("Location:", "Requesting Location Updates");
            var locationRequest = new LocationRequest();

            locationRequest.SetPriority(100);
            locationRequest.SetSmallestDisplacement(20);

            await LocationServices.FusedLocationApi.RequestLocationUpdates(apiClient, locationRequest, this);
        }
        else
        {
            Log.Info("Location:", "Client API not connected");
        }

    }

    public void OnConnected(Bundle connectionHint)
    {
        Log.Info("Location:", "Connected!");
        location = LocationServices.FusedLocationApi.GetLastLocation(apiClient);
        DisplayLocation(location);

        CheckLocationPermission();
        RequestLocationAsync();
    }

    public void OnConnectionSuspended(int cause)
    {
        throw new System.NotImplementedException();
    }

    public void OnConnectionFailed(Android.Gms.Common.ConnectionResult result)
    {
        Log.Info("Location:", "Connected failed!");
        //Log.Info("FEIL:", "Connection failed: ConnectionResult.getErrorCode()" + result.ErrorCode);
        RequestLocationAsync();
    }

    public void OnLocationChanged(Android.Locations.Location location)
    {
        Log.Info("Location:", "Location updates:\n");
        DisplayLocation(location);

        //GetLocationUpdates();
    }

    private void DisplayLocation(Android.Locations.Location location)
    {
        if (location != null)
        {
            Log.Info("Location:", "Latitude: " + location.Latitude.ToString() + "\n" + "Longitude: " + location.Longitude.ToString() + "\n");
        }
        else
        {
            Log.Info("Location:", "No location info available");
        }

    }
4

0 回答 0