在更新listview之前冻结进度条

时间:2013-10-12 04:47:24

标签: android listview progress-bar adapter

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

 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



                }

我的getview()

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

    final int pos=position;
    System.out.println(position);
    View v = convertView;
    final ViewHolder holder;

    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.feed_row, null);
        holder = new ViewHolder();
       holder.like=(ImageButton) v.findViewById(R.id.like);
        holder.share=(ImageButton) v.findViewById(R.id.share);
        holder.report=(ImageButton) v.findViewById(R.id.report);
        holder.headline_text = (TextView) v.findViewById(R.id.lar);
        holder.topic_text = (TextView) v.findViewById(R.id.mt);
        holder.count_likes = (TextView) v.findViewById(R.id.count_likes);
        holder.count_shares = (TextView) v.findViewById(R.id.count_shares);
        holder.image = (ImageView) v.findViewById(R.id.img1);
        holder.image2 = (ImageView) v.findViewById(R.id.img2);
        v.setTag(holder);

    }
    else
        holder=(ViewHolder)v.getTag();

    mSharedPreferences= v.getContext().getSharedPreferences("mypref", 0);

         holder.headline_text.setText("  "+entries.get(pos).get(TAG_FFN)+" had a chance with "+entries.get(pos).get(TAG_IFN)+"!  ");
           holder.topic_text.setText(entries.get(pos).get(TAG_TOPIC));
           holder.image.setTag(entries.get(pos).get(TAG_FTID));
           holder.image2.setTag(entries.get(pos).get(TAG_ITID));
           holder.count_likes.setText(entries.get(pos).get(TAG_LIKERS)+" likes");
           holder.count_shares.setText(entries.get(pos).get(TAG_SHARERS)+" shares");

           if(entries.get(pos).get(TAG_LIKED).equals("True"))
            {
                 holder.like.setImageResource(R.drawable.like);                     
                 holder.like.setTag("True");
            }
           else
           {
               holder.like.setTag("False");
           }
           if(entries.get(pos).get(TAG_SHARED).equals("True"))
            {   

                 holder.share.setImageResource(R.drawable.share);   
                 holder.share.setEnabled(false);

            }


       //=======================setting image of user==========================================//
         // Loader image - will be shown before loading image


           // whenever you want to load an image from url
           // call DisplayImage function
           // url - image url to load
           // loader - loader image, will be displayed before getting image
           // image - ImageView 


          imgLoader.DisplayImage((image_url.getimage(Long.parseLong(entries.get(pos).get(TAG_FTID))))[0],loader, holder.image);           
          imgLoader.DisplayImage((image_url.getimage(Long.parseLong(entries.get(pos).get(TAG_ITID))))[0],loader, holder.image2);

    //====================================================================================// 



          holder.image.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    if((holder.image.getTag()).equals(mSharedPreferences.getString("USERID", null)))
                    {

                        Toast.makeText(v.getContext(), "Your profile", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                    Intent i=new Intent(v.getContext(),OtherProfilePage.class);
                    i.putExtra("Image_id",entries.get(pos).get(TAG_FTID));  
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    (v.getContext()).startActivity(i);

                    }

            }
            });

          holder.image2.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    if(holder.image2.getTag().equals(mSharedPreferences.getString("USERID", null)))
                    {

                        Toast.makeText(v.getContext(), "Your profile", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                    Intent i=new Intent(v.getContext(),OtherProfilePage.class);
                    i.putExtra("Image_id", entries.get(pos).get(TAG_ITID));

                    (v.getContext()).startActivity(i);

                    }

            }
            });

           holder.like.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                if(holder.like.getTag()=="True")
                {
                     holder.like.setImageResource(R.drawable.like_pressed); 
                     holder.like.setTag("False");
                    new sendlikes().execute("link");

                }
                else
                {   holder.like.setImageResource(R.drawable.like);
                    holder.like.setTag("True");
                    new sendlikes().execute("link");
                }


        }
        });
           holder.share.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                holder.share.setImageResource(R.drawable.share);
                holder.share.setEnabled(false);
            new sendlikes().execute("http://gangster.cloudapp.net/share/",entries.get(pos).get(TAG_CID),mSharedPreferences.getString("person_id",null));


        }
        });

       return v;
      }

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

4 个答案:

答案 0 :(得分:0)

尝试在linlaHeaderProgress.setVisibility(View.GONE);if块之后添加此行else。希望这对你有用。

答案 1 :(得分:0)

我认为这个问题可能存在于几个地方:

1)你在Adapter的构造函数中进行了大量的数据预处理。

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

创建一个适配器可以在doInBackground中完成,然后作为结果传递给onPostExecute,以便setAdapter调用是那里唯一的调用。

2)适配器中的getView()方法效率不高。

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

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

http://www.youtube.com/watch?v=wDBM6wVEO70

答案 2 :(得分:0)

在活动集adpater中的侦听器onsuccess方法中完成onpostexecute后添加侦听器,然后关闭它将起作用的进度

答案 3 :(得分:0)

在您的活动中添加此界面

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

            }
        };

在你的asynctask contruct中的construtor中发送这个varible(listenr),然后在onpostexecute中写一个这个tolistener写yourlistener.onSucess(result);