如何获得Custom Gird子项目点击事件?

时间:2012-12-17 07:24:30

标签: android gridview event-handling

我正在努力使用自定义网格视图。不完全使用自定义网格视图,但其单击子视图事件。

主要两个查询:

首先:

问题是,如果我在网格中添加简单的图像视图和文本视图,并单击其工作的任何项目,并显示该项目的位置。但是当我在网格中添加两个按钮替换不会调用简单的文本视图时按钮点击项目的事件。

第二名:

我希望获得所有三个视图的点击事件,如图像和两个按钮。 如果我点击图像它应该做一些任务和两个按钮相同。

看到我有这个代码。

主要java:

package com.mkyong.android;

import java.util.ArrayList;

import com.mkyong.android.adapter.ImageAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity {

    GridView gridView;

    private ArrayList<String> listCountry;
    private ArrayList<Integer> listFlag;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridView = (GridView) findViewById(R.id.gridView1);
        prepareList();
        gridView.setAdapter(new ImageAdapter(this, listCountry, listFlag));

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                ImageView img = (ImageView) v
                        .findViewById(R.id.grid_item_image);
                Toast.makeText(getApplicationContext(), "item" + img.getTag(),
                        Toast.LENGTH_SHORT).show();

            }

        });

    }

    public void prepareList() {

        listCountry = new ArrayList<String>();

        listCountry.add("india");
        listCountry.add("Brazil");
        listCountry.add("Canada");
        listCountry.add("China");
        listCountry.add("France");
        listCountry.add("Germany");
        listCountry.add("Iran");
        listCountry.add("Italy");
        listCountry.add("Japan");
        listCountry.add("Korea");
        listCountry.add("Mexico");
        listCountry.add("Netherlands");
        listCountry.add("Portugal");
        listCountry.add("Russia");
        listCountry.add("Saudi Arabia");
        listCountry.add("Spain");
        listCountry.add("Turkey");
        listCountry.add("United Kingdom");

        listFlag = new ArrayList<Integer>();

        listFlag.add(R.drawable.sample_thumb_1);
        listFlag.add(R.drawable.sample_thumb_2);
        listFlag.add(R.drawable.sample_thumb_3);
        listFlag.add(R.drawable.sample_thumb_4);
        listFlag.add(R.drawable.sample_thumb_5);
        listFlag.add(R.drawable.sample_thumb_6);
        listFlag.add(R.drawable.sample_thumb_7);
        listFlag.add(R.drawable.sample_thumb_8);
        listFlag.add(R.drawable.sample_thumb_9);
        listFlag.add(R.drawable.sample_thumb_10);
        listFlag.add(R.drawable.sample_thumb_11);
        listFlag.add(R.drawable.sample_thumb_12);
        listFlag.add(R.drawable.sample_thumb_13);
        listFlag.add(R.drawable.sample_thumb_14);
        listFlag.add(R.drawable.sample_thumb_15);
        listFlag.add(R.drawable.sample_thumb_16);
        listFlag.add(R.drawable.sample_thumb_17);
        listFlag.add(R.drawable.sample_thumb_18);

    }

}

image_adp:

package com.mkyong.android.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mkyong.android.R;

public class ImageAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<Integer> listFlag;
    private ArrayList<String> listCountry;

    public ImageAdapter(Activity activity, ArrayList<String> listCountry,
            ArrayList<Integer> listFlag) {
        this.activity = activity;
        this.listCountry = listCountry;
        this.listFlag = listFlag;
    }

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

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

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

    public static class ViewHolder {
        public ImageView imgViewFlag;
        public TextView txt;
        public Button btn1, btn2;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.grid_item, null);

            view.imgViewFlag = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            view.txt = (TextView) convertView
                    .findViewById(R.id.grid_item_label);
            view.btn1 = (Button) convertView.findViewById(R.id.button1);
            view.btn2 = (Button) convertView.findViewById(R.id.button2);
            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }

        view.imgViewFlag.setImageResource(listFlag.get(position));
        view.imgViewFlag.setTag(listFlag.get(position),
                "" + listFlag.get(position));
        // view.txt.setText(listCountry.get(position));

        return convertView;
    }

}

grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="120dp"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="1dp"
            android:background="@drawable/like" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="1dp"
            android:background="@drawable/dislike" />

        <TextView
            android:id="@+id/grid_item_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

</LinearLayout>

我尝试找到的东西,但没有成功。如何实现这一点。

感谢您访问我的查询

1 个答案:

答案 0 :(得分:2)

中的getView例如

viewHolder.checkbox.setOnCheckedChangeListener(listenerCheckBox);

和听众

OnCheckedChangeListener listenerCheckBox = new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

        }
    };

你的item_grid例如

    public class Item {

        private String title;
        private String description;
        private Date pubDate;
        private String link;

        public Item(String title, String description, Date pubDate, String link) {
            this.title = title;
            this.description = description;
            this.pubDate = pubDate;
            this.link = link;
        }

public String getTitle() {

return this.title;
}
public String setTitle(String title_) {

this.title  = title_;
}

    }

\\\\\\\\\\\\\\\\\\\\

如果你的情况

 if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.grid_item, null);

            view.imgViewFlag = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            view.txt = (TextView) convertView
                    .findViewById(R.id.grid_item_label);
            view.btn1 = (Button) convertView.findViewById(R.id.button1);
            view.btn2 = (Button) convertView.findViewById(R.id.button2);



view.btn1.setOnClicListener(myClickListener);

            convertView.setTag(view);



        } else 

\\\\\\\\\\\\\\\\\\\\\\

private OnClickListener myClickListener = new OnClickListener() {
        public void onClick(View v) {


        }
    };

\\\\\\\\\\\\\\\\\\\\\\\