【问题标题】:Custom Android Preference w/ custom layout has no focus or click events带有自定义布局的自定义 Android 首选项没有焦点或点击事件
【发布时间】:2016-05-30 15:44:43
【问题描述】:

我有一个包含一些 TextEditPreferences 等的 PreferenceFragment,然后我有这个自定义首选项,它使用 Volley NetworkImageView 而不是默认的 ImageView,并在末尾有一个“转到”图标。布局方面,否则它是从 compat-v14 preference.xml 文件中复制粘贴的。

问题是,当触摸自定义首选项项目时,我无法触发任何焦点、选择或单击事件。我已经尝试了所有我想出的方法:onClick()、onPreferenceTreeClick()、setOnPreferenceClickListener、设置布局 xml android:selectable="true"、android:enabled="true"、android:descendantFocusability="blocksDescendants"...

xml/preferences.xml

<PreferenceCategory android:title="Icon">
    <dev.niko.project.views.GravatarIconPreference android:key="auth.user.avatar"
        android:title="Gravatar" android:summary="This has no focus animation, no click events are fired and certainly no Intent"
        android:selectable="true" android:enabled="true"
        android:layout="@layout/preference_gravatar"
        android:defaultValue="">
        <intent android:action="android.intent.action.VIEW" android:data="https://en.gravatar.com/connect/" />
    </dev.niko.project.views.GravatarIconPreference>
    <Preference
        android:title="Gravatar" android:summary="This works fine">
        <intent android:action="android.intent.action.VIEW" android:data="https://en.gravatar.com/connect/" />
    </Preference>
</PreferenceCategory>

MyPreferencesFragment.java

public class AccountSettingsFragment extends PreferenceFragment
    implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);

    preferences = context.getSharedPreferences(PREF_NAME_AUTH, Context.MODE_PRIVATE);

    GravatarIconPreference gravatarIconPref = ((GravatarIconPreference)findPreference(AVATAR));
    gravatarIconPref.setImageUrl(API.gravatar(preferences.getString(EMAIL, null)));
    gravatarIconPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Log.wtf(TAG, "gravatarIconPref.onPreferenceClick()");
            return false;
        }
    });

    for (String key : preferences.getAll().keySet()) {
        onSharedPreferenceChanged(preferences, key);
    }

}

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    Log.wtf(TAG, "onPreferenceTreeClick(..., "+preference.getKey()+")");
    return super.onPreferenceTreeClick(preferenceScreen, preference);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key) {

    final Preference preference = findPreference(key);

    if (preference != null) {
        Log.d(TAG, "onSharedPreferenceChanged. key="+key+" preference.getKey="+preference.getKey());
    }
}
}

GravatarIconPreference.java

public class GravatarIconPreference extends Preference {

private static final String TAG = GravatarIconPreference.class.getSimpleName();

private Context context;
private View view;
private Drawable icon;

private String url;

public GravatarIconPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setSelectable(true);
    setEnabled(true);
    this.context = context;
}

// also tried overriding onCreateView, no change

@Override
protected void onBindView(View view) {

    super.onBindView(view);

    view.setClickable(true);

    if (view != null && !TextUtils.isEmpty(url)) {
        NetworkImageView gravatar = ((NetworkImageView) view.findViewById(R.id.gravatar));
        if (gravatar != null) {
            gravatar.setImageUrl(url, App.getInstance().getImageLoader());
        }
    }

}

@Override
protected void onClick() {

    Log.wtf(TAG, "onClick()!!!");
    super.onClick();

}

@Override
public Drawable getIcon() {
    // TODO: gravatar-networkimageview
    return super.getIcon();
}

public void setImageUrl(@Nullable String url) { this.url = url; }

}

preference_gravatar.xml source

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:minHeight="?android:attr/listPreferredItemHeightSmall"
              android:gravity="center_vertical"
              android:paddingStart="?android:attr/listPreferredItemPaddingStart"
              android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
              android:background="?android:attr/activatedBackgroundIndicator"
              android:clipToPadding="false"
              android:focusable="true" android:descendantFocusability="blocksDescendants"
              android:baselineAligned="false">

    <LinearLayout android:id="@+id/icon_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="-4dp"
        android:minWidth="60dp"
        android:gravity="start|center_vertical"
        android:orientation="horizontal"
        android:paddingEnd="12dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" android:focusable="false">
        <com.android.volley.toolbox.NetworkImageView android:id="@+id/gravatar"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             app:maxWidth="48dp"
             app:maxHeight="48dp"
             android:layout_marginStart="@dimen/list_icon_margin"
             android:layout_gravity="center" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">
        <TextView android:id="@android:id/title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:singleLine="true"
                  android:textAppearance="?android:attr/textAppearanceListItem"
                  android:ellipsize="marquee"
                  android:layout_alignParentStart="true"
                  android:layout_toStartOf="@+id/chevron"/>
        <TextView android:id="@android:id/summary"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@android:id/title"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:textColor="?android:attr/textColorSecondary"
                  android:maxLines="10"
                  android:layout_alignParentStart="true"
                  android:layout_toStartOf="@+id/chevron"/>

        <ImageView android:id="@+id/chevron"
                   android:src="@mipmap/ic_chevron_right_black_48dp"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_centerVertical="true"
                   android:layout_alignParentEnd="true"
                   android:alpha="0.5"
                   android:contentDescription="go to url" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@android:id/widget_frame"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:gravity="end|center_vertical"
                  android:paddingStart="16dp"
                  android:orientation="vertical" />

</LinearLayout>

【问题讨论】:

  • 似乎从顶部获取适当的教程解决了我的问题:而不是覆盖 onCreateView() 在构造函数中被称为 setLayoutResource()。

标签: java android xml android-layout android-preferences


【解决方案1】:

对于自定义首选项,您需要以编程方式设置点击监听器。

@Override
protected void onBindView(View view) {

    super.onBindView(view);

    view.setClickable(true);

    if (view != null && !TextUtils.isEmpty(url)) {
        NetworkImageView gravatar = ((NetworkImageView) view.findViewById(R.id.gravatar));
        if (gravatar != null) {
            gravatar.setImageUrl(url, App.getInstance().getImageLoader());
            gravatar.setOnClickListener(new OnClickListener() {      
                @Override
                public void onClick(View v) {
                   Log.i("test","clicked");
                }
            }
        }
    }
}

【讨论】:

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