我创建了一个自定义网格视图,其中包括图像视图和文本视图。我想点击两个视图,如点击图像图标和文本视图。还有一件事是这个自定义网格视图是以片段形式实现的。我使用适配器类来设置从Base Adapter类扩展的图标和文本标题。 所以请任何人知道实现这一点的方法请回复回答.. 提前致谢。
答案 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>
答案 1 :(得分:0)
您需要将views
放在一个container
中,然后将onClick
分配给container
。然后设置android:duplicateParentState="true"
以启用相同的onClick method
到child views
。
<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>