在同一个Activity中切换两个GridView

时间:2011-04-25 17:53:47

标签: android layout grid

我一直在寻找如何解决几个问题并得到一些问题的答案,但有一件事仍在建设中,如果没有人能帮助我,将无法完成。 :/

我一直在尝试放大和缩小GridView,但又转到了另一个解决方案,因为我只需要两个状态:概述和详细视图。因此我制作了两个Gridview。第一个是两个网格视图内的图像缩小并显示而不滚动的图像。另一个是图像以原始大小显示的图像。您可以在该内部水平和垂直滚动。

我的问题是在这两个网格视图之间切换。如果我点击其中一个,我试图将两者的“可见性”设置为“已消失”或“可见”。

这是我的代码:

简化版:

package test.scroll;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.view.View;
import android.view.View.OnClickListener;

public class TestScrollActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    final GridView grd_overview = (GridView)this.findViewById(R.id.grd_overview);
    grd_overview.setAdapter(new OverviewImageAdapter(this));
    final GridView grd_detailed = (GridView)this.findViewById(R.id.grd_detailed);
    grd_detailed.setVisibility(2);
    grd_detailed.setAdapter(new DetailedImageAdapter(this));

    grd_overview.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            grd_overview.setVisibility(2);
            grd_detailed.setVisibility(0);
        }
    });

    grd_detailed.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            grd_detailed.setVisibility(2);
            grd_overview.setVisibility(0);
        }
    });
    }
}

OverviewAdapter:

package test.scroll;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class OverviewImageAdapter extends BaseAdapter {  
     private Context mContext;

        public OverviewImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(82, 82));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.memory_1, R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
                R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
                R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
                R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
                R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
                R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1
        };

    }  

DetailedAdapter:

package test.scroll;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class DetailedImageAdapter extends BaseAdapter {  
     private Context mContext;

        public DetailedImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(82, 82));
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.memory_2, R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
                R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
                R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
                R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
                R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
                R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2
        };

    }  

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/app_layout" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        >

        <!-- PLAYGROUND -->
        <test.scroll.TwoDScrollView 
                android:id="@+id/scene_scroller" 
                android:drawingCacheQuality="low" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                >
                <LinearLayout
                        android:id="@+id/grds"
                        android:drawingCacheQuality="low"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        >
                        <GridView
                                android:id="@+id/grd_overview"
                                android:drawingCacheQuality="low"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                />
                        <GridView
                                android:id="@+id/grd_detailed"
                                android:drawingCacheQuality="low"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                />

                </LinearLayout>
        </test.scroll.TwoDScrollView>

        <!-- ATTRIBUTES -->
        <Button
                android:id="@+id/btn_cancel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/scene_scroller"
                android:text="cancel"
                />    

</RelativeLayout>

对于如何在这两个gridview之间切换,您有什么建议吗?让我知道:))

巴斯蒂

3 个答案:

答案 0 :(得分:1)

为什么不将两个GridView放在ViewSwitcher / ViewFlipper中,只是在它们之间翻转?

答案 1 :(得分:1)

Imho你真的可以很好地实现ViewFlipper来切换你的2个网格!

来自android 参考指南

  

ViewFlipper是一个简单的ViewAnimator,可以进行动画处理   两个或多个视图之间有   已添加到它。只有一个孩子   一次显示。如有要求,可以   在每个孩子之间自动翻转   定期。

以下是关于如何实施此问题的an example 以下是another example演示此内容(带动画)

答案 2 :(得分:0)

您应该能够从app_layout中删除您不想看到的网格,并添加您想要查看的网格。例如:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.app_layout);
GridView gv = (GridView) findViewById(R.id.grid01); // Replace the ID
GridView gv2 = (GridView) findViewById(R.id.grid02); // Replace too

//...
rl.removeView(gv);
rl.addView(gv2);

// ...
rl.removeView(gv2);
rl.addView(gv);

等等。