【问题标题】:How to have separate Progress Bars for separate TextViews如何为单独的 TextView 设置单独的进度条
【发布时间】:2017-02-02 09:56:59
【问题描述】:

我想为我拥有的个人TextView 提供单独的进度条。例如,每个TextView 都会显示一些我用来从数据库中查询的信息。我可以为此使用AsyncTask,并且在postExecute 中我知道我可以显示我想要显示的TextView 信息。但是,问题是,当页面加载以从我的数据库中获取不同的信息时,我进行了多次异步调用,并且我想为每个显示信息的TextView 显示一个ProgressBar。在我的数据库中提供 TextView 信息之前,有什么方法可以显示 ProgressBar?我只是想知道 UI 方面,因为我已经知道如何进行数据库调用。

我知道我可以用我的AsyncTask 做到这一点:

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

        //showDialog() here;
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        //do what you want here//
    }

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

        //dismissDialog() here;//


    }

但是,如果我这样做 showDialog(),我知道我必须在我的 XML 中创建 4 个不同的 ProgressBars,但是我将如何确保它们只会出现在我的 TextView 所在的位置而不是在整个屏幕上?

【问题讨论】:

  • textview 旁边的进度条是否符合您的项目要求?
  • @user1506104 我希望显示进度条,直到从数据库中获取信息。然后我想设置进度条的可见性消失并显示TextView当信息准备好显示时进度条所在的位置

标签: android android-asynctask android-progressbar


【解决方案1】:

您可以在每个 textview 旁边显示一个进度条,以显示该字段仍在加载数据:

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal">

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" />

   <ProgressBar
       android:id="@+id/progressbar"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:indeterminate="true" />
</LinearLayout>

一旦为一个文本视图准备好数据,就可以在对象视图上调用其对应的setVisibility(View.GONE)。您应该通过onPostExecute() 调用它。

【讨论】:

    【解决方案2】:

    您的布局文件 (activity_main.xml) 可能如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    
    <LinearLayout
        android:layout_marginTop="50dp"
        android:background="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="18dp"
            android:textColor="@android:color/white"
            android:text="contents of textview 1" />
    
        <ProgressBar
            android:id="@+id/progressbar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    
    <LinearLayout
        android:background="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="18dp"
            android:textColor="@android:color/white"
            android:text="contents of textview 2" />
    
        <ProgressBar
            android:id="@+id/progressbar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    
    </LinearLayout>
    

    然后在您的活动中,声明以下内容:

    ProgressBar progressBar1,progressBar2;
    

    在活动的OnCreate()方法中,

    progressBar1=(ProgressBar)findViewById(R.id.progressbar1);
    progressBar2=(ProgressBar)findViewById(R.id.progressbar2);
    

    最后,在您的 onPostExecute() 方法中,隐藏相应的进度条:

    progressBar1.setVisibility(View.GONE);
    

    或/和,

    progressBar2.setVisibility(View.GONE);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2015-02-10
      • 1970-01-01
      • 2015-06-05
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多