带有自定义布局的自定义 Android 首选项没有焦点或点击事件



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

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

XML/首选项.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>

我的偏好片段.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());
    }
}
}

GravatarIcon偏好.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来源

<?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>

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

@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");
                }
            }
        }
    }
}

最新更新