2013-05-02 78 views
0

我有一個活動,我需要改變佈局。如何在一項活動中顯示不同的佈局?

在第一個佈局中,我有四個按鈕可以顯示,第二個佈局需要GridView來顯示圖像。

我需要在AsyncTask onPostExecute方法中顯示第二個佈局。

現在,我想設置兩個setContentViews,但我得到以下異常:ClassCastException

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_focusarea); 
           videoBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      new LoadFiles().execute(); 
     } 
    }); 
    animateBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      new LoadFiles().execute();   
     } 
    }); 
    pdfBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      new LoadPDFFiles().execute();   
     } 
    }); 
} 

在我postExecute我嘗試這樣

protected void onPostExecute(String file_url) { 
     pDialog.dismiss();  
     runOnUiThread(new Runnable() { 
    public void run() { 
     setContentView(R.layout.gallery); 
      girGridView=(GridView) findViewById(R.id.gridView1_bir); 
      girGridView.setAdapter(new ImageAdapter(this));  
       girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
      public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) { 
    Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show(); 
    } 
}); 
       } 
    }); 
+0

請發佈您的logcat錯誤。 – Deepak 2013-05-02 09:48:22

回答

2

取而代之的是你可以同時包含了你的四個按鈕的包裝等爲GridView,而這最後一個是能見度設置爲「水漲船高」的佈局。

當AsycTask完成後,您將隱藏按鈕佈局並顯示GridView佈局。

+0

如何使用代碼???請幫助我?即時新 – 2013-05-02 10:20:14

+0

@OneManArmy很簡單。在你的xml中,你在你所有的按鈕周圍放置了一個包裝器佈局(比如linearlayout,relativelayout),並且在你的gridview中放置了另一個包裝器佈局) - 最後一個佈局具有可見性:消失了。此外,你需要你的兩個包裝佈局的findviewbyid,並在asynctaskfinished時,你把wrapper1.setvisibility(view.gone)和wrapper2.setvisibility(view.visible) – dwbrito 2013-05-02 10:24:25

+0

PLZ你可以發佈一些代碼爲xml..plz給例子..幫助我 – 2013-05-02 10:26:22

0

onPostExecute的AsyncTask在UI線程上運行,所以你不需要指定runonUiThread。而不是使用2 setcontent查看最好在佈局文件中有2個視圖,並根據需要使其不可見。

+0

該怎麼做? – 2013-05-02 09:57:27

1

爲什麼不用兩個佈局或兩個片段設置一個內容視圖?佈局可以是這樣的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/text1">   
    </Button> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/text2">   
    </Button> 
    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/text3">   
    </Button> 
    <Button 
     android:id="@+id/button4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/text4">   
    </Button> 
    <GridView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ... 
    </GridView> 
</LinearLayout> 
+0

如何設置兩個佈局??我不知道要做那個..我剛接觸android ..請幫我設置.. – 2013-05-02 09:59:48

+0

只能有一個父佈局,但它可以包含儘可能多的子佈局你要。 – Neoh 2013-05-04 16:22:06

1
Use one setContentView() and define separates Linear/Relative layout one for buttons and second for gridView.And hide/show the Views according to your need. 
+0

如何編寫隱藏一個佈局並顯示另一個佈局? – 2013-05-02 10:21:52

+0

使用setVisbility(View.GONE)和setVisibility(View.VISIBLE)到你從XML佈局文件中找到的視圖。 – 2013-05-02 10:25:47

3

而不是使用兩個佈局的使用單個佈局波紋管

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
    android:id="@+id/MyLayoutOne" 
    android:layout_width="fill_parent" 
    android:visibility="gone" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Hi This is my first layout" /> 


    <!-- Your first layout contents add here--> 



</LinearLayout> 

<LinearLayout 
    android:id="@+id/MyLayoutTwo" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Hi This is my Second layout" /> 


    <!-- Your second layout contents add here --> 


</LinearLayout> 

</LinearLayout> 

內MyLayoutOne和第二版面內容添加您的第一個版面的內容裏面MyLayoutTwo

您的活動中

而使用如下代碼,

public class MainActivity extends Activity { 


LinearLayout MyLayoutOne; 
LinearLayout MyLayoutTwo; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    MyLayoutOne=(LinearLayout)findViewById(R.id.MyLayoutOne); 
    MyLayoutTwo=(LinearLayout)findViewById(R.id.MyLayoutTwo); 


    // this will make first layout visible 
    MyLayoutOne.setVisibility(View.VISIBLE); 


    // this will make second layout hidden from your layout 
    MyLayoutTwo.setVisibility(View.GONE); 



    //========================================= 

    //in your post create add this codes 

    //========================================= 

    // this will make first layout hidden 
    MyLayoutOne.setVisibility(View.GONE); 

    // this will make second layout visible in your layout 
    MyLayoutTwo.setVisibility(View.VISIBLE); 

    //========================================= 


} 



} 

這是一個最簡單的方法,你必須研究碎片爲更好的UI管理。你也可以使用viewflipper。

所以研究片段和Viewflipper ..

+0

PLZ檢查這個[http://pastebin.com/Dcm8Y6Rf] – 2013-05-02 11:25:37

+0

即時通訊錯誤??? – 2013-05-02 11:26:17

+0

[http://pastebin.com/JwSUeVPZ]check這個即時通訊錯誤.. – 2013-05-02 12:16:29

0

朋友改變wrapper1爲以下孩子的id,

<LinearLayout 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" 
tools:context=".Focusarea" > 

<LinearLayout 
    android:id="@+id/wrapper1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:visibility="gone" > 

    <GridView 
     android:id="@+id/gridView1_bir" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" > 
    </GridView> 
</LinearLayout> 

<RelativeLayout 
    android:id="@+id/wrapper2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/vid_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="18dp" 
     android:src="@drawable/ic_launcher" /> 


</RelativeLayout> 

</LinearLayout> 

和下面的OnCreate外初始化線性佈局,

LinearLayout wrapper1; 
RelativeLayout wrapper2; 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    wrapper1 = (LinearLayout)findViewById(R.id.wrapper1); 
    wrapper2=(RelativeLayout)findViewById(R.id.wrapper2); 


    // this will make first layout visible 
    wrapper2.setVisibility(View.VISIBLE); 


    // this will make second layout hidden from your layout 
    wrapper1.setVisibility(View.GONE); 


    ImageView videoBtn = (ImageView) findViewById(R.id.vid_btn); 
    ImageView animateBtn = (ImageView) findViewById(R.id.anit_btn); 
    ImageView pdfBtn = (ImageView) findViewById(R.id.pdf_btn); 

    videoBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       new LoadFiles().execute();       
      } 
    }); 
    animateBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
        new LoadFiles().execute();      
      } 
    }); 
    pdfBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
        new LoadPDFFiles().execute();     
      } 
    }); 
    } 
    protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting VIDEOS 
      pDialog.dismiss();  
      vid=new ArrayList<String>(new ArrayList<String>(vid)); 
      videoUrl=parsing.parse(videoUrl); 
      System.out.println("VIDEO URL" +videoUrl); 
        runOnUiThread(new Runnable() { 
          public void run() { 


          //--here you wont need to initialize again-- 

           // this will make first layout visible 
           wrapper1.setVisibility(View.VISIBLE); 
           // this will make second layout hidden from your layout 
           wrapper2.setVisibility(View.GONE); 

          girGridView=(GridView) findViewById(R.id.gridView1_bir); 
          //ListView gibi buna da adapter set ediliyor. 
          girGridView.setAdapter(new ImageAdapter(this));        
          girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) { 
    Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();        
              } 
            }); 
          } 
     }); 
相關問題