首页 > 解决方案 > Android ImageButton在更改片段后停止触发

问题描述

我正在使用 Android Studio 制作一个应用程序,您可以在其中收听下载的音乐,并将音乐上传到云端。目前,我正在尝试制作一个按钮来添加播放列表。这是添加按钮所在的主页片段:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Home Fragment"
        android:textSize="30sp" />

    <ImageButton
        android:id="@+id/new_playlist"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        android:background="@drawable/shadow"
        android:elevation="10dp"
        android:src="@drawable/ic_new" />

</RelativeLayout>

如上所示,该按钮是 ImageButton 图标。下面是添加 onClick 事件的代码:

@Override
protected void onStart() {
    super.onStart();

    ImageButton imgBtn = findViewById(R.id.new_playlist);
    imgBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new NewPlaylistFragment()).commit();
        }
    });

}

这工作正常,显示 NewPlaylistFragment。问题是当我使用导航菜单并单击主页片段时。尝试第二次点击新的播放列表按钮时,没有任何反应。当用简单的 Toast 消息替换片段内容时,每次我点击按钮时都会触发。在进行了更多调查后,我发现无论 onclick 函数中的代码如何,更改为配置文件片段并返回到主页片段都会使新的播放列表按钮无用。如果有帮助,这里是导航重定向的代码:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    switch (item.getItemId()) {
        case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();
            break;
        case R.id.nav_profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ProfileFragment()).commit();
            break;
        case R.id.nav_email:
            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"person@example.com"});
            startActivity(emailIntent);
            break;
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

我唯一的猜测是更改片段会导致 onClick 事件消失,但我不知道如何解决这个问题。

编辑

我尝试在创建新的主片段对象时添加 onClick,但问题仍然存在。

标签: javaandroidandroid-fragments

解决方案


findViewById(R.id.new_playlist)从第一个实例化的HomeFragment. setOnClickListener每次实例化一个新的时都必须找到它(和)。

case R.id.nav_home:
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.fragment_container, new HomeFragment())
            .commit();
    imgBtn = findViewById(R.id.new_playlist);
    imgBtn.setOnClickListener(onClickListner);
    break;

上面是一个快速修复。但理想情况下,我认为,你不应该在课堂上处理and的发现ButtonsetOnClickListenerActivityHomeFragment

此外,关于您的代码,每次R.id.nav_homeR.id.nav_profile选择时,都会产生一个新的HomeFragment或新的。ProfileFragment

为避免多次产生 Fragment,请使用标签字符串和findFragmentByTag(或findFragmentById)。

FragmentManager fragmentManager = getSupportFragmentManager();
Fragment homeFragment = fragmentManager.findFragmentByTag(HomeFragment.TAG);
if (homeFragment == null) {
    homeFragment = new HomeFragment();
}
fragmentManager
        .beginTransaction()
        .replace(R.id.fragment_container, homeFragment, HomeFragment.TAG)
        .addToBackStack(null)
        .commit();

(在上面的示例中,HomeFragment.TAG 是一个字符串常量(静态最终字符串)。


推荐阅读