单击时用另一个图标更改grideview图标

时间:2015-02-19 13:57:36

标签: gridview menu icons

我创建了一个gridview菜单,当我点击另一个图标时,我想更改每个图标。 我有4个图标点击每个打开新活动,我想在gridview中点击图标时首先点击图标更改为另一个图标然后打开活动。

实际上是制作图形菜单。

这是ImageAdapter.java:

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.public_news, R.drawable.azmoon_news,
            R.drawable.azmoon_result, R.drawable.super_user,
    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
        return imageView;
    }
}

这是MainActivity.java:

public class MainActivity extends ActionBarActivity {

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

        final GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));


        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Sending image id to FullScreenActivity

                Intent i;
                switch (position) {
                    case 0:
                        i = new Intent(getApplicationContext(), PublicNewsActivity.class);
                        // passing array index
                        i.putExtra("id", position);
                        startActivity(i);
                        break;
                    case 1:
                        i = new Intent(getApplicationContext(), AzmoonNewsActivity.class);
                        // passing array index
                        i.putExtra("id", position);
                        startActivity(i);
                        break;
                    case 2:
                        i = new Intent(getApplicationContext(), AzmoonResult.class);
                        // passing array index
                        i.putExtra("id", position);
                        startActivity(i);
                        break;
                    case 3:
                        i = new Intent(getApplicationContext(), SuperUser.class);
                        // passing array index
                        i.putExtra("id", position);
                        startActivity(i);
                        break;
                }

            }
        });

    }
}
谢谢。

0 个答案:

没有答案