【问题标题】:Android onClickListener not fired in custom flavorAndroid onClickListener 未以自定义风格触发
【发布时间】:2017-03-02 12:14:28
【问题描述】:

已编辑

感谢所有在没有尝试帮助的情况下对我投反对票的人。这真的很有用。

这个主要问题是我有 2 个相同的片段,一个是主要的,另一个是高级风味的。所以系统总是加载存储在 main 中的片段,因此永远不会调用按钮监听器。

您可以解决它从主路径中删除片段并以您拥有的每种风格创建片段,这样将选择正确的片段

如果你想在两者中拥有不同版本的同一个类 您需要在两种口味中创建它并将其从 main 中删除。

src/flavor1/java/com/foo/A.java

src/flavor2/java/com/foo/A.java

然后你在src/main/java 中的代码可以做到:

import com.foo.A

根据选择的风味,com.foo.A 的正确版本是 用过。

原始问题

我正在尝试将按钮设置为“高级”风格,而该按钮未在主风格中显示。问题是按钮显示在 UI 中,但从未触发 onClickListener。

我的结构如下:

我有一个包含片段的活动。该活动对于所有风味都是唯一的,但片段将根据风味而改变。其中一种风味包含主风味中未显示的按钮。当点击该按钮时,没有任何反应。

下面是相关代码:

活动xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    ...
    android:orientation="vertical"
    android:weightSum="10"
    tools:context=".activities.ConfigurationActivity">

<ImageView
        ...
        android:layout_weight="2"/>

<FrameLayout
        android:id="@+id/configurationFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"/>

</LinearLayout>

活动代码为:

package com.abc.project.activities;
public class ConfigurationActivity extends AppCompatActivity {
    private Fragment confFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_configuration);

        confFragment = new ConfigurationFragment();
        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.configurationFragment, confFragment)
            .commit();
    }
}

主要片段 xml是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          ...
          android:orientation="vertical"
          tools:context="com.abc.project.fragments.ConfigurationFragment">

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Main fragment"/>

</LinearLayout>

主要片段代码为:

package com.abc.project.fragments;
public class ConfigurationFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_configuration, container, false);

        return view;
    }
}

高级片段 xml 是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          ...
          android:orientation="vertical"
          tools:context="com.abc.project.premium.fragments.ConfigurationFragment">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Premium fragment!"/>

<Button
        android:id="@+id/premiumButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button premium"/>
</LinearLayout>

高级片段代码是:

package com.abc.project.premium.fragments;
public class ConfigurationFragment extends Fragment {
    private Button premiumButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_configuration, container, false);

        premiumButton = (Button)view.findViewById(R.id.premiumButton);
        premiumButton.setOnClickListener(premiumOnClickListener);

        return view;
    }

    private View.OnClickListener premiumOnClickListener = new View.OnClickListener()  {
        @Override
        public void onClick(View view) {
            //THIS CODE NEVER IS FIRED
        }
    };
}

因此,“高级”xml 正确显示,但按钮不会触发 onClickListener。

有人知道为什么会这样吗?

提前致谢。

问候, 凯米托兹。

【问题讨论】:

  • 是否有 2 个类具有相同的 ConfigurationFragment 名称?
  • 是的,但在不同的包中。一个在包装中与主宠有关,另一个在包装中与高级风味有关
  • 在 onclick 事件中使用 view.getId()
  • 如何控制应该使用哪个片段类?看来您的高级片段课程从未被调用过。
  • 尝试在这里找到解决方案:tools.android.com/tech-docs/new-build-system/…

标签: android android-fragments android-productflavors


【解决方案1】:

你的片段需要用View.OnClickListener实现

 public class InputFragment extends Fragment implements View.OnClickListener

然后更改您的代码如下

 onActivityCreated(Bundle savedInstanceState){

 btn = (Button) view.findViewById(R.id.btnClick);

 btn.setOnClickListener(this);

}

@Override
public void onClick(View view){

 switch (v.getId()) {
    case R.id.btn:

        //HERE YOU CAN PERFORM YOUR ACTION
        break;
    default:
        break;
    }
}

【讨论】:

    【解决方案2】:

    在这里找到解决方案:

    Android Studio build flavors - How to have same source files in diverse flavors

    如果你想在两者中拥有不同版本的同一个类 您需要在两种口味中创建它并将其从 main 中删除。

    src/flavor1/java/com/foo/A.java
    
    src/flavor2/java/com/foo/A.java
    

    然后你在src/main/java 中的代码可以做到:

    import com.foo.A
    

    根据选择的风味,com.foo.A 的正确版本是 用过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多