在ImageAdapter中更改ImageView onClick

时间:2013-01-28 19:42:32

标签: java android uiimageview onclick

由于标题已经提到,我想改变我的ImageView onClick。我在ImageAdapter中创建了一个包含4个项目的GridView。当我点击每个ImageView时,它会在它周围创建一个大的橙色区域。我有一个自定义'按下ImageView',并希望使用它。我该怎么做?

提前致谢

ImageAdapter:

public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] buttonValues;

public ImageAdapter(Context context, String[] buttonValues) {
    this.context = context;
    this.buttonValues = buttonValues;
}

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.mobile, null);


        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);

        String button = buttonValues[position];



        if (button.equals("homework")) {
            imageView.setImageResource(R.drawable.button_schedule);
        } else if (button.equals("schedule")) {
            imageView.setImageResource(R.drawable.schedulebut);
        } else if (button.equals("planner")) {
            imageView.setImageResource(R.drawable.plannerbut);
        } else {
            imageView.setImageResource(R.drawable.settingsbut);
        }

    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

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

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

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

}

我的GridView方法(活动):

static final String[] MOBILE_OS = new String[] { "homework", "schedule",
        "planner", "settings" };
private void setGridView() {
    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            switch (position) {
            case 0:
                v.setBackgroundColor(R.drawable.button_schedule);
                break;
            case 1:
                Toast.makeText(getApplicationContext(), "schedule", Toast.LENGTH_SHORT).show();
                break;

            case 2:
                Toast.makeText(getApplicationContext(), "planner",
                        Toast.LENGTH_SHORT).show();
                break;

            case 3:
                Toast.makeText(getApplicationContext(), "settings",
                        Toast.LENGTH_SHORT).show();
                break;
            }

        }
    });

}

我的按钮选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/homeworkpres" android:state_pressed="true"/>
<item android:drawable="@drawable/homeworkpres" android:state_enabled="false"/>
<item android:drawable="@drawable/homework"/>

mobile.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:layout_gravity="center"
android:gravity="center"
android:padding="5dp" >

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:layout_gravity="center"
    android:contentDescription="@string/logo"
    android:src="@drawable/plannerbut"
    >
</ImageView>


1 个答案:

答案 0 :(得分:1)

gridview.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        arg1.setBackgroundColor(R.drawable.selector);

        }});

selector.xml

  <item 
        android:state_pressed="true"
        android:drawable="@drawable/btn_pressed"/>
    <item 
        android:state_enabled="false"
        android:drawable="@drawable/btn_pressed"/>
    <item
        android:drawable="@drawable/btn_normal"/>

其中btn_pressed和btn_normal是你的图片。