求大神,Android代码优化

public class MyListView4 extends ListActivity {


    private List<Map<String, Object>> mData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mData = getData();
        MyAdapter adapter = new MyAdapter(this);
        setListAdapter(adapter);
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "G1");
        map.put("info", "google 1");
        map.put("img","url1" );
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "G2");
        map.put("info", "google 2");
        map.put("img", "url2");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "G3");
        map.put("info", "google 3");
        map.put("img", "url3");
        list.add(map);

        return list;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        Log.v("MyListView4-click", (String)mData.get(position).get("title"));
    }

    public void showInfo(){
        new AlertDialog.Builder(this)
                .setTitle("我的listview")
                .setMessage("介绍...")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();

    }




    public class MyAdapter extends BaseAdapter{

        private LayoutInflater mInflater;


        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

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

            View view = mInflater.inflate(R.layout.vlist2, null);
            ImageView img = (ImageView)view.findViewById(R.id.img);
            TextView title = (TextView)view.findViewById(R.id.title);
            TextView info = (TextView)view.findViewById(R.id.info);
            Button viewBtn = (Button)view.findViewById(R.id.view_btn);

            img.setBackgroundResource((Integer)mData.get(position).get("img"));
            title.setText((String)mData.get(position).get("title"));
            info.setText((String)mData.get(position).get("info"));

            viewBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    showInfo();
                }
            });

            return view;
        }

    }


0

6个回答

listview的优化,主要是在adapter的getview中1.复用view减少findViewById的次数 下面只供参考、、、

public View getView(int position, View convertView, ViewGroup parent) {
MyHolder holder;
if(convertView==null){
convertView =
LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
holder = new MyHolder();
holder.artist =
(TextView) convertView.findViewById(R.id.item_artist);
holder.duration =
(TextView) convertView.findViewById(R.id.item_duration);
convertView.setTag(holder);
}else{
holder = (MyHolder) convertView.getTag();
}
Music m = getItem(position);
holder.artist.setText(m.getArtist()); holder.duration.setText(dateFormat.format(new
Date(m.getDuration())));
return convertView;
}}
public class MyHolder{
private TextView title;
private TextView artist;
private TextView duration;
}

3

listview替换为 recyclerview。最新版的比较建议使用recyclerview。做了很多缓存方面的优化

0

频繁使用的变量看情况可声明为全局的
现在的listview大多数情况被比较好的recyclerview代替了,可以去试试看
getView方法里要减少view的复用,用一个ViewHolder
再一个就是convertView的使用再次优化

0

1、数据结构的优化,不要采用List< Map >这种方式,建议把Map里的数据封装成bean,这样你就不会存在各种强转,看起来代码不用那么乱,要面向对象编程
2、getView方法采用ViewHolder的方式,复用缓存,你这样直接inflater,在滑动的时候回造成内存抖动,从而引起gc,造成界面滑动卡顿
3、showinfo方法,需要做Dialog的封装,不要上来就builder,alertDialog界面也比较丑,一般都自定义dialog
基本每一个方法有有待优化呀,加油

0

Adapter的getView方法中使用convertView去缓存整个item的各个view,这样可以减少滑动时反复的去inflate布局和findview,提高ListView滑动的流畅度

0

这代码没有什么性能或者代码本身的问题,不知道你要优化什么。

-1
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!