0

正如标题已经提到的那样,我想更改我的 ImageView onClick。我在 ImageAdapter 中制作了一个包含 4 个项目的 GridView。当我单击每个 ImageView 时,它会在其周围创建一个大的橙色区域。我有一个自定义的“按下 ImageView”并想使用它。我该怎么做?

提前致谢

图像适配器:

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"/>

移动.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>


4

1 回答 1

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);

        }});

选择器.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 是您的图像。

于 2013-01-28T19:51:07.860 回答