自定义 CheckBoxPreference 性能

Custom CheckBoxPreference performance

我正在使用此 CheckBoxPreference Class 来支持 Android 4.2 之前版本的 RTL 文本方向,并根据我的喜好使用自定义字体样式

public class MyCheckBoxPreference extends CheckBoxPreference {


   TextView txtTitle, txtSum;
   View Custmv;


   public MyCheckBoxPreference(Context context) {
    super(context);
}

   public MyCheckBoxPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

   public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

   @Override
   protected void onBindView(@NonNull View view) {
    super.onBindView(view);
       Context context = getContext();
       txtTitle = (TextView) view.findViewById(android.R.id.title);
       txtTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"));
       txtSum = (TextView) view.findViewById(android.R.id.summary);
       txtSum.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"));
}

   @Override
   protected View onCreateView(ViewGroup parent) {
       Context ccc = getContext();
       LayoutInflater inflater = (LayoutInflater) ccc.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       Custmv = inflater.inflate(R.layout.arabic_checkboxpreference_layout, parent, false);
    return Custmv;
}

我在我的 prefs.xml 中使用它,如下所示:

<com.app.myclasses.MyCheckBoxPreference
        android:defaultValue="false"
        android:key="cb_big"
        android:summaryOff="@string/off"
        android:summaryOn="@string/on"
        android:title="@string/Show_Big_Text" />

这里是 arabic_checkboxpreference_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize"
android:id="@+id/sos">

<CheckBox
    android:id="@+android:id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_marginLeft="16dip"
    android:minWidth="56dp"
    android:gravity="center"
    android:layout_gravity="center" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dip"
    android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip"
    android:layout_weight="1"
    android:gravity="end">

    <TextView
        android:id="@+android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"

        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:textColor="?android:attr/textColorPrimary"
        android:text="Title"
        android:layout_alignParentRight="true" />

    <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:maxLines="3"
        android:text="subTitle"
        android:layout_alignRight="@android:id/title"
        android:textColor="?android:attr/textColorSecondary" />

</RelativeLayout>

一切都很好,但问题是当使用我的自定义 class 的多个实例时,我的意思是当在 prefs.xml 中使用多个 CheckBoxPreferences 并且当单击 CheckBoxPreference 时它变得集中在检查或取消检查之前一段时间(约 1-2 秒)。 有什么想法吗?

编辑:

根据 kha 的回答,我删除了从 onBind 方法到构造函数的初始化,现在一切都很顺利:

public MyCheckBoxPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
    this.Custmv = View.inflate(context, R.layout.arabic_checkboxpreference_layout, null);
    TextView txtTitle = (TextView) Custmv.findViewById(android.R.id.title);
    txtTitle.setTypeface(mTypeface);
    TextView txtSum = (TextView) Custmv.findViewById(android.R.id.summary);
    txtSum.setTypeface(mTypeface);
}

这可能与您每次绑定视图时都使用 Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf") 加载字体有关。

尝试将它从您的 onBindView 方法中移出并在您的视图中创建一次,并在您需要设置字体时使用参考,看看是否有任何不同