2017-04-17 70 views
-3

我創建了一個包含圖像視圖和文本視圖的自定義網格視圖。我想單擊這兩個視圖像點擊圖像圖標以及文本視圖。還有一件事是這個自定義網格視圖是在片段中實現的。我使用適配器類來設置從Base Adapter類擴展的圖標和文本標題。 所以請任何人知道如何實現這個請返回與答案.. 在此先感謝。如何點擊自定義網格視圖圖像圖標?

+0

郵編。 @sid –

+0

請將相關代碼 –

回答

0

試試這個,

JAVA:

public class MyAdapter extends BaseAdapter 
{ 
    /* Variable declaration */ 
    private final Context mContext; 
    private final ArrayList<Menu> al_Menu; 
    private Holder viewHolder; 


    public MyAdapter (Context p_context, ArrayList<Menu> al_Menu) 
    { 
     super(); 
     this.mContext = p_context; 
     this.al_Menu = al_Menu; 
    } 

    @Override 
    public int getCount() 
    { 
     return al_Menu.size(); 
    } 

    @Override 
    public Object getItem(int p_int_arg0) 
    { 
     return p_int_arg0; 
    } 

    @Override 
    public long getItemId(int p_int_arg0) 
    { 
     return p_int_arg0; 
    } 

    @Override 
    public View getView(final int p_int_pos, View p_v_convertView, ViewGroup p_vg_parent) 
    { 
     if (p_v_convertView == null) 
     { 
      /*inflate the layout */ 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      p_v_convertView = inflater.inflate(R.layout.adapter_my, p_vg_parent, false); 
      viewHolder = new Holder(); 
      viewHolder.iv_image =(ImageView) p_v_convertView.findViewById(R.id.icon_image); 
      viewHolder.tv_text =(TextView) p_v_convertView.findViewById(R.id.icon_text); 
      p_v_convertView.setTag(viewHolder); 
     } 
     else 
     { 
      viewHolder = (Holder) p_v_convertView.getTag(); 
     } 
     /*set text*/ 
     viewHolder.tv_text.setText(al_Menu.get(p_int_pos).getStrMenuName()); 

     /*set image*/ 
     viewHolder.iv_search.setImageResource(al_Menu.get(p_int_pos).getStrMenuImage()); 

     viewHolder.iv_image.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       //do here 
      } 
     }); 

     viewHolder.tv_text.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       //do here 
      } 
     }); 

     return p_v_convertView; 
    } 

    class Holder 
    { 
     ImageView iv_image; 
     TextView tv_text; 
    } 
} 

adapter_my.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 

      <ImageView 
       android:id="@+id/icon_image" 
       android:layout_width="90dp" 
       android:layout_height="90dp" 
       android:layout_gravity="center_horizontal" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="4dp" 
       android:scaleType="fitXY" 
       /> 

      <TextView 
       android:id="@+id/icon_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/icon_image" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:textSize="17sp" 
       android:textColor="#000000"/> 

    </RelativeLayout> 
+0

設置爲將圖像圖標設置爲圖像視圖的位置? – sid

+0

在viewHolder.tv_text.setText下面設置imageview圖標 – user2025187

+0

您直接將setOnClickListener設置爲viewHolder.tv_text.setText下方的圖像。 – sid

0

你需要把兩個在一個containerviews然後分配onClickcontainer。然後設置android:duplicateParentState="true"以啓用相同的onClick methodchild views。您的自定義的GridView的

<RelativeLayout 
     android:id="@+id/relativeLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/textView" 
     android:duplicateParentState="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:duplicateParentState="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView" 
     app:srcCompat="@mipmap/ic_launcher" /> 
</RelativeLayout> 
相關問題