使用ListView和详细信息视图的片段工作得太慢

时间:2013-10-16 13:20:02

标签: android listview android-listview android-fragments

我想实现一个具有以下设计的布局: 左侧的项目列表,右侧的详细信息。这仅适用于平板电脑和风景。 它工作但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获取相应信息和设置textview文本之间的时间:

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

1 个答案:

答案 0 :(得分:0)

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

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;
}

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