2012-12-04 8 views
5

मेरे पास EditText, Button और ListView के साथ एक दृश्य है। बटन onClick() कुछ साइट को पार करता है (यह हिस्सा ठीक काम करता है), लेकिन ListView में पार्स की गई जानकारी को प्रदर्शित करने में कुछ समय लगता है, इसलिए मैं जगह पर छोटे ProgressBar प्रदर्शित करना चाहता हूं, जहां ListView थोड़ी देर के बाद होना चाहिए।कुछ काम करते समय एक दृश्य पर प्रोग्रेसबार कैसे प्रदर्शित करें?

तो, मैं अपने लेआउट में जोड़ने (मैं यहाँ महत्वपूर्ण हिस्सा लिखने):

<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"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1" 
     android:weightSum="1.0"> 

     <EditText 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight=".86" /> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0px" 
      android:layout_weight=".14" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_below="@id/layout1" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     android:id="@+id/progressbar" > 

     <ProgressBar 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      style="@android:style/Widget.ProgressBar.Small" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/layout1"> 

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

    </RelativeLayout> 

</RelativeLayout> 

स्रोत कोड में:

progressBar = (RelativeLayout) findViewById(R.id.progressbar); 

और हर मैं ProgressBar दिखाना चाहते हैं, मैं यह कर :

progressBar.setVisibility(View.VISIBLE); 
अक्षम करने के लिए

:

progressBar.setVisibility(View.INVISIBLE); 

लेकिन यह काम नहीं करता है।

मैं इसे कैसे काम कर सकता हूं?

+0

* लेकिन यह काम नहीं करता है। * - यह बहुत उपयोगी नहीं है। बेहतर समझाने की देखभाल क्या समस्या है? – Luksprog

+0

मैं किसी भी 'प्रोग्रेसबार' को प्रदर्शित नहीं कर सकता। यह 'प्रोग्रेसबार' ट्राइफल्स को जोड़ने के बिना काम करता है। – azizbekian

+2

लुक्सप्रोग के साथ सहमत हैं, लेकिन मैं यह अनुमान लगाने के इच्छुक हूं कि आपका पार्सिंग अवरुद्ध हो रहा है (इसलिए सेटविसेबिलिटी कोड पोस्ट-पार्स तक निष्पादित नहीं हो रहे हैं)। इसे हल करने के कई तरीके हैं लेकिन मैं प्रोग्रेसबार और एसिन्टास्क के लिए खोज करने की सिफारिश करता हूं (इसे पूरा करने के अन्य तरीके भी हैं, लेकिन मूल रूप से आपको एक गैर-अवरुद्ध प्रदर्शन प्रोग्रेसबार विधि को उसी समय निकालना होगा जब आप अपना पार्सिंग, फिर प्रदर्शन प्रोग्रेसबार में लौटने से पहले पार्स प्रक्रिया को पूरा करने के लिए जांचें)। – logray

उत्तर

2

अपने ProgressBar की दृश्यता में हेरफेर करने की आवश्यकता नहीं है। ListView में "खाली दृश्य" नामक एक सुविधा है जो केवल तभी दिखाई जाती है जब आपका ListView खाली हो। अपने ProgressBar के लिए अपने ListView और android:id="@android:id/empty" के लिए android:id="@android:id/list" उपयोग करते हैं, तो आपकी गतिविधि ListActivity फैली

  1. हैं: यहाँ आप इसे उपयोग करने के लिए क्या करने की जरूरत है।
  2. यदि आप ListActivity का विस्तार नहीं करते हैं, तो setEmptyView() आपके ListView की विधि है, जो आपको वही काम करने देता है।

उपरोक्त विधि सबसे अच्छा फिट बैठती है यदि आपको ListView खाली करने की आवश्यकता नहीं है (उदाहरण के लिए डेटा केवल स्टार्टअप पर लोड हो जाता है)। यदि आपको उसके बाद डेटा सेट करने की आवश्यकता है, तो ViewSwitcher का उपयोग करना बेहतर है, जिसका भी बहुत फायदा है - यह आपको संक्रमण एनीमेशन लागू करने देता है। ViewSwitcher पर विवरण के लिए, this पर एक नज़र डालें।

0

उपरोक्त वर्णित एसिंक कार्य का उपयोग करने का प्रयास करें।

/** 
* this class performs all the work, shows dialog before the work and dismiss it after 
*/ 
public class ProgressTask extends AsyncTask<String, Void, Boolean> { 

public ProgressTask(ListActivity activity) { 
    this.activity = activity; 
    dialog = new ProgressDialog(context); 
} 

/** progress dialog to show user that the backup is processing. */ 
private ProgressDialog dialog; 
/** application context. */ 
private ListActivity activity; 

protected void onPreExecute() { 
    this.dialog.setMessage("Progress start"); 
    this.dialog.show(); 
} 

    @Override 
protected void onPostExecute(final Boolean success) { 
    if (dialog.isShowing()) { 
     dialog.dismiss(); 
    } 


    //do whatever with your data 
} 

protected Boolean doInBackground(final String... args) { 
    try{  
     //parsing of your website here... 

     return true; 
    } catch (Exception e) 
     Log.e("tag", "error", e); 
     return false; 
    } 
    } 
} 
+0

मोडल संवाद का उपयोग करना एक नुकसान है - वे पूरी विंडो को अवरुद्ध करते हैं। लोड होने के दौरान उपयोगकर्ता एप्लिकेशन के साथ बातचीत जारी रखना चाह सकता है। –

1

RelativeLayoutListView के साथ अपने ProgressBar .Modify इस तरह लेआउट छुपा और देखें कि यह कैसे जाता है:

<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"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1" 
     android:weightSum="1.0"> 

     <EditText 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight=".86" /> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0px" 
      android:layout_weight=".14" /> 

    </LinearLayout> 

    <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_below="@id/layout1"/> 

    <RelativeLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     android:id="@+id/progressbar" > 

     <ProgressBar 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      style="@android:style/Widget.ProgressBar.Small" /> 

    </RelativeLayout> 


</RelativeLayout> 

इसके अलावा एक नज़र at one of my answers, में कुछ ऐसी ही समस्या के लिए की है।

1

आपको में अपने को घोंसला करने की आवश्यकता नहीं है। बस इसे अपने मुख्य लेआउट में केंद्रित करें और इसे अंतिम रूप में रखें ताकि यह शीर्ष पर बने रहे।

<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"> 

    // the rest 

    <ProgressBar 
     android:id="@+id/progressBar" 
     android:layout_centerInParent="true" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

</RelativeLayout> 
0

उन लोगों के लिए, जो उत्तर में रुचि रखते हैं।

मुख्य टैग केवल लिखे गए हैं।

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1" 
     android:weightSum="1.0" > 

     <EditText 
      android:layout_width="0px" 
      android:layout_height="wrap_content" 
      android:layout_weight=".86" /> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0px" 
      android:layout_weight=".14" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/layout1" > 

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/layout1" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/layout1" 
     android:gravity="center" 
     android:visibility="gone" 
     android:background="#DDE7E7E8" 
     android:id="@+id/pBar" > 

     <ProgressBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@android:style/Widget.ProgressBar.Small" /> 

    </LinearLayout> 

</RelativeLayout> 

और AsyncTask:

private class Parsing extends AsyncTask<Void, Void, Void> 
{ 
    private LinearLayout pBar; 

    private Parsing() 
    { 
     pBar = (LinearLayout) findViewById(R.id.pBar); 
    } 

    @Override 
    protected Void doInBackground(Void... params) 
    { 
     parsingHere(); 
     return null; 
    } 

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

    @Override 
    protected void onPostExecute(Void result) 
    { 
     super.onPostExecute(result); 
     pBar.setVisibility(View.INVISIBLE); 
    } 

}