1

我有一个带有gridview的android活动,每个单元格都包含一个带有单个字符的textview(因此一次屏幕上大约有60-70个字符/单元格)。网格视图的滚动速度缓慢且不流畅,令人无法接受。我尝试用listview替换gridview,listview的滚动速度要快得多。我怎样才能加快速度?

活动布局为:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="56dp"
        android:numColumns="auto_fit" >
    </GridView>

</LinearLayout>

每个单元格内部都是这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="40sp" />

</LinearLayout>

活动的代码是:

public class TestGridActivity  extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_activity);
        GridView gridView = (GridView) findViewById(R.id.gridView1);
        ArrayList<Map<String, String>> list = getData();
        SimpleAdapter arrayAdapter = new SimpleAdapter(this, list, 
            R.layout.grid_layout, new String[] { "literal"}, 
            new int[] { R.id.textView1});
        gridView.setAdapter(arrayAdapter);
    }

}

编辑:pks 要求发布适配器代码,上面的代码我使用了通用的 simpleAdapter,但我尝试了自定义视图,但没有帮助。

public class GridAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Map<String, String>> list;
    private LayoutInflater inflater;
    public static class ViewHolder {
    TextView textView1;
    int position;
    }
    public GridAdapter(Context c, ArrayList<Map<String, String>> l) {
        context = c;
        list = l;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.grid_layout, null);
            holder = new ViewHolder();
            holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.position = position;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView1.setText(list.get(position).get("character"));
        return convertView;
    }
}

我也尝试过提前创建所有视图,但这对滚动速度没有帮助。

4

0 回答 0