【问题标题】:Android - ListView - lagsAndroid - ListView - 滞后
【发布时间】:2016-02-18 11:48:21
【问题描述】:

我在使用 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);

}

【问题讨论】:

  • 现在我显示了 2 个项目,每个项目我有 6 个项目要更新,现在我的列表视图有更多滞后

标签: android listview scroll lag


【解决方案1】:

我怀疑延迟是由于这段代码

    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");


        }

【讨论】:

  • 嗨。感谢您的回答。我将这个时间减少到 100,然后我删除这段代码。但我仍然有滞后;/
  • 但是如果我评论这个片段我没有更新我的适配器。
  • 这将帮助您找到延迟的位置 - 然后您可以解决这个问题,因为您有 2 个 5 秒的睡眠/延迟/
  • 好的。谢谢你说得对,我的功能更新适配器(调用异步任务)导致我滞后。但我想定期更新我的适配器。也许你有解决方案来解决这个问题?
  • 我建议您在 getView 方法中进行单元格更新。
猜你喜欢
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多