0

我想实现一个具有以下设计的布局:左侧的项目列表,右侧的详细信息。这仅适用于平板电脑和横向。它可以工作,但 ListView 滚动滞后,整个模式的工作速度非常慢。该列表有大约 20-30 条记录。所有信息均为文字信息。点击和结果之间大约需要 1-2 秒。以下是片段的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="5" >

    <ListView
        android:id="@id/android:list"
        style="@style/TransparentBgListView"
        android:layout_width="0dp"
        android:layout_weight="2" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="@color/gray_bg"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
            android:id="@+id/txt_question"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Question Question Question Question Question Question Question"
            android:textColor="@android:color/black"
            android:textSize="@dimen/faq_question_textSize" />

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/txt_question"
            android:overScrollMode="always"
            android:paddingBottom="15dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txt_answer"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer "
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/faq_answer_textSize" />

            </LinearLayout>
        </ScrollView>
    </RelativeLayout>

</LinearLayout>

我在 onItemClick 中测量了从数据 ArrayList 获取相应信息和设置 textviews 文本之间的时间:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long start = System.currentTimeMillis();
        Question item = questionList.get(position);
        Log.i(TAG, "questionList.get(position): "+(System.currentTimeMillis()-start));
        txtQuestion.setText(item.question);
        Log.i(TAG, "txtQuestion.setText(item.question): "+(System.currentTimeMillis()-start));
        txtAnswer.setText(item.answer);
        Log.i(TAG, "txtAnswer.setText(item.answer): "+(System.currentTimeMillis()-start));
    }  

总共大约20-30ms。那么为什么它在视觉上工作得这么慢呢?我认为这是由于使用了 LinearLayout weightSum,但通过恒定值(300dp 和 600dp)改变布局宽度并没有改变整体视觉速度。以下是适配器(作为内部类包含在片段代码中):

private class QuestionsAdapter extends BaseAdapter {

    private final LayoutInflater inflater = getActivity().getLayoutInflater();

    public QuestionsAdapter() {
    }

    @Override
    public int getCount() {
        return questionList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        QuestionHolder holder = null;

        if(convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_faq, parent, false);
            holder = new QuestionHolder(convertView);
            convertView.setTag(holder);
        } 
        else 
            holder = (QuestionHolder)convertView.getTag();

        Question item = questionList.get(position);

        holder.labelView.setText(item.question);
        FontHelper.setM0FontToViews(holder.labelView);

        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return questionList.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }
}

protected static class QuestionHolder {
    final TextView labelView;
    public QuestionHolder (final View convertView){
        labelView = (TextView)convertView.findViewById(R.id.item_title);
    }
}

下面是 FontHelper 的样子:

public class FontsHelper {
    public final static String FONTHELPER_FONTS_PATH = "fonts/";

    public static Typeface getFont(final Context context, final String font) {
        final Typeface mFont = Typeface.createFromAsset(context.getAssets(), FONTHELPER_FONTS_PATH + font);
        return mFont;
    }

    public static void setFontToView(final TextView view, final String font) {
        final Typeface mFont = getFont(view.getContext(), font);
        if (mFont != null)
            view.setTypeface(mFont);
    }
}
4

1 回答 1

0

由于答案在评论中,我自己回答我的问题。问题是类助手 FontHelper 从资产文件夹加载相应的字体并将其设置为给定的视图或视图。类是这样的:

public class FontsHelper {
    public final static String FONTHELPER_FONTS_PATH = "fonts/";

    public static Typeface getFont(final Context context, final String font) {
        final Typeface mFont = Typeface.createFromAsset(context.getAssets(), FONTHELPER_FONTS_PATH + font);
        return mFont;
    }

    public static void setFontToView(final TextView view, final String font) {
        final Typeface mFont = getFont(view.getContext(), font);
        if (mFont != null)
            view.setTypeface(mFont);
    }
}

所以解决方法是加载一次字体并将其保存在字段中

protected Typeface myFont;

并在适配器中使用它

...
holder.labelView.setText(item.question);
holder.labelView.setTypeFace(myFont);

return convertView;
...

最后,我对 FontHelper 进行了以下更改:

private static Map<String,Typeface> lastUsedFonts = new HashMap<String,Typeface>();
private static String   lastUsedFontFileName;
private static Typeface lastUsedFont;

public static Typeface getFont(final Context context, final String fontFileName) {

    if ( fontFileName.equals(lastUsedFontFileName) )
        return lastUsedFont;

    if (lastUsedFontFileName != null)
        lastUsedFonts.put(lastUsedFontFileName, lastUsedFont);

    lastUsedFontFileName = fontFileName;
    lastUsedFont = lastUsedFonts.get(fontFileName);

    if (lastUsedFont == null)
        lastUsedFont = Typeface.createFromAsset(context.getAssets(), FONTHELPER_FONTS_PATH + fontFileName);

    return lastUsedFont;
}

现在无需创建字段来存储字体。

于 2013-10-16T14:39:20.250 回答