0

我有一个需要更改布局的活动。

在第一个布局中,我有四个按钮要显示,而在第二个布局中,我需要一个GridView来显示图像。

我需要显示第二个布局AsyncTask onPostExecute方法中显示第二个布局。

目前,我正在尝试设置两个 setContentView,但出现以下异常: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();   
    }
});
                }
    });
4

6 回答 6

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

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


}



}

这是一种最简单的方法,您必须研究 Fragment 才能更好地进行 UI 管理。您也可以使用取景器。

所以研究 Fragments 和 Viewflipper..

于 2013-05-02T10:40:53.847 回答
2

取而代之的是,您可以拥有一个布局,其中包含四个按钮的包装器和 GridView 的另一个包装器,而最后一个的可见性设置为“已消失”。

AsycTask 完成后,隐藏按钮布局并显示 GridView 布局。

于 2013-05-02T10:11:48.510 回答
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>
于 2013-05-02T09:57:55.703 回答
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.
于 2013-05-02T10:13:42.233 回答
0

朋友将 wrapper1 的 id 更改为 child,如下所示,

<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();                              
                                            }
                                    });
                            }
        });
于 2013-05-02T12:41:13.753 回答
0

AsyncTask 的 onPostExecute 在 UI 线程上运行,因此您无需显式指定 runonUiThread。与其使用 2 个 setcontent 视图,不如在布局文件中有 2 个视图并根据需要使其可见不可见。

于 2013-05-02T09:50:11.793 回答