重新加载Android旋转刷新按钮

时间:2012-06-19 10:22:07

标签: android android-asynctask refresh

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

到目前为止,我已经得到了什么

@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();

      }
}

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:-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();