2013-10-16 22 views
0

我想要實現具有以下設計佈局:左側項目,細節在右邊的列表 。這僅適用於平板電腦和橫向。 它的作品,但ListView的滾動滯後和整個模式工作非常慢。該清單有大約20-30條記錄。所有信息僅供文字使用。點擊和結果之間大約需要1-2秒。 以下是片段的佈局:片段與ListView和細節查看作品太慢

<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); 
    } 
} 
+0

你應該發佈你的適配器代碼..我會假設無論你是列表項目是非常複雜的,或者你沒有正確地回收視圖 – dymmeh

+0

添加它,但我懷疑它的問題來源... – Stan

+2

是「 FontHelper「你自己的班級?你有沒有嘗試刪除它的電話?其他一切都很好。 – dymmeh

回答

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

,現在不需要創建字段來存儲字體。