进度条设置在ViewPager的每个页面上都可见

时间:2014-07-30 13:48:21

标签: android android-viewpager progress-bar visibility image-uploading

我有一个viewpager,用于加载用户从图库中选择的图像。 在该viewpager的最后一页,我有一个上传按钮。 该按钮应该将viewpager中的图像加载到网站。但是,我的问题在这里,我想在viewpager中的每个图像下显示一个进度条。 在我的xml文件中,我将进度条视为不可见,并且每当用户按下上传栏时,进度条将设置为可见。 这适用于我当前的位置(这是最后一个)。但是,如果我向后滑动,我看不到进度条。 我需要在viewpager中的每个图像下显示进度条,而不仅仅是最后一页。 任何帮助表示赞赏。

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import java.util.ArrayList;


public class ViewPagerAdapter extends PagerAdapter {

    // Declare Variables
    int click=0;
    int execute=0;
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    View itemView;
    int id=1;
    Context context;
    int number_pages;
    ArrayList<Uri> uris;
    LayoutInflater inflater;

    /**
     * get the information from the other activity
     * @param context
     * @param imageUris paths of the images
     * @param number_images : number of pages displayed according to the number of images selected
     */
    public ViewPagerAdapter(Context context, ArrayList<Uri> imageUris, int number_images) {
        this.context = context;
        this.uris = imageUris;
        this.number_pages = number_images;
    }

    /**
     * get the number of pages to be used by the initiate number
     * @return number of pages
     */
    @Override
    public int getCount() {
        return number_pages;
    }

    /**
     * get the layout
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    /**
     * initiate the items of the pages
     * @param container
     * @param position : position of the current page
     * @return the view
     */

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView img;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.viewpager_item, container, false);
        final ProgressBar spinner= (ProgressBar)itemView.findViewById(R.id.loading);
        Button btn = (Button) itemView.findViewById(R.id.button);
        // Locate the ImageView in viewpager_item.xml
        img = (ImageView) itemView.findViewById(R.id.View1);
        // Capture position and set to the ImageView
        img.setImageURI(uris.get(position));
        if (number_pages - position == 1) {
            btn.setVisibility(View.VISIBLE);
        }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    click=1;
                CustomViewPagerListener spinners = new CustomViewPagerListener();

                    spinners.onPageScrolled(position,10,10);
                    spinners.onPageSlected(position);

                final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (wifi.isAvailable() && wifi.getDetailedState() == NetworkInfo.DetailedState.CONNECTED || mobile.isAvailable() && mobile.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
                    for (int i = 0; i < number_pages; i++) {
                        mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                        mBuilder = new NotificationCompat.Builder(context);
                        mBuilder.setContentTitle("Upload").setContentText("Upload in Porgress").setSmallIcon(R.drawable.ic_launcher);
                        new Uploader().execute();
                    }
                }
                else{
                    //show alert box
                    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                    alertDialog.setTitle("Error Uploading");
                    alertDialog.setMessage("Please make sure you are connected to the internet");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            //ok
                        }
                    });
                    alertDialog.show();
                }
            }
            class Uploader extends AsyncTask<Void, Integer, Integer> {
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    // Displays the progress bar for the first time.
                    mBuilder.setProgress(100, 0, false);
                    mNotifyManager.notify(id, mBuilder.build());

                }
                @Override
                protected void onProgressUpdate(Integer... values) {
                    // Update progress
                    mBuilder.setProgress(100, values[0], false);
                    mNotifyManager.notify(id, mBuilder.build());
                    super.onProgressUpdate(values);
                }

                @Override
                protected Integer doInBackground(Void... params) {
                    int i;
                    execute++;
                    for (i = 0; i <= 100; i += 20) {
                        // Sets the progress indicator completion percentage
                        publishProgress(Math.min(i, 100));
                        try {
                            // Sleep for 5 seconds
                            Thread.sleep(2 * 1000);
                        } catch (InterruptedException e) {
                            Log.d("TAG", "sleep failure");
                        }
                    }
                    return null;
                }
                @Override
                protected void onPostExecute(Integer result) {
                    super.onPostExecute(result);
                    if(execute==number_pages-1){
                        mBuilder.setContentText("Upload Complete");
                    }
                    // Removes the progress bar
                    mBuilder.setProgress(0, 0, false);
                    mNotifyManager.notify(id, mBuilder.build());
                }
            }
        });
        // Add viewpager_item.xml to ViewPager
        ((ViewPager) container).addView(itemView);
        return itemView;
    }

    /**
     * Destroy the item of the views when done
     * @param container
     * @param position
     * @param object
     */
    @Override
        public void destroyItem (ViewGroup container,int position, Object object){
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);
    }
private class CustomViewPagerListener extends ViewPager.SimpleOnPageChangeListener {
        final ProgressBar spinner= (ProgressBar)itemView.findViewById(R.id.loading);
        @Override
        public void onPageSelected(int p){
           if(click==1){
            spinner.setVisibility(View.VISIBLE);
             }
        }
        @Override
        public void onPageScrolled(int p, float positionOffset, int positionOffsetPixels) {

            if(click==1)
            spinner.setVisibility(View.VISIBLE);

        }
    }
    }

1 个答案:

答案 0 :(得分:2)

XML示例:

  <RealtiveLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >
         <ViewPager
            android:id="@+id/pager" 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
         </ViewPager>

         <ProgressBar
             ....
             android:layout_alignParentBottom="true" />

这会导致Progress进入所有项目,导致进度条根本不包含在ViewPager中。