我使用GridView显示类别,我在网格视图中使用了ImageButton和文本视图,但是我无法在这些项目上设置setOnItemClickListener(点击它时发生的Imean nothings) 我的代码是 这是我的GridView适配器
public class MyAdapter extends BaseAdapter{
private ArrayList<String> listJewel_name;
private ArrayList<Integer> listJewellery;
private Activity activity;
//private LayoutInflater inflater;
public MyAdapter(Activity activity,ArrayList<String> listJewel_name, ArrayList<Integer> listJewellery) {
super();
this.listJewel_name = listJewel_name;
this.listJewellery = listJewellery;
this.activity = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listJewel_name.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listJewel_name.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageButton imgViewJewel;
public TextView txtViewTitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.tvtext);
view.imgViewJewel = (ImageButton) convertView.findViewById(R.id.picture);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listJewel_name.get(position));
view.imgViewJewel.setImageResource(listJewellery.get(position));
return convertView;
}
}
我的activity.class是
GridView gridView;
Button add;
private MyAdapter mAdapter;
private ArrayList<String> listJewel_name;
private ArrayList<Integer> listJewellery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_jewellery_option);
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new MyAdapter(this,listJewel_name, listJewellery);
// Set custom adapter to gridview
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
String shri=mAdapter.getItem(position);
Toast.makeText(SelectJewelleryOption.this, shri , Toast.LENGTH_SHORT).show();
}
});
}
我的XML文件是
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.myjewelbox.SquareImageButtonView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="130dp"
android:scaleType="fitCenter"
android:clickable="true" />
<TextView
android:id="@+id/tvtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="1dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="bottom"
android:textColor="@color/Golden"
android:background="#55000000"
android:focusable="false"
/>
</FrameLayout>
我已将此XMl文件设置为我的Main XML文件的GridView
答案 0 :(得分:2)
为其适配器中的GridView项设置ItemClickListener,而不是直接为GridView设置ONItemClickListener。
view.imgView Jewel.setonItemClickListener(activity);
答案 1 :(得分:0)
在你的适配器类中,返回行之前的convertView上的setOnClickListener。
convertView.setOnClickListener(new onClickListener(){
//In this method you can get the items same as you are getting in your
//adapter, as follow:-
String name=listJewel_name.get(position);
//or you can do whatever you want as now you have the whole view that is
//clicked, so you can event get its children or start a new activity or
//whatever you want
}
return converView;
希望这有帮助。
答案 2 :(得分:0)
请将您的xml更改为此 -
添加第2行 -
机器人:可点击= “真”
机器人:可聚焦= “假”
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.myjewelbox.SquareImageButtonView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="130dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/tvtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#55000000"
android:focusable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="1dp"
android:paddingTop="10dp"
android:textColor="@color/Golden" />
</FrameLayout>
有关详细信息,请参阅此处 - http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html
答案 3 :(得分:-1)
确保android:focusable="false"
First Child
GridView Layout
。{
和
GridViews
无法处理可点击的项目。例如Button
将不起作用。如果你有任何其他non-clickable
项,你必须确保你没有设置属性:clikable =“true”。
请参阅以下链接 - 它与您的期望相同
http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html