how to show progress bar(circle) in an activity having a listview before loading the listview with data

后端 未结 12 2306
忘掉有多难
忘掉有多难 2020-11-28 00:18

I have a ListView in my second activity.OnItemClick of it I called a webservice and trying to fetch data. And after that I am moving to third activ

相关标签:
12条回答
  • 2020-11-28 01:09

    I used this one for list view loading may helpful.

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >
    
    <LinearLayout
        android:id="@+id/progressbar_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
    
            <ProgressBar
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Loading data..." />
        </LinearLayout>
    
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#C0C0C0" />
    </LinearLayout>
    
    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="1dip"
        android:visibility="gone" />
    
    </RelativeLayout>
    

    and my MainActivity class is,

    public class MainActivity extends Activity {
    ListView listView;
    LinearLayout layout;
    List<String> stringValues;
    ArrayAdapter<String> adapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        layout = (LinearLayout) findViewById(R.id.progressbar_view);
    
        stringValues = new ArrayList<String>();
    
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, stringValues);
    
        listView.setAdapter(adapter);
        new Task().execute();
    }
    
    class Task extends AsyncTask<String, Integer, Boolean> {
        @Override
        protected void onPreExecute() {
            layout.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
            super.onPreExecute();
        }
    
        @Override
        protected void onPostExecute(Boolean result) {
            layout.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
            super.onPostExecute(result);
        }
    
        @Override
        protected Boolean doInBackground(String... params) {
            stringValues.add("String 1");
            stringValues.add("String 2");
            stringValues.add("String 3");
            stringValues.add("String 4");
            stringValues.add("String 5");
    
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    }
    

    this activity display progress for 3sec then it will display listview, instead of adding data statically to stringValues list you can get data from server in doInBackground() and display it.

    0 讨论(0)
  • 2020-11-28 01:14

    Are you extending ListActivity?

    If so, put a circular progress dialog with the following line in your xml

    <ProgressBar
    android:id="@android:id/empty"
    ...other stuff...
    />
    

    Now, the progress indicator will show up till you have all your listview information, and set the Adapter. At which point, it will go back to the listview, and the progress bar will go away.

    0 讨论(0)
  • 2020-11-28 01:15

    There are several methods of showing a progress bar (circle) while loading an activity. In your case, one with a ListView in it.

    IN ACTIONBAR

    If you are using an ActionBar, you can call the ProgressBar like this (this could go in your onCreate()

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
    setProgressBarIndeterminateVisibility(true);
    

    And after you are done displaying the list, to hide it.

    setProgressBarIndeterminateVisibility(false);
    

    IN THE LAYOUT (The XML)

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    
        <LinearLayout
            android:id="@+id/linlaHeaderProgress"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="gone" >
    
            <ProgressBar
                android:id="@+id/pbHeaderProgress"
                style="@style/Spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </ProgressBar>
        </LinearLayout>
    
        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:cacheColorHint="@android:color/transparent"
            android:divider="#00000000"
            android:dividerHeight="0dp"
            android:fadingEdge="none"
            android:persistentDrawingCache="scrolling"
            android:smoothScrollbar="false" >
        </ListView>
    </LinearLayout>
    

    And in your activity (Java) I use an AsyncTask to fetch data for my lists. SO, in the AsyncTask's onPreExecute() I use something like this:

    // CAST THE LINEARLAYOUT HOLDING THE MAIN PROGRESS (SPINNER)
    LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
    
    @Override
    protected void onPreExecute() {    
        // SHOW THE SPINNER WHILE LOADING FEEDS
        linlaHeaderProgress.setVisibility(View.VISIBLE);
    }
    

    and in the onPostExecute(), after setting the adapter to the ListView:

    @Override
    protected void onPostExecute(Void result) {     
        // SET THE ADAPTER TO THE LISTVIEW
        lv.setAdapter(adapter);
    
        // CHANGE THE LOADINGMORE STATUS TO PERMIT FETCHING MORE DATA
        loadingMore = false;
    
        // HIDE THE SPINNER AFTER LOADING FEEDS
        linlaHeaderProgress.setVisibility(View.GONE);
    }
    

    EDIT: This is how it looks in my app while loading one of several ListViews

    enter image description here

    0 讨论(0)
  • 2020-11-28 01:15

    You can do this easier.
    Source: http://www.tutorialspoint.com/android/android_loading_spinner.htm
    It helped me.

    Layout:

    <ProgressBar
       android:id="@+id/progressBar1"
       style="?android:attr/progressBarStyleLarge"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true" />
    

    After defining it in xml, you have to get its reference in java file through ProgressBar class. Its syntax is given below:

    private ProgressBar spinner;
    spinner = (ProgressBar)findViewById(R.id.progressBar1);
    

    After that you can make its disappear , and bring it back when needed through setVisibility Method. Its syntax is given below:

    spinner.setVisibility(View.GONE);
    spinner.setVisibility(View.VISIBLE);
    
    0 讨论(0)
  • 2020-11-28 01:18
    ProgressDialog dialog = ProgressDialog.show(Example.this, "", "Loading...", true);
    
    0 讨论(0)
  • 2020-11-28 01:18

    Create an xml file any name (say progressBar.xml) in drawable and add <color name="silverGrey">#C0C0C0</color> in color.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720" >
    
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="6"
            android:useLevel="false" >
            <size
                android:height="200dip"
                android:width="300dip" />
    
            <gradient
                android:angle="0"
                android:endColor="@color/silverGrey"
                android:startColor="@android:color/transparent"
                android:type="sweep"
                android:useLevel="false" />
    
        </shape>
    </rotate>
    

    Now in your xml file where you have your listView add this code:

     <ListView
                android:id="@+id/list_form_statusMain"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </ListView>
    
            <ProgressBar
                android:id="@+id/progressBar"
                style="@style/CustomAlertDialogStyle"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:layout_gravity="center_horizontal"
                android:indeterminate="true"
                android:indeterminateDrawable="@drawable/circularprogress"
                android:visibility="gone"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>
    

    In AsyncTask in the method:

    @Override
    protected void onPreExecute()
    {
        progressbar_view.setVisibility(View.VISIBLE);
    
        // your code
    }
    

    And in onPostExecute:

    @Override
    protected void onPostExecute(String s)
    {
        progressbar_view.setVisibility(View.GONE);
    
        //your code
    }
    
    0 讨论(0)
提交回复
热议问题