简体   繁体   English

如何从Recycler-View适配器接收活动中的点击事件

[英]How to receive click event in activity from a recycler-view adapter

EDITED on 14.11.2018: 于14.11.2018编辑:

I would like to simplify my question, how can I receive a click event in the activity from the Fragment which contains a recycler-view and it has an adapter which handels the click event? 我想简化我的问题,如何从Fragment中获得一个click事件,该Fragment中包含一个recycler-view,它具有一个处理click事件的适配器?

The application looks the following: 该应用程序看起来如下: 在此处输入图片说明


I am working on an application which is using BLE service (based on the Google BLE example). 我正在使用BLE服务的应用程序(基于Google BLE示例)。 I have a DeviceScanActivity which is successfully able to search and connects to BLE periphery, this activity starts a new activity, DeviceControlActivity and I bind the BLE service to it: 我有一个DeviceScanActivity ,它能够成功搜索并连接到BLE外围设备,此活动启动一个新活动DeviceControlActivity ,并将BLE服务绑定到该活动:

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "onStart: bind Service. " + mServiceConnection);
    Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}

In this activity I am able to send values/messages to the BLE periphery. 在此活动中,我能够将值/消息发送到BLE外设。

My application has another activity, ManualModeActivity . 我的应用程序还有另一个活动, ManualModeActivity When I start this activity I unbind the BLE service first in case of DeviceControlActivity : 当我开始此活动时,如果是DeviceControlActivity,我首先将BLE服务解除绑定:

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop: BLE Service is unbind " + mServiceConnection);
    unbindService(mServiceConnection);
    BLEServiceBonded = false;
}

Then in the newly started activity ( ManualModeActivity ) I bind the BLE service to it, like it is done in case of the DeviceControlActivity . 然后,在新启动的活动( ManualModeActivity )中,将BLE服务绑定到它,就像在DeviceControlActivity的情况下一样。

And now starts the problem, or basically I am new in android programming and I do not know what should I do next to use the BLE service in the ManualModeActivity , I am searching since two days, nothing useful is found. 现在开始出现问题,或者基本上我是android编程的新手,我不知道接下来应该怎么做才能在ManualModeActivity中使用BLE服务,因为搜索了两天,没有发现有用的信息。

So let see what I am not able to do, or what makes the challenge for me. 因此,让我看看我无能为力,或者是什么使我面临挑战。

The ManualModeActivity has a ViewPage with two tabs, I have created a ManualTabPagerAdapter which extends FragmentStatePagerAdapter, in this adapter I am loading the fragments, eg the ManualDeviceFrament . ManualModeActivity有一个带有两个选项卡的ViewPage,我创建了一个ManualTabPagerAdapter来扩展FragmentStatePagerAdapter,在此适配器中,我正在加载片段,例如ManualDeviceFrament This fragment has RecyclerView, every item in the RecyclerView has a button, to handle the list and button clicks I have created a ManualListAdapter . 该片段具有RecyclerView,RecyclerView中的每个项目都有一个按钮,用于处理列表和按钮单击,我创建了ManualListAdapter And now the challenge, if I click on the button I want to send a BLE message, but the BLE service, mBluetoothLeService is null. 现在的挑战是,如果我单击按钮要发送BLE消息,但是BLE服务mBluetoothLeService为空。 I want to use the following command to send message: 我想使用以下命令发送消息:

mBluetoothLeService.writeCustomCharacteristic("Hello");

In case of both activities I have the following code to handle the service connection: 对于这两种活动,我都有以下代码来处理服务连接:

    private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
        if (!mBluetoothLeService.initialize()) {
            Log.e(TAG, "Unable to initialize Bluetooth");
            finish();
        }
        mBluetoothLeService.connect(mDeviceAddress);
        BLEServiceBonded = true;
    }
    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        BLEServiceBonded = false;
    }
}

So guys I would like to ask you to tell me some possible solutions, because I did not find any useful stuff, I am a newbee so maybe some basic knowledge is missing. 伙计们,我想请您告诉我一些可能的解决方案,因为我没有找到任何有用的东西,我是一个新手,所以也许缺少一些基本知识。 If you need further details, just let me know and I will post it here. 如果您需要更多详细信息,请告诉我,我将在此处发布。

did you bind and unbind the service on ManualModeActivity as well? 您是否还在ManualModeActivity上绑定和取消绑定服务? and another thing on overriding onStop method it's better to write overridden code first than calling parent function first. 关于重写onStop方法的另一件事,首先编写重写的代码比先调用父函数更好。 @Override protected void onStop() { unbindService(mServiceConnection); @Override protected void onStop(){unbindService(mServiceConnection); BLEServiceBonded = false; BLEServiceBonded =假; super.onStop(); super.onStop(); Log.d(TAG, "onStop: BLE Service is unbind " + mServiceConnection); Log.d(TAG,“ onStop:BLE服务已解除绑定” + mServiceConnection); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击行时的回收站视图数据绑定 - Recycler-view data binding on click of row 如何在 Recycler-view 中添加分区 - How to add section divider in Recycler-view 从回收者视图适配器到活动中获取价值 - Get value from recycler view Adapter to activity 如何将视图ID从回收站适配器传递给活动? - How to pass the view Id from Recycler Adapter to Activity? 如何将数据从活动传递到 android 中的回收器视图适配器 - How to pass data from a activity to a recycler view adapter in android 在活动中发送和添加回收者视图数据时遇到问题 - Getting a problem in sending and adding recycler-view data in an activity 如何在回收站视图点击上移动不同的活动? - How to move a different activity on recycler view click? 如何从其 Recycler View Adapter 调用 Activity 的方法? 或者更好,如何从适配器访问活动的 UI 元素? - How do i call a method of an Activity from its Recycler View Adapter? Or better,How to access the UI elements of an activity from the adapter? 如何从回收商视图适配器中获取项目 - How to get items from recycler view adapter Android 停止回收器-查看已显示项目的适配器绑定数据 - Android Stop recycler-View adapter binding data for already shown items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM