【问题标题】:progressbar not animated when views are added添加视图时进度条没有动画
【发布时间】:2018-04-23 11:51:21
【问题描述】:

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

我的 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>

当我打开我的活动时,我会在我的表格布局中添加一定数量的 tableRows。之后,我启动一个 AsyncTask 从我的数据库中提取数据并将图片添加到行中。在那段时间里,我想显示一个动画进度条。进度条正确显示,并且在加载表格布局时也会消失,但它不是动画的。有人可以帮我找出问题所在吗?

当没有 tableRows 添加到 tablelayout 或没有 imageviews 添加到 tablerows 时,进度条的动画正确。所以问题一定是添加了imageviews引起的

我的活动代码:

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);
        }
    }
}

【问题讨论】:

    标签: android android-asynctask android-progressbar


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回答,但添加此方法并没有改变任何内容。它仍然没有动画......
    【解决方案2】:

    我能够确定问题所在。我用过

    位图 myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      相关资源
      最近更新 更多