使用ViewHolder后,我的ListView仍然很慢

时间:2014-02-17 16:06:43

标签: android listview

我正在使用listview和自定义列表项布局。我也在列表视图中使用ViewHolder,但列表视图的性能仍然非常慢。如何解决这个问题?

这是我的Activity.java

public class FilemanagerActivity extends Activity {

    private ListView list;
    private TextView status;
    private String[] list_items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filemanager);

        //Initialization
        list=(ListView)findViewById(R.id.list);

///Function to List a dirs from that dir
        listFiles("/");     
        list.setItemsCanFocus(false);

        CustomAdapter adapter=new CustomAdapter(this, R.layout.list_filemanager, list_items);
        list.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        getMenuInflater().inflate(R.menu.filemanager, menu);
        return true;
    }

    // CustomAdapter
    private class CustomAdapter extends ArrayAdapter<String> {

        String[] values;
        int layout,img_dir;

        LayoutInflater lf;

        public CustomAdapter(Context context, int resource, String[] objects) {
            super(context, resource, objects);
            values = objects;
            layout = resource;          
            lf= getLayoutInflater();
            img_dir=R.drawable.device_mobile;
        }

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

            if(convertView==null)
            {
                convertView = lf.inflate(layout, parent, false);            
                viewholder=new ViewHolder();
                viewholder.checkbox=(CheckBox)convertView.findViewById(R.id.list_checkbox);
                viewholder.txt = (TextView) convertView.findViewById(R.id.list_txt);
                //viewholder.img = (ImageView) convertView.findViewById(R.id.list_img);
                convertView.setTag(viewholder);
            }
            else
            {
                viewholder=(ViewHolder) convertView.getTag();
            }


            viewholder.txt.setText(values[position]);
            //viewholder.img.setImageResource(img_dir);
            viewholder.checkbox.setChecked(true);           
            return convertView;
        }
    }

    //View Holder
    public class ViewHolder
    {
        public TextView txt;
        public ImageView img;
        public CheckBox checkbox;       
    }
}

布局:

<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"    
    tools:context=".FilemanagerActivity" 
    android:orientation="vertical">

    <ListView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list"   
        android:focusable="false"                    
        ></ListView>

</LinearLayout>

列表项布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"    
>        
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"                     
        android:textAppearance="@android:style/TextAppearance.Large"
        android:id="@+id/list_txt"                
        android:layout_weight="0.2"        
        />
    <CheckBox
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list_checkbox"        
        android:layout_weight="0.8"       
        />
</LinearLayout>

我的代码中是否存在任何性能错误?

0 个答案:

没有答案