使gridView中的imageView可单击

时间:2015-11-20 15:24:02

标签: android android-activity gridview imageview

我有这个带有imageView项目的gridView,但我无法点击它。我是Android的新手,我不明白如何使这些图像可以点击。我尝试了一些我在其他帖子中读过的答案,但它没有用。

...帮助

另一个问题!如果我想将硬编码的字符串传递给另一个活动,但每次单击imageView项时此字符串都会更改。我可以声明一个静态字符串,然后我说string =" word"然后使用Activity.string从其他活动中调用它?我会在其他活动中获得价值吗?

请在下面找到我的xml文件,gridViewAdapter和活动!

services_list_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ServicesListActivity">


<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="4dp"
    android:columnWidth="80dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth" />
</RelativeLayout>

gris_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >

<ImageView
    android:id="@+id/item_image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/atm">
</ImageView>

<TextView
    android:id="@+id/item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:textSize="15sp" >
</TextView>   
</LinearLayout>

CustomGridViewAdapter.java

public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();

public CustomGridViewAdapter(Context context, int layoutResourceId,
                             ArrayList<Item> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RecordHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
        holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
        row.setTag(holder);
    } else {
        holder = (RecordHolder) row.getTag();
    }

    Item item = data.get(position);
    holder.txtTitle.setText(item.getTitle());
    holder.imageItem.setImageBitmap(item.getImage());


    return row;

    }
    static class RecordHolder {
    TextView txtTitle;
    ImageView imageItem;

     }

        }

ServicesListActivity.java

public class ServicesListActivity extends Activity implements AdapterView.OnItemClickListener{
protected static final Toast text = null;

//String to pass to another activity
public static String types;

GridView gridView;

ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;

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

    //set grid view item
    Bitmap atmIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.atm);
    Bitmap bankIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bank);
    Bitmap barIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bar);
    Bitmap cafeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.cafe);

    gridArray.add(new Item(atmIcon,"ATMs"));
    gridArray.add(new Item(bankIcon,"Banks"));
    gridArray.add(new Item(barIcon,"Bars"));
    gridArray.add(new Item(cafeIcon,"Cafés"));

    gridView = (GridView) findViewById(R.id.gridView1);
    customGridAdapter = new CustomGridViewAdapter(this, R.layout.grid_row, gridArray);
    gridView.setAdapter(customGridAdapter);


}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //intent
    Intent data = new Intent(getApplicationContext(), ListActivity.class);
        switch (position) {
            case 0:
                //String to send to another activity
                types= "restaurant";
                startActivity(data);
                break;

            case 1:

                startActivity(data);
                break;
            case 2:

                startActivity(data);
                break;
            case 3:

                startActivity(data);
                break;

            default:
                break;
        }

}

        }

1 个答案:

答案 0 :(得分:1)

将此添加到您的ImageView:

android:clickable="true"

有反馈意见:

android:background="?attr/selectableItemBackground"

然后在Activity中设置OnClickListener:

    yourImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });