Android app long-click: works in from Android Stud

2019-08-17 07:38发布

This is a really bizarre one.... I've written a very simple Android shopping-list app which uses a RecyclerView. I want the user to long-click on an item to select it for deletion, etc. The long-click works fine when I run the app from Android Studio, both on the emulator and via USB on a real device; it doesn't work when the app is installed independently.

By "doesn't work", I mean that the app simply doesn't respond to a long-click. It doesn't crash or anything; just nothing happens. Strangely, a short-click on the same RecyclerView, using the same mechanism (of which further below), works fine.

Here's what I've tried, in which the long-click doesn't work:

  • Signed release build, uploaded to Play Store and installed from there.

  • Signed release build, APK transferred to phone via USB and installed by clicking on it.

  • Debug build, APK transferred to phone via USB and installed by clicking on it.

I've also commented out everything from the release build type in build.gradle, so (in theory anyway) the release build should be the same as the debug one, but no difference:

buildTypes {
    debug {
        debuggable true
    }
    release {
        // minifyEnabled false
        // proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        // signingConfig signingConfigs.config
    }
}

What am I missing, or what am I doing wrong?

How it's supposed to work

My RecyclerView.ViewHolder implementation responds to short- and long-clicks, and I'm using EventBus to transmit events to the fragment hosting the RecyclerView, which handles them.

class ShoppingListViewHolder extends RecyclerView.ViewHolder
{
  //....
}

@Override
public void onBindViewHolder(final ShoppingListViewHolder holder, final int position)
{
    // ....

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            // This always gets called.
            EventBus.getDefault().post(new ItemClickEvent(item, holder.getAdapterPosition()));
        }
    });

    holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
    {
        @Override
        public boolean onLongClick(View v)
        {
            // This doesn't get called when the app is not run from Android Studio.
            EventBus.getDefault().post(new ItemLongClickEvent(item, holder.getAdapterPosition()));
            return true;
        }
    });
}

Here is the method in the hosting fragment which responds to the events posted to Eventbus:

@Subscribe(threadMode = ThreadMode.MAIN)
void onItemLongClick(ShoppingListAdapter.ItemLongClickEvent event)
{
    // This doesn't get called when the app is not run from Android Studio.
}

As I said above, this all works fine when the app is launched directly from Android Studio, both on the emulator and on a real phone; but once the app is installed independently of Studio, the long-click no longer responds.

Any help will be greatly appreciated.

0条回答
登录 后发表回答