Android - ListView - 滞后

时间:2016-02-18 11:48:21

标签: android listview scroll lag

我在android上的应用程序有问题。更具体地说,我遇到了滞后ListView的问题。我使用课程ViewHolder。实际上我没有展示很多东西,但是我的移动物品有很大的滞后。

我不知道我能改变什么,我试图在搜索中找到答案,但没有找到解决这个问题的方法。

我可以滚动我的视图,但是在几秒钟的间隔内,我的视图会停止2秒。我认为这是更新适配器的问题,但我不知道我能做些什么改变。

抱歉我的英文。

以下是适配器的代码:

public class ListAdapter_obiekty extends BaseAdapter{
    Context ctx;
    LayoutInflater lInflater;
    private int screenHeight;
    ArrayList<HashMap<String, String>> products,dane;

    ListAdapter_obiekty(Context context, ArrayList<HashMap<String, String>> data) {

        ctx = context;
        this.products = data;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        screenHeight = metrics.heightPixels;

    }

      @Override
    public int getCount() {
        return products == null ? 0 : products.size();
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void updateItem(final String name, final String item, final Integer pos) {
        HashMap<String,String> hm = this.products.get(pos);
        hm.put(name, item);
        this.products.set(pos,hm);
        notifyDataSetChanged();
    }


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

        View view = convertView;

        HashMap<String, String> hm = this.products.get(position);
        String tryb = hm.get("tryb");

        ListViewHolder holder = null;

        if (view == null) {
            view = lInflater.inflate(R.layout.list_item, parent, false);
            ViewGroup.LayoutParams params = view.getLayoutParams();
            if (params.height != screenHeight) params.height = screenHeight/2;
            view.setLayoutParams(params);
            holder = new ListViewHolder();
            holder.tryb = ((TextView) view.findViewById(R.id.tV_tryb));

            view.setTag(holder);
        }else{
            holder = (ListViewHolder) view.getTag();
        }
        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.lista_obiekty_niebieski);
            holder.tryb.setTextColor(Color.parseColor("#e3e9e4"));

        }else{
            view.setBackgroundResource(R.drawable.lista_obiekty_pod_logo);
            holder.tryb.setTextColor(Color.parseColor("#007bb3"));
        }


        holder.tryb.setText(hm.get(ctx.getString(R.string.tryb)));

        return view;
    }


    static class ListViewHolder{
        TextView tryb;
    }

}

以下是创建适配器和更新的代码:

 runOnUiThread(new Runnable() {
        public void run() {

            adapter = new ListAdapter_obiekty(HomeActivity.this, productsListNAZWY,productsListDANE);
            ListView lvMain = (ListView) findViewById(R.id.list);
            lvMain.setAdapter(adapter);
            for(int i =0; i<=ile; i++){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                HashMap news = null;

                news = (HashMap) productsListNAZWY.get(i);


                String ip = (String) news.get("numer_ip");

                callAsynchronousTask(i, "http://" + ip + "/config/xml.cgi?A|1|74|I|1|29|D|1|62");
                Log.d("TAGS","URUCHOMILEM");


            }

        }

这是我的函数callAsynchronousTask():

public void callAsynchronousTask(final int i, final String numerIPx) {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {


                    try {

                        obiekt=null;
                        obiekt = new ListaXML();

                        String[] tabAnal = obiekt.execute(numerIPx).get();

                        Integer code = obiekt.code;

                        String trybPracy = "";

                        trybPracy = tabAnal[10];

                        HashMap maps = new HashMap<String,String>();

                        ... more items (5-6)

                        maps.put("trybPracy", trybPracy);

                        adapter.updateItem(maps,i);

                    } catch (Exception e) {
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 5000);

}

1 个答案:

答案 0 :(得分:0)

我怀疑滞后是由于代码的这一部分

    try {
      Thread.sleep(5000);
      } catch (InterruptedException e) {
            e.printStackTrace();
      }

您在主UI线程中执行此操作。

将此时间减少到100,看看会发生什么。如果是原因,您将不得不重新编写代码,因为列表适配器似乎没有任何问题。

编辑:注释掉这段代码以找到延迟。

 for(int i =0; i<=ile; i++){
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            HashMap news = null;

            news = (HashMap) productsListNAZWY.get(i);


            String ip = (String) news.get("numer_ip");

            callAsynchronousTask(i, "http://" + ip + "/config/xml.cgi?A|1|74|I|1|29|D|1|62");
            Log.d("TAGS","URUCHOMILEM");


        }