2013-11-20 62 views
0

我正在使用GridView来显示类别,我已经在Grid View中使用了ImageButton和Text View,但是我无法对这些项目设置OnOnItemClickListener(Imean nothings发生在点击它) My代码是 这是我的GridView没有发生点击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文件设置为我的主XML文件的GridView控件

+0

你能请分享您的GridView布局文件? – Subbu

+0

@Homosapiens我已经加入了CML文件,请参阅 我也加入了我的XML外观 –

+0

删除'安卓clickable'和'安卓:从孩子的观点focusable',只有在GridView的设置。 –

回答

2

集ItemClickListener在它的GridView项的适配器,而不是直接的GridView设置ItemClickListener。

view.imgViewJewel.setItemClickListener(活性);

+0

这是正确的,如果活动实现相关的听众 – azerto00

+0

请检查更新,并建议我请@ azerto00 –

+0

在适配器的getview()中的视图上设置setItemClickListener()like ---- view.txtViewTitle.setText(listJewel_name.get(position )); view.imgViewJewel.setImageResource(listJewellery.get(位置)); view.imgViewJewel。setOnClickListener(listner) return convertView; – vinay

-1

确保android:focusable="false"First ChildGridView Layout

GridViews不能处理点击的项目。例如Button将不起作用。而且,如果您有任何其他non-clickable项目,你必须确保你没有设置属性:clikable =“真”。

请参考以下链接 - 这是一样的,你指望

http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html

+0

我设置这些clicable和可聚焦性,但没有任何事情发生 –

+0

能否请您分享xml文件? – Subbu

+0

您能否将您的SquareImageButtonView设置为android:clickable =“false”并检查? – Subbu

0

什么在你的适配器类,setOnClickListener您convertView前回线。

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; 

希望这有助于。

0

请改变你的XML来这 -

添加thease 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

+0

还是空话破天.... –

+0

那么你正在做一些事情是错误的。刚刚从这里下载代码并替换您的图像和文本的 https://github.com/manishsri01/CustomGridView –

+0

我做了,但也还没破天乌尔方式....我现在该怎么办???? –

相关问题