如何在PreferenceFragment中使长按可长按?

时间:2015-03-18 06:08:46

标签: android android-fragments android-fragmentactivity android-preferences

Here是一个类似的问题,但假设使用了PreferenceActivity

但是,如果没有PreferenceActivity,如何使用PreferenceFragment时如何使首选项可以长按?

PreferenceFragment未提供对ListView的引用(getListView()在API中隐藏),因此无法采用相同的方法。

3 个答案:

答案 0 :(得分:1)

class LongClickablePreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
    /**
     * @return true if the long-click was handled
     */
    var onPreferenceLongClickListener: ((Preference) -> Boolean)? = null

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
        holder.itemView.setOnLongClickListener {
            performLongClick()
        }
    }

    private fun performLongClick(): Boolean {
        if (!isEnabled || !isSelectable) {
            return false
        }
        return onPreferenceLongClickListener?.invoke(this) ?: false
    }
}

对于其他Preference类型也可以采用类似的方法。

答案 1 :(得分:0)

这个解决方案风险稍大,因为它假设超类PreferenceFragment(使用ListView并使用android.R.id.list id)。

我尝试了另一种在pref的视图上设置OnLongClickListener的方法,但这使得复选框无法点击,我无法找到解决方法。所以这是我迄今为止最好的解决方案。

public class MyPreferenceFragment extends PreferenceFragment implements OnItemLongClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity());
        setPreferenceScreen(root);

        CheckBoxPreference pref = new LongClickableCheckBoxPreference(getActivity()) {
            @Override
            public boolean onLongClick(View v) {
                // YAY! you long-clicked me :)
                return true;
            }
        };
        // configure the pref here
        root.addPreference(pref);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View result = super.onCreateView(inflater, container, savedInstanceState);
        if (result != null) {
            View lv = result.findViewById (android.R.id.list);
            if (lv instanceof ListView) {
                ((ListView)lv).setOnItemLongClickListener(this);
            }
            else {
                // oh noes! it looks like this PreferenceFragment does not use the standard id for listviews
            }
        }
        return result;
    }

    private static abstract class LongClickableCheckBoxPreference extends CheckBoxPreference implements View.OnLongClickListener {
        public LongClickableCheckBoxPreference(Context context) {
            super(context);
        }
    }
}

答案 2 :(得分:0)

我在SO上面找到的所有这些答案现在都已过时,因为它们处理基于ListView的首选项屏幕,而AppCmpat和androidx使用RecyclerView。在这种情况下,可以执行以下操作: 1.扩展偏好等级 2.覆盖onBindViewHolder 3.为外部片段/活动定义一些接口,并在Holder.View.OnLongClickListener

中调用接口方法
private static class OrgPreference extends SwitchPreference
    {
        private final OnEntryClickListener mOnEntryClickListener;

        public OrgPreference(Context context, OnEntryClickListener onEntryClickListener) {
            super(context);
            mOnEntryClickListener = onEntryClickListener;
        }

        @Override
        public void onBindViewHolder(PreferenceViewHolder holder) {
            super.onBindViewHolder(holder);

            holder.itemView.setOnLongClickListener(view -> {
                String companyId = OrgPreference.this.getKey();
                mOnEntryClickListener.onEntryLongClick(companyId);
                return true;
            });
        }
    }

在片段/活动中:

interface OnEntryClickListener {
        void onEntryLongClick(String companyId);
    }

...

OrgPreference switchPreference = new OrgPreference(getContext(), new OnEntryClickListener() {
            @Override
            public void onEntryLongClick(String companyId) {
                //Do stuff
            }
        });