我想实现一个具有以下设计的布局:左侧的项目列表,右侧的详细信息。这仅适用于平板电脑和横向。它可以工作,但 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);
}
}