父LinearLayout onclick,未从子recyclelerview调用

时间:2017-12-09 19:58:38

标签: android android-linearlayout

我有一个线性布局,有很多儿童视图,包括recyclerview。我希望整个父视图是可点击的,当点击孩子时,我希望发生同样的事情。即我想要在父视图中单击的地方,将调用该方法。

现在这个工作正常,无论何时单击父级,都会调用该方法。除了回收者视图,当我点击那里时,没有任何反应。我尝试将recyclerview视图的可点击和可聚焦设置为true和false,但它仍然无法正常工作。

这是我的xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="onTodayMainPageClick"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/date_background"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/date_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:layout_gravity="center"
        android:text="1st October 2017"
        android:layout_marginBottom="10dp"/>


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimary"/>


    <TextView
        android:id="@+id/whats_left_for_today_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"

        android:text="Whats left for today"
        android:textColor="@color/colorPrimary" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/today_recycler_view_summary"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="true"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

    </android.support.v7.widget.RecyclerView>



</LinearLayout>

由于

1 个答案:

答案 0 :(得分:0)

你应该尝试以下列方式,

extension String {
    func localized(_ key: String, arguments: [CVarArg] = []) -> String {
        return Localize.localized(key, arguments: arguments)
    }
}

class Localize {
    static var storeKey = "custom_current_language"

    class func localized(_ key: String, arguments: [CVarArg]) -> String {
        let string: String
        let lang = currentLanguage()

        if let path = Bundle.main.path(forResource: lang, ofType: "lproj"), let bundle = Bundle(path: path) {
            string = bundle.localizedString(forKey: key, value: nil, table: nil)
        } else {
            string = key
        }

        return String(format: string, arguments: arguments)
    }

    class func currentLanguage() -> String {
        if let current = UserDefaults.standard.object(forKey: storeKey) as? String, Bundle.main.localizations.contains(current) {
            return current
        } else if let preferred = Bundle.main.preferredLocalizations.first {
            return preferred
        }
        fatalError("Could not determine application localization")
    }

    class func setCurrentLanguage(_ language: String?) {
        UserDefaults.standard.set(language, forKey: storeKey)
        UserDefaults.standard.synchronize()
    }

}