8

我想像下图那样实现progressBar。

现在,我有从片段类中分离出来的 AsyncTask,我使用该 AsyncTask 来加载数据,现在我已经实现了 progressBar 对话框,但我想摆脱它,因为我想要制作更好的 UX。

我在 onPreExecute() 方法中显示 ProgressBar 对话框,然后在 onPostExecute 中将其关闭。

那么,我如何实现如下图所示的背景进度条

在此处输入图像描述

4

4 回答 4

11

老兄,

如果您真的只想要一个像您发布的图片一样的进度条,您可以简单地将进度条不确定属性设置为 true :)

您可以在代码上或直接在 xml 上进行操作。

在代码中:

yourProgressBar.setIndeterminate(true);

在您的 XML 中,只需将属性 indeterminate 设置为 true。

这是一个简单的布局作为示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:indeterminate="true" />

</RelativeLayout>
于 2013-09-17T15:51:52.507 回答
2

如果您只想显示进度,请查看此代码。这可能会给你一个想法:

public class MainActivity extends Activity implements OnClickListener {

    myTask mytask = new myTask();
    Button button1;
    ProgressBar progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) findViewById(R.id.button1);
        progressbar = (ProgressBar) findViewById(R.id.progressBar);
        button1.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View view) {
        mytask.execute();
    }

    class myTask extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            progressbar.setProgress(0);
            progressbar.setMax(100);
            int progressbarstatus = 0;
        };

        @Override
        protected String doInBackground(String... params) {
            for (int i = 0; i < 20; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                progressbar.incrementProgressBy(10);
            }
            return "completed";
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

        }

        @Override
        protected void onProgressUpdate(String... values) {

            super.onProgressUpdate(values);
        }
    }
}

但是,如果您想显示为对话框,请查看以下代码:

public class YoutubeVideoMain extends Activity {

    ListView videolist;

    ArrayList<String> videoArrayList = new ArrayList<String>();

    ArrayAdapter<String> videoadapter;
    Context context;
    String feedURL = "https://gdata.youtube.com/feeds/api/users/twistedequations/uploads?v=2&alt=jsonc&start-index=1&max-results=5";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.youtubelist);
        videolist = (ListView) findViewById(R.id.videolist);

        videoadapter = new ArrayAdapter<String>(this, R.layout.video_list_item,
                videoArrayList);

        videolist.setAdapter(videoadapter);
        VideoListTask loadertask = new VideoListTask();
        loadertask.execute();

    }

    private class VideoListTask extends AsyncTask<Void, String, Void> {

        ProgressDialog dialogue;

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            dialogue.dismiss();

            videoadapter.notifyDataSetChanged();
        }

        @Override
        protected void onPreExecute() {
            dialogue = new ProgressDialog(context);
            dialogue.setTitle("Loading items..");
            dialogue.show();
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... params) {

            HttpClient client = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet(feedURL);
            try {
                HttpResponse response = client.execute(getRequest);
                StatusLine statusline = response.getStatusLine();
                int statuscode = statusline.getStatusCode();

                if (statuscode != 200) {

                    return null;
                }

                InputStream jsonStream = response.getEntity().getContent();

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(jsonStream));

                StringBuilder builder = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {

                    builder.append(line);

                }
                String jsonData = builder.toString();



                JSONObject json = new JSONObject(jsonData);


                JSONObject data = json.getJSONObject("data");

                JSONArray items = data.getJSONArray("items");

                for (int i = 0; i < items.length(); i++) {


                    JSONObject video = items.getJSONObject(i);



                    videoArrayList.add(video.getString("title"));



                }

                Log.i("YouJsonData:", jsonData);

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

也许这可以帮助你。

于 2013-09-17T15:43:36.327 回答
0

使用相同的 asynctask 使用publishProgress()

于 2013-09-17T15:36:35.197 回答
0
class YourCLassName extends AsyncTask<String, Integer, Void> {

    private ProgressDialog pd = new ProgressDialog(Files.this);

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd.setTitle("your message");

        pd.show();

    }

    @Override
    protected Void doInBackground(String... params) {
            // YOUR PIECE OF CODE

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


            pd.setMessage("Task Completed  .. whatever");


    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }


}
于 2013-09-17T16:16:21.417 回答