【问题标题】:Progress Bar with Custom List带有自定义列表的进度条
【发布时间】:2018-04-04 03:19:31
【问题描述】:

嘿,我正在尝试使用持有人模式实现自定义电影列表。目前,我正在成功显示列表。我想在加载唯一活动之前显示一个带有一些消息的进度条。我似乎无法用异步任务将我的头绕在进度条上,我已经遵循了一些示例,但似乎无法理解它们,有人可以建议在哪里插入什么以及插入什么以使其工作。我真的很感激。

这是我唯一的活动

 public class MovieRatingsActivity extends ListActivity
    {
    private ArrayList<Movie> movies = new ArrayList<Movie>();
    private LayoutInflater mInflater;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeUI();
    }

    private void initializeUI()
    {
        mInflater = (LayoutInflater) 
           this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
        InputStream inputStream = getResources().openRawResource(   R.raw.ratings);
        movies = Movie.loadFromFile(inputStream);       
        setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));
    }

    //declare a static holder class
    public static class ViewHolder{
        ImageView icon;
        TextView movieText,votesText;
    }

    /** Custom row adatper -- that displays an icon next to the movie name */
    class RowIconAdapter extends ArrayAdapter<Movie> 
    {
        private ArrayList<Movie> movies;        
        public RowIconAdapter(Context c, int rowResourceId, int textViewResourceId, 
                ArrayList<Movie> items)
        {
            super(c, rowResourceId, textViewResourceId, items);
            movies  = items;
        }

        public View getView(int pos, View convertView, ViewGroup parent)
        {

            //declare a holder object
            ViewHolder holder;
            Movie currMovie = movies.get(pos);

            //nly inflate view once and get all the views once
            if (convertView == null)
            {
                convertView = mInflater.inflate(R.layout.listrow, parent, false);
                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.row_icon);
                holder.movieText = (TextView) convertView.findViewById(R.id.row_label);
                holder.votesText= (TextView) convertView.findViewById(R.id.row_subtext);

                //set the holder class
                convertView.setTag(holder);
            }

            else{
                //get all the views once done
                holder = (ViewHolder) convertView.getTag();
            }

            //set all the values in the list
            holder.movieText.setText(currMovie.getName());
            String votesStr = currMovie.getVotes()+" votes";
            holder.votesText.setText(votesStr);
            Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating());
            holder.icon.setImageBitmap(movieIcon);

            return convertView;
        }
    }

我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@android:id/list" 
        android:layout_height="fill_parent"  
        android:layout_width="match_parent" />
</LinearLayout>

任何帮助将不胜感激。

【问题讨论】:

  • 您可以在运行服务获取数据时插入进度条,完成后关闭进度条。
  • 您能否展示一个带有异步任务的示例代码,因为我觉得这很难理解?干杯
  • 我添加了答案,您将通过它了解如何在代码中使用进度条功能,

标签: android android-asynctask progress-bar


【解决方案1】:

onPreExecute 中初始化您的进度条并执行以下操作:

 @Override
protected void onPreExecute() {
    super.onPreExecute();
    try {
        createProgressBar(context);   // add methodology of your progressbar here,     
       }
    }catch (Exception e){
        Log.d(TAG, "Exception: >> "+e.toString());
    }

onPostExecute得到结果后关闭进度条

   @Override
protected void onPostExecute(Object o) {
    super.onPostExecute(o);

    try {
        cancelProgressBar();   // here add your progressbar to be dismissed.
    }catch (Exception e){
        Log.d(TAG, "Exception: >> "+e.toString());
    }

    Log.d(TAG, "On_Post");
}

我希望它能让你知道如何做到这一点。

【讨论】:

    【解决方案2】:

    在启动加载栏时创建一个异步任务,当异步任务完成后,只需将其关闭。

    公共类 MyAsync 扩展 AsyncTask>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //Show you progress bar
        }
    
        @Override
        protected Void doInBackground(Void... voids) {
              InputStream inputStream = getResources().openRawResource(   
                    R.raw.ratings);
              movies = Movie.loadFromFile(inputStream);            
              return movies;
        }
    
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //Hide your progress bar.
            setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));
    
        }
    }
    

    然后在您的 OnCreate 或您想要显示电影列表的任何位置调用此 AsyncTask。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多