gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:57 24 4
gpt4 key购买 nike

我有一个包含一些 TextEditPreferences 等的 PreferenceFragment,然后我有这个自定义首选项,它使用 Volley NetworkImageView 而不是默认的 ImageView,并在末尾有一个“转到”图标。布局方面,否则它是从 compat-v14 choice.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>

最佳答案

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

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

关于java - 带有自定义布局的自定义 Android 首选项没有焦点或单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37529520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com