在按钮单击时填充android活动中的不同gridview元素

时间:2014-03-06 18:28:38

标签: android gridview

在我的Android应用程序中,如何在同一GridView on button click事件中显示不同的图标。我必须在按钮点击时继续在gridview中的两组图标之间进行交换。我所做的是I have made two activities each with a button and different set of icons in a GridView并继续在点击按钮时切换这些活动。但有没有更好的方法,在相同的活动,我可以更改按钮点击的gridview元素?感谢。

被修改 我通过以下代码使用ViewSwitcher以某种方式执行此操作:

JavaActivity代码:

public class TestActivity extends Activity {

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

        GridView EngGrid,UrduGrid;
        final ViewSwitcher switcher;
        Button Next, Previous;



            EngGrid=(GridView) findViewById(R.id.gridView1);
            EngGrid.setAdapter(new EngAdapter(this));
            EngGrid.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int position,
                            long id) {
                    // TODO Auto-generated method stub
                    // Toast.makeText(MenuActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                     Intent i = new Intent(getApplicationContext(), DisplayActivity.class);
                     i.putExtra("menu_id", position);
                    startActivity(i);

                }
        });


            UrduGrid=(GridView) findViewById(R.id.gridView2);
            UrduGrid.setAdapter(new UrduAdapter(this));
            UrduGrid.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int position,
                            long id) {
                    // TODO Auto-generated method stub
                    // Toast.makeText(MenuActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                     Intent i = new Intent(getApplicationContext(), DisplayActivity.class);
                     i.putExtra("menu_id", position);
                    startActivity(i);

                }
        });

             switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher);
             Next = (Button) findViewById(R.id.button2);
                Previous = (Button) findViewById(R.id.button1);

                Next.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    new AnimationUtils();
                    switcher.setAnimation(AnimationUtils.makeInAnimation
                        (getBaseContext(), true));
                    switcher.showNext();
                }
            });

                Previous.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    new AnimationUtils();
                    switcher.setAnimation(AnimationUtils.makeInAnimation
                        (getBaseContext(), true));
                    switcher.showPrevious();
                }
            });
            }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_test, menu);
        return true;
    }


public class UrduAdapter extends BaseAdapter{

        public Integer[] mThumbIds = {
                  R.drawable.urdu_dua1, R.drawable.urdu_dua2,
                    R.drawable.urdu_dua3, R.drawable.urdu_dua4,
                    R.drawable.urdu_dua5, R.drawable.urdu_dua6,
                    R.drawable.urdu_dua7, R.drawable.urdu_dua8,
                    R.drawable.urdu_dua9, R.drawable.urdu_dua10,
                    R.drawable.urdu_dua11, R.drawable.urdu_dua12,
                    R.drawable.urdu_dua13, R.drawable.urdu_dua14,
                    R.drawable.urdu_dua15, R.drawable.urdu_dua16,
                    R.drawable.urdu_dua17, R.drawable.urdu_dua18,
                    R.drawable.urdu_dua19, R.drawable.urdu_dua20,
                    R.drawable.urdu_dua21

        };

        private Context mContext;

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

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return mThumbIds[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View MyView;
            if(convertView == null){

                LayoutInflater li=((Activity) mContext).getLayoutInflater();
                MyView =li.inflate(R.layout.urdumenuitem, parent,false);

                }
                else{
                    MyView =(View)convertView;
                }
            ImageView iv=(ImageView)MyView.findViewById(R.id.image1);   
            iv.setImageResource(mThumbIds[position]);
                   return MyView;
            }

    }


public class EngAdapter extends BaseAdapter{

    public Integer[] mThumbIds = {
            R.drawable.eng_pic1, R.drawable.eng_pic2,
            R.drawable.eng_pic3, R.drawable.eng_pic4,
            R.drawable.eng_pic5, R.drawable.eng_pic6,
            R.drawable.eng_pic7, R.drawable.eng_pic8,
            R.drawable.eng_pic9, R.drawable.eng_pic10,
            R.drawable.eng_pic11, R.drawable.eng_pic12,
            R.drawable.eng_pic13, R.drawable.eng_pic14,
            R.drawable.eng_pic15, R.drawable.eng_pic16,
            R.drawable.eng_pic17, R.drawable.eng_pic18,
            R.drawable.eng_pic19, R.drawable.eng_pic20,
            R.drawable.eng_pic21
           };
    private Context mContext;

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

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View MyView;
        if(convertView == null){

            LayoutInflater li=((Activity) mContext).getLayoutInflater();
            MyView =li.inflate(R.layout.urdumenuitem, parent,false);

            }
            else{
                MyView =(View)convertView;
            }
        ImageView iv=(ImageView)MyView.findViewById(R.id.image1);   
        iv.setImageResource(mThumbIds[position]);
               return MyView;
        }

}

}

XML_layout:

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ViewSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:background="#E0EEE0"
   >

    <GridView

        android:id="@+id/gridView1"
        android:layout_below="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="2" 
        android:horizontalSpacing="5dp"
        android:layout_margin="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center_vertical"
         />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gridView1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/settings"
        android:focusable="true" />

</RelativeLayout>


    <RelativeLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:background="#E0EEE0"
   >

    <GridView

        android:id="@+id/gridView2"
        android:layout_below="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="2" 
        android:horizontalSpacing="5dp"
        android:layout_margin="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center_vertical"
         />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gridView2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/settings"
        android:focusable="true" />

</RelativeLayout>


</ViewSwitcher>

问题仍然存在,如何在按钮点击时在同一个GridView中的两组图标之间切换。

1 个答案:

答案 0 :(得分:0)

在GridView的适配器中,您可以创建一个更改视图图标的方法,例如:

private boolean imageSetChange = false;

public void changeImages(boolean change){
    this.imageSetChange  = change;
    notifyDataSetChanged();
}

然后在你的getView()方法中,根据imageSetChanged的值切换项目。