Android:如何刷新偏好设置用户界面

时间:2020-03-16 06:28:14

标签: android android-preferences preferenceactivity android-10.0

我正在制作一个PrefreenceFragment,其中将Preferences添加到PreferenceGroup。 我对首选项有自己的布局,根据某些系统广播,首选项应更新其UI。

以下是我的代码示例:

public static class SettingsFragment extends PreferenceFragmentCompat {
    Activity activity = null;
    String TAG = "SettingsFragment";
    PreferenceCategory category;
    MyPreference mPreference;
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (Activity) getActivity();
        category = findPreference("MessageKey");
    }

    //called when a system broadcast is received
    public void refreshUI() {
        mPreference.refresh();
    }

    @Override
    public void onResume() {
        super.onResume();
        category = findPreference("MyPreference");
        category.setLayoutResource(R.layout.preference_group_layout);

        MyPreference preference = new MyPreference(getPreferenceScreen().getContext(), category);
        mPreference = preference;
        preference.setKey("P");
        preference.setTitle("i");
        preference.setLayoutResource(R.layout.preference_layout);
        Log.d(TAG, "" + category.addPreference(preference));
    }
}

class MyPreference extends Preference {

    PreferenceGroup mgroup ;
    Context mContext;
    ViewHolder mViewHolder;

    public MyPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
    }

    public MyPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    public MyPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public MyPreference(Context context, PreferenceGroup category) {
        super(context);
        mContext = context;
        mgroup = category;
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        Log.d("MyPreference","onBindViewHolder");
        super.onBindViewHolder(holder);
        TextView tv = (TextView)holder.findViewById(R.id.title);
        mViewHolder = new ViewHolder();
        mViewHolder.tv = tv;
        tv.setText("ABCCCCCCCCCCCCCC");
    }

    public void refresh(){
        Log.d("MyPreference","abc called");
        if(mViewHolder == null) {
            Log.d("MyPreference","abc called mViewHolder is null");
            return;
        }
        mViewHolder.tv.setText("PQR");

    }

    private static class ViewHolder{
        TextView tv;
    }

}

但是我观察到的是,甚至在refresh()方法之前,也都调用了onBindViewHolder()时,在mViewHolder中初始化时,我的本地变量onBindViewHolder()被发现为空。 / p>

我要确保在onBindViewHolder()方法之前调用refresh(),并在收到系统广播后刷新UI。

我在互联网上搜索了此内容,但没有得到任何帮助。另外,建议将对Prefrence的视图的引用存储在ViewHolder类中。

0 个答案:

没有答案