在android中动态更改视图内容

时间:2012-07-04 16:02:09

标签: android android-view

我是android新手,我对我现在正在做的项目有一些疑问。 我正在开发一个应用程序,在3x3中显示总共9个图像,如下图所示。 pict1

当我按下一个按钮时,图像变为另外9个图像。如果剩余的图像不是9,那么图像将根据下图所示的剩余数量显示。 (例如剩下的是6张图片)

pict2

问题是:

  1. 显示图像的最佳方法是什么?我的想法是创建一个包含9个ImageViews

  2. 的视图
  3. 如果我有2个xml布局,首先是主要布局,第二个是布局包含ImageViews,如何将第二个插入第一个?

  4. 以及如何根据上述说明动态插入图像? question3

  5. 请帮我一些代码。我非常感谢任何帮助。对不起,如果我的英语不太好。

    提前谢谢。


    更新

    我在这种情况下尝试过使用GridView,这是我第一次使用GridView,所以我使用here中的示例并将其实现为我的(我已经尝试了包含那里的示例) ,它的确有效。)

    但是,我已经检查了很多次,没有来自LogCat的错误,没有强制关闭,图像没有显示。我不知道哪里错了。

    这是我的代码:

    choosepic.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="@drawable/bg_inner">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/book_inner"
                android:layout_marginTop="50dp"
            />
    
            <ImageButton
                android:id="@+id/homeBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/home_btn"
                android:background="@null"
            />
    
            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/bg_arrow_btn"
                android:layout_alignParentRight="true"    
            />
    
            <ImageButton
                android:id="@+id/nextBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/right_arrow"
                android:background="@null"
                android:layout_alignParentRight="true"
                android:layout_marginTop="5dp"
                android:layout_marginRight="7dp"
                android:layout_marginLeft="7dp"
            />
    
            <ImageButton
                android:id="@+id/prevBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/left_arrow"
                android:background="@null"
                android:layout_toLeftOf="@+id/nextBtn"
                android:layout_marginTop="5dp"
            />
    
            <GridView
                android:id="@+id/gridView1"
                android:numColumns="3"
                android:gravity="center"
                android:columnWidth="30dp"
                android:stretchMode="columnWidth"
                android:layout_width="300dp"
                android:layout_height="200dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="70dp"
            >
    
            </GridView>
    </RelativeLayout>
    

    animalbutton.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/grid_item_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
        />
    </LinearLayout>
    

    ImageAdapter.java

        public class ImageAdapter extends BaseAdapter{
    
        private Context context;
        private final String[] animalValues;
    
        public ImageAdapter(Context context, String[] animalValues) {
            this.context = context;
            this.animalValues = animalValues;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View gridView;
    
            if (convertView == null) {
    
                gridView = new View(context);
    
                // get layout from mobile.xml
                gridView = inflater.inflate(R.layout.animalbutton, null);
    
                // set image based on selected text
                ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
    
                String animal = animalValues[position];
    
                if (animal.equals("Cat")) {
                    imageView.setImageResource(R.drawable.anim_cat);
                } else if (animal.equals("Cow")) {
                    imageView.setImageResource(R.drawable.anim_cow);
                } else if (animal.equals("Croc")) {
                    imageView.setImageResource(R.drawable.anim_croc);
                } else if(animal.equals("Duck")){
                    imageView.setImageResource(R.drawable.anim_duck);
                } else if(animal.equals("Elephant")){
                    imageView.setImageResource(R.drawable.anim_elephant);
                } else if(animal.equals("Giraffe")){
                    imageView.setImageResource(R.drawable.anim_giraffe);
                } else if(animal.equals("Lion")){
                    imageView.setImageResource(R.drawable.anim_lion);
                } else if(animal.equals("Moose")){
                    imageView.setImageResource(R.drawable.anim_moose);
                } else if(animal.equals("Mouse")){
                    imageView.setImageResource(R.drawable.anim_mouse);
                }else {imageView.setImageResource(R.drawable.ic_launcher);}
    
    
            } else {
                gridView = (View) convertView;
            }
    
            return gridView;
        }
    
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public String getItem(int position) {
            // TODO Auto-generated method stub
            return animalValues[position];
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    }
    

    choosepic.java

        public class choosepic extends Activity {
        /** Called when the activity is first created. */
    
        ImageAdapter mAdapter;
        GridView gridView;
        static final String[] animal = new String[] { 
            "Cat", "Cow","Croc", "Duck", "Elephant", "Giraffe", "Lion", "Moose", "Mouse"};
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.choosepic);
    
            mAdapter = new ImageAdapter(this, animal);
    
            gridView = (GridView) findViewById(R.id.gridView1);
            gridView.setAdapter(mAdapter);
    
            gridView.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                    Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
    
                }
            });
        }
    }
    

    我需要一些帮助。提前谢谢你!

1 个答案:

答案 0 :(得分:1)

我找到了答案。

  1. 我正在使用GridView来显示图像。只需更改适配器即可更改内容

  2. 这个问题无需回答。

  3. 与答案号相同。 1.代码就像我之前发布的choosepic.xml。

  4. 以及我的`GridView未显示图像的问题,ImageAdapter中的coz getCount()返回0,因此GridView中不显示任何图像。 我用return 0;

    更改了return animalValues.size()
相关问题