2

嗨,有谁知道如何创建一个刷新按钮,当按下它时它会显示一个加载微调器,然后当它停止时会返回到该按钮?

这是我到目前为止所得到的

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //Check Preferences which sets UI

    checkPreferences();
   PostTask posttask;
   posttask = new PostTask();
   posttask.execute();

   Button backbtn = (Button) findViewById(R.id.backbtn);

    //Listening to button event
    backbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Starting a new Intent
            Intent previousScreen = new Intent(getApplicationContext(), ChooseTeamActivity.class);
            ChosenMethod = "null";
            SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("ChosenMethod", ChosenMethod);            
            editor.commit();
            previousScreen.putExtra("FullData", fulldata);
            startActivity(previousScreen);

        }
    });

    Button refresh = (Button) findViewById(R.id.refresh);

    //Listening to button event
    refresh.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

             PostTask posttask;
               posttask = new PostTask();
               posttask.execute();

        }


    });

这是我希望它加载的功能

public class PostTask extends AsyncTask<Void, String, Boolean> {


    @Override
    protected Boolean doInBackground(Void... params) {
        boolean result = false;


        loadFixtures();
        publishProgress("progress");
        loadResultsFeed();
        publishProgress("progress");
        loadNewsFeed();
        publishProgress("progress");
        return result;
    }

    protected void onProgressUpdate(String... progress) {
        StringBuilder str = new StringBuilder();
            for (int i = 1; i < progress.length; i++) {
                str.append(progress[i] + " ");

            }
    }

        @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        Log.v("BGThread", "begin fillin data");
         FillData();

      }
}
4

2 回答 2

4

在你的 xml 文件中

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_margin="10dip"
        android:visibility="gone" >
    </ProgressBar>
</RelativeLayout>

然后在你的 PostTask onPreExecute 方法中

findViewById(R.id.refresh).setVisibility(View.GONE);
findViewById(R.id.progress).setVisibility(View.VISIBLE);

在你的 OnPostExecute

findViewById(R.id.refresh).setVisibility(View.VISIBLE);
findViewById(R.id.progress).setVisibility(View.GONE);
于 2012-06-19T11:38:18.087 回答
-1

我遇到了同样的问题,所以我为此创建了一个专门的按钮:LoadingProgressButton

包括这样的按钮:

<br.com.simplepass.loading_button_lib.CircularProgressButton
    android:id="@+id/btn_id"
 android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/circular_border_shape"
    app:spinning_bar_width="4dp" <!-- Optional -->
     app:spinning_bar_color="#FFF" <!-- Optional -->
     app:spinning_bar_padding="6dp" <!-- Optional -->

并像这样使用它:

CircularProgressButton btn = (CircularProgressButton) findViewById(R.id.btn_id)
        btn.startAnimation(); 
            [do some async task. When it finishes]

             //You can choose the color and the image after the loading is finished
             btn.doneLoagingAnimation(fillColor, bitmap);
    [or just revert de animation]
 btn.revertAnimation();
于 2016-11-01T20:19:20.973 回答