添加视图时,进度条不动画

时间:2017-11-10 09:32:38

标签: android android-asynctask android-progressbar

当我将动态图像视图添加到我的桌面时,我的进度条没有动画

我的xml如下所示:

    <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:orientation="horizontal"
tools:context=".activities.xyActivity">

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:indeterminate="true" />

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/progressBar">

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">    
    </TableLayout>
</ScrollView>

当我打开我的Activity时,我向tablelayout添加了一定数量的tableRows。之后,我启动AsyncTask从我的数据库中提取数据并将图片添加到行中。在那段时间里,我想展示一个动画进度条。进度条显示正确,并且在加载tablelayout时也会消失,但不会设置动画。有人可以帮我弄清问题是什么吗?

当没有tableRows添加到tablelayout或没有将imageview添加到tablerows时,Progressbar会正确设置动画。所以问题必须由添加图像视图

引起

我的活动代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shelf);

    Intent clickedShelf = getIntent();
    shelf_id = clickedShelf.getIntExtra("shelfId", 0);
    noofBoards = clickedShelf.getExtras().getInt("noofBoards");

    tableLayout = (TableLayout) findViewById(R.id.tableLayout);
    scrollView = (ScrollView) findViewById(R.id.scrollView);
    pb = (ProgressBar) findViewById(R.id.progressBar);

    for (int i = 0; i < noofBoards; i++) {
        tableRow = new TableRow(this);
        tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        tableRow.setId(i+1);
        tableRow.setBackgroundResource(R.mipmap.shelfboard);
        tableRow.setPadding(20, 0, 20, 0);
        tableRow.setOnClickListener(this);

        tableLayout.addView(tableRow);

    }
    if (noofBoards!=0) {
       new ArticleDataProductAsync(pb).execute("someParams");
   }
   else {
       TextView v = new TextView(thisvariable);
       v.setTypeface(Typeface.DEFAULT_BOLD);
       v.setText("no data saved yet");
       tableLayout.addView(v);
   }
}    


    public class ArticleDataProductAsync extends AsyncTask<String, Integer, String> {
     ProgressBar pb;
     public ArticleDataProductAsync (ProgressBar pb) {
        this.pb=pb;
    }

    @Override
    protected String doInBackground(String... params) {
        //database connection is started
    }

    @Override
    protected void onPostExecute(String result) {
        //result is converted to ArrayList

        if (adpList.isEmpty()) {
            TextView v = new TextView(thisvariable);
            v.setTypeface(Typeface.DEFAULT_BOLD);
            v.setText("no products saved yet");
            tableLayout.addView(v);
        }

        else {

        for (int i = 0; i < adpList.size(); i++) {
            //find corresponding tablerow and add new imageview for each object and set imagebitmap to it

               imageView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        //start new Intent
                    }
                });
            }
        }
            pb.setVisibility(View.GONE);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用onPreExecute方法在AsyncTask上显示ProgressBar,或在ArticleDataProductAsync类上添加此方法,如下所示。

    @Override
    protected void onPreExecute() {
        pb.setVisibility(View.VISIBLE);
    }

答案 1 :(得分:0)

我能够找出问题所在。我用了

  

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

在我将位图设置为ImageView之前。这阻止了我的ImageView

我在这里找到了答案:https://stuff.mit.edu/afs/sipb/project/android/docs/training/displaying-bitmaps/process-bitmap.html