Android 在更新listview之前冻结progressbar

Android 在更新listview之前冻结progressbar,android,listview,progress-bar,adapter,Android,Listview,Progress Bar,Adapter,我想在更新listview之前显示一个圆圈,一切正常,当异步任务的后台从服务器获取数据时,它显示progressbar,但当更新listview时,它冻结了一段时间,然后显示listview,我想在更新前删除progressbar的冻结,这是我的代码 public class feeds extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>{ p

我想在更新listview之前显示一个圆圈,一切正常,当异步任务的后台从服务器获取数据时,它显示progressbar,但当更新listview时,它冻结了一段时间,然后显示listview,我想在更新前删除progressbar的冻结,这是我的代码

 public class feeds extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>{


                protected void onPreExecute() {
                    // SHOW THE SPINNER WHILE LOADING FEEDS
                    linlaHeaderProgress.setVisibility(View.VISIBLE);    
                }

                @Override
                protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
                    new_request_feeds(); //here i am fetching data from server
                    return fetch;
                }

                protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

                    // HIDE THE SPINNER AFTER LOADING FEEDS
                    linlaHeaderProgress.setVisibility(View.GONE);
                     if(result.size()!=0)
                   {
                         adapter=new CustomListAdapter(getActivity(), R.id.list_ongoing, result);

                           list.setAdapter(adapter);               

                   }
                   else
                   {   


                       Toast.makeText(getActivity(), "no feeds", 3000).show();
                   }// Here if you wish to do future process for ex. move to another activity do here



                }

imageloader是我用来缓存图像的一个类。

尝试将这一行
linlaHeaderProgress.setVisibility(View.GONE)
if
else
块之后。希望这对您有用。

我认为问题可能存在于以下几个方面:

1) 您正在适配器的构造函数中对数据进行大量的预处理

请记住,onPostExecute()中的所有内容都在应用程序UI线程上运行,因此,如果适配器的构造函数执行了一些繁重的处理,它可能会锁定UI线程

可以在doInBackground中创建适配器,然后将结果传递给onPostExecute,因此setAdapter调用是其中唯一的调用

2) 适配器中的getView()方法不是很有效

当您调用setAdapter时,AdapterView必须为屏幕上显示的每个单元格调用getView。如果有很多单元格,并且您正在执行许多昂贵的操作,如膨胀视图或查找ViewsByd,那么您可能会锁定UI线程以进行初始加载

你的滚动表现如何?如果缺少,getView()将是第一个开始,我将观看Romain Guy关于ListView性能的精彩演讲,以获得关于如何创建一个好的、高效的适配器的建议


在活动集adpater中的listener onsuccess方法中完成onpostexecute后添加一个侦听器,然后关闭进度它将在活动中工作添加此界面

public interface asyncListener{
        public void onSucess(Object  object);
        public void onFailure(Exception  exception);
    }
并添加一个变量

asyncListener as= new asyncListener() {

            @Override
            public void onSucess(Object object) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFailure(Exception exception) {
                // TODO Auto-generated method stub

            }
        };

在construtor中发送此变量(listenr)或在asynctask construct中发送此变量,并在onpostexecute write
yourlistener.onSuces(结果)中将其发送给侦听器

尝试此链接,可能会对您有所帮助,请在设置后尝试解除adapter@user1920666我已经做了,但没有成功是的,我的滚动性能也不好,即使我正在使用view holder并在适配器内缓存图像,但性能仍然不好。我曾尝试在后台将适配器设置为do,但它给了我错误。这里是我的代码……受保护的CustomListAdapter doInBackground(Void…params){new_request_feeds();adapter=new CustomListAdapter(getActivity(),R.id.list_continuousing,fetch);return adapter;}是的,您不能在doInBackground中设置适配器,但是您上面所做的看起来是正确的,假设您没有在CustomListAdapter构造函数中执行任何需要在UI线程上的操作。我认为您需要分析您的getView()方法来查看您可能正在UI线程上执行的操作,这些操作的速度可能会减慢到足以导致UI打嗝的程度。在我的customlistadapter中,getview()中有一系列if-else条件,我正在根据fetch hashmap中存储的值查找项。我们是否有可能看到您的getview()或CustomListAdapter的构造函数?输入onpostexecute时将调用onsucces方法。您可以在OnSuces中编写方法在任务前的“活动”中显示进度对话框。设置adpter后,在OnSuces方法中调用并取消执行
asyncListener as= new asyncListener() {

            @Override
            public void onSucess(Object object) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onFailure(Exception exception) {
                // TODO Auto-generated method stub

            }
        };