2012-12-04 3 views
5

EditText, ButtonListView과 함께보기가 있습니다. 단추의 onClick()은 일부 사이트를 구문 분석하지만 (이 부분은 정상적으로 작동합니다), 구문 분석 된 정보를 ListView에 표시하는 데 시간이 걸리므로 잠시 후 ListView이 발생하는 위치에 작은 ProgressBar을 표시하고 싶습니다. 내가 이렇게몇 가지 작업을하는 동안보기에 ProgressBar를 표시하는 방법?

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

을 그리고 매번 내가 ProgressBar을 보여주고 싶은 : 소스 코드에서

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

가 :

그래서, 내 레이아웃이 추가 (I 여기에서 중요한 부분을 쓰기) : 사용하지 않도록

progressBar.setVisibility(View.VISIBLE); 

:

progressBar.setVisibility(View.INVISIBLE); 

하지만 이것은 작동하지 않습니다.

어떻게 해결할 수 있습니까?

+0

*하지만 작동하지 않습니다. * -별로 도움이되지 않습니다. 문제가 무엇인지 더 자세히 설명하는 치료? – Luksprog

+0

'ProgressBar'가 표시되지 않습니다. 'ProgressBar' 사소한 일을 추가하지 않고 작업 한 것처럼 작동합니다. – azizbekian

+2

Luksprog와 동의하지만, 나는 당신의 파싱이 블로킹 된 것 같아서 기꺼이 할 것입니다 (그래서 코드는 파싱까지 실행되지 않습니다). 이 문제를 해결할 수있는 방법이 많이 있지만 progressbar 및 asynctask를 검색하는 것이 좋습니다 (이 작업을 수행하는 다른 방법이 있지만 기본적으로 작업을 수행하는 동시에 비 차단 displayprogressbar 메서드를 시작해야합니다. 구문 분석, displayprogressbar에서 반환하기 전에 구문 분석 프로세스 완료 확인). – logray

답변

2

당신의 ProgressBar의 가시성을 조작 할 필요가 없습니다. ListView에는 ListView이 비어있는 경우에만 표시되는 "빈보기"라는 기능이 있습니다. 당신의 활동이 ListActivity를 확장

  1. 경우, ProgressBar 대한 ListViewandroid:id="@android:id/empty"에 대한 android:id="@android:id/list"를 사용 : 여기 당신이 그것을 사용하기 위해 수행해야 할 것입니다.
  2. ListActivity을 확장하지 않으면 ListViewsetEmptyView() 방법이 있으므로 동일한 작업을 수행 할 수 있습니다.

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

ListView 가진 RelativeLayout는 다음과 같이 배치하여 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

ProgressBar을 다른 RelativeLayout에 중첩하지 않아도됩니다. 메인 레이아웃에 가운데 놓고 마침내 맨 위에 놓습니다.

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

} 
관련 문제