【问题标题】:Fragment does not respond to onClick event on custom views片段不响应自定义视图上的 onClick 事件
【发布时间】:2019-09-11 15:33:26
【问题描述】:

我有 MainFragment,并在我的片段中添加了我的 CustomButton 自定义视图。并且,onViewCreated,我将 OnClickListener 设置为视图。但是,fragmenet 不响应点击。

ButtonWithImage

class ButtonWithImage(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

    init {
        val view = View.inflate(context, R.layout.item_custom_btn, this)

        val attributes = context.obtainStyledAttributes(attrs, R.styleable.BadgeIcon)

        view.btn_image.setImageDrawable(attributes.getDrawable(R.styleable.BadgeIcon_image))
        view.btn_text.setText(attributes.getResourceId(R.styleable.BadgeIcon_text, 0))

        attributes.recycle()
    }

}

fragment_main.xml

...
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">

        <corn.transaction.custom_view.ButtonWithImage
                android:id="@+id/my_cards"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                app:image="@drawable/ic_my_cards"
                app:text="@string/btn_text_my_cards"/>

        <corn.transaction.custom_view.ButtonWithImage
                android:id="@+id/transactions"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                app:image="@drawable/ic_transactions"
                app:text="@string/btn_text_transactions"/>

    </LinearLayout>
...

MainFragment.kt

class MainFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.fragment_main, container, false)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        my_cards.setOnClickListener { logMessage("loooooog") }
    }

【问题讨论】:

  • 你在哪里实现点击监听?
  • 你能发logMessage的代码
  • 你应该也添加了item_custom_btn,但它可能只是setClickable(true); setFocusable(true);ButtonWithImage构造函数中丢失了

标签: android android-fragments kotlin listener android-custom-view


【解决方案1】:

我认为你可以通过以下方式解决它:

  1. focusable=falseclickable=false 添加到您的btn_imagebtn_text
  2. focusable=trueclickable=true 添加到此自定义布局的根视图中(也给它 id -> 比如说 custom_button_container
  3. 致电my_cards.findViewById&lt;LinearLayout&gt;(R.id.custom_button_container).setOnClickListener { logMessage("loooooog") }

【讨论】:

    【解决方案2】:

    最佳解决方案是让 XML 中的所有视图都可点击和聚焦为 false。

    您可以使用以下代码:

    my_cards.setOnClickListener { logMessage("loooooog") }
    

    由于所有视图都不可点击和聚焦,因此它们会忽略点击,但不会忽略自定义视图本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      相关资源
      最近更新 更多