问题描述
|
如果有更好的方法,请告诉我,这可能不是正确的方法。
我创建了一个自定义适配器类,并在我的getView方法中为我要使用的视图充气
public View getView(int position,View convertView,ViewGroup parent)
{
View v = mInflater.inflate(R.layout.wherelayout,null);
if (convertView != null)
{
v = convertView;
}
HashMap<String,Object> whereHash = (HashMap<String,Object>) this.getItem(position);
if (whereHash != null)
{
TextView whereId = (TextView) v.findViewById(R.id.tvWhere);
TextView whereDetails = (TextView) v.findViewById(R.id.tvWhereDetails);
ImageButton ibDelWhere = (ImageButton) v.findViewById(R.id.ibDelWhere);
whereId.setText((CharSequence) whereHash.get(\"where\"));
whereDetails.setText((CharSequence) whereHash.get(\"details\"));
if (ibDelWhere != null)
{
ibDelWhere.setId(position);
ibDelWhere.setonClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//do stuff when clicked
}
}
);
}
}
return v;
}
该视图由向左对齐的2个TextView和向右对齐的ImageButton组成,我希望能够在单击按钮时从ListView中删除该项目。布局是这样的-
<RelativeLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\" android:orientation=\"horizontal\" android:clickable=\"true\">
<TextView android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:textSize=\"25sp\" android:id=\"@+id/tvWhere\" android:textColor=\"#00FF00\" android:text=\"TextView\" android:gravity=\"top|left\" android:layout_alignParentTop=\"true\" android:layout_alignParentLeft=\"true\"></TextView>
<TextView android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:id=\"@+id/tvWhereDetails\" android:textColor=\"#0000FF\" android:text=\"TextView\" android:textSize=\"18sp\" android:layout_below=\"@+id/tvWhere\" android:gravity=\"bottom|left\" android:layout_alignParentLeft=\"true\"></TextView>
<ImageButton android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:src=\"@drawable/eraser\" android:id=\"@+id/ibDelWhere\" android:layout_alignParentRight=\"true\" android:layout_alignParentTop=\"true\"></ImageButton>
</RelativeLayout>
问题是,当ImageButton在布局中时,我可以单击它,并且onClick()会按预期触发,但是我不能单击实际的列表项本身,即单击TextView项以触发ListView.onItemClick已经分配给它了。如果我从布局中删除ImageButton,则单击该项目时将触发ListView.onItemClick事件。有什么办法可以使我同时单击ListView项和布局中的按钮?
谢谢你们和加尔斯。
解决方法
您必须将imagebutton设置为非
focusable
和非focusableInTouchMode
(可单击即可)。
请注意,与其他视图相反,您不能在xml中执行此操作,因为android:focusable
在ImageButton
的构造函数中被覆盖。
更准确地说,这是ImageView
和ImageButton
之间的少数差异之一。亲自看看,这是ѭ5的完整资料。
@RemoteView
public class ImageButton extends ImageView {
public ImageButton(Context context) {
this(context,null);
}
public ImageButton(Context context,AttributeSet attrs) {
this(context,attrs,com.android.internal.R.attr.imageButtonStyle);
}
public ImageButton(Context context,AttributeSet attrs,int defStyle) {
super(context,defStyle);
setFocusable(true);
}
@Override
protected boolean onSetAlpha(int alpha) {
return false;
}
}
要解决此问题,只需从Java调用setFocusable(false)
。或者使用ImageView
:)
myImageButton.setFocusable(false);
希望能帮助到你。
,您可以将两者都设为可点击状态,但实际上并不受它的支持,Romain Guy会对您大喊大叫。另外,您将无法使用轨迹球聚焦/按下按钮。话虽如此,您可以将以下属性添加到按钮上,这两个属性都应可单击:
android:focusable=\"false\"
android:focusableInTouchMode=\"false\"
只要确保您可以承受后果即可。
,尝试设定
相对布局上的android:clickable = \“ false \”。
我在另一个Linearlayout中的LinearLayout遇到了相同的问题。
外部LinearLayout为clickable = true,结果:
ListView.OnItemClickListener不会触发。
将其设置为clickable = false后,它可以工作。
, 即,单击TextView项目以触发已分配给它的ListView.onItemClick。
当ImageButton在那里时,单击TextView会发生什么?它是否注册了单击按钮?还是什么都不做?
ImageButton在行中占用多少空间?可能是因为它足够大,您无法单击它外面的行。
,我有同样的问题。我的解决方案是为适配器内部的视图设置onClick方法,而不是使用onItemClick。
@Override
public View getView(int position,View convertView,ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(R.layout.list_item,parent,false);
}
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//This should replace the onListItemClick method
}
});
v.findViewById(R.id.someinnerview).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Write one of these for each of your inner views
}
});
return v;
}
希望有帮助!根据您程序的其余部分,这可能会迫使您将更多数据交给适配器(我必须这样做),但是它可以工作。
,这是列表视图中定制适配器的示例。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<RelativeLayout
android:id=\"@+id/relativeLayout1\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:padding=\"5dip\">
<ImageView
android:layout_width=\"50dip\"
android:layout_height=\"50dip\"
android:id=\"@+id/imgViewLogo\"
android:src=\"@drawable/icon\"
android:layout_alignParentLeft=\"true\"
android:layout_centerInParent=\"true\"
android:scaleType=\"center\">
</ImageView>
<TextView
android:textAppearance=\"?android:attr/textAppearanceLarge\"
android:layout_height=\"wrap_content\"
android:text=\"TextView\"
android:layout_width=\"wrap_content\"
android:id=\"@+id/txtViewTitle\"
android:layout_toRightOf=\"@+id/imgViewLogo\"
android:layout_marginLeft=\"2dip\">
</TextView>
<TextView
android:layout_height=\"wrap_content\"
android:text=\"TextView\"
android:layout_width=\"wrap_content\"
android:id=\"@+id/txtViewDescription\"
android:layout_toRightOf=\"@+id/imgViewLogo\"
android:layout_below=\"@+id/txtViewTitle\"
android:layout_marginLeft=\"2dip\">
</TextView>
<TextView
android:layout_height=\"wrap_content\"
android:text=\"TextView\"
android:layout_width=\"wrap_content\"
android:id=\"@+id/txtViewMobile\"
android:layout_toRightOf=\"@+id/imgViewLogo\"
android:layout_below=\"@+id/txtViewDescription\"
android:layout_marginLeft=\"2dip\">
</TextView>
并在BaseAdapter上制作Java文件
public class ListViewCustomAdapter extends BaseAdapter {
Context context;
String[] mobile;
String[] month;
String[] number;
public LayoutInflater inflater;
public ListViewCustomAdapter(Context context,String[] month,String[] number,String[] mobile) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.month=month;
this.number=number;
this.mobile=mobile;
Log.i(\"88888888888888888\",\"*******333********\");
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//ADC71C 95AA1F
public int getCount() {
// TODO Auto-generated method stub
return month.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
ImageView imgViewLogo;
TextView txtViewDescription;
TextView txtViewMobile;
}
public View getView(int position,ViewGroup parent)
{
// TODO Auto-generated method stub
Log.i(\"88888888888888888\",\"*******444********\");
ViewHolder holder;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null)
{
Log.i(\"88888888888888888\",\"*******555********\");
convertView = inflater.inflate(R.layout.listitem_row,null);
holder = new ViewHolder();
holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);
holder.txtViewMobile = (TextView) convertView.findViewById(R.id.txtViewMobile);
convertView.setTag(holder);
}
else
{
Log.i(\"88888888888888888\",\"*******666********\");
holder = (ViewHolder) convertView.getTag();
}
Log.i(\"888888888888\",\"Display the value of the textbox like(9856321584)other wise (TextView)\");
holder.txtViewTitle.setText(month[position]);
holder.txtViewDescription.setText(number[position]);
holder.txtViewMobile.setText(mobile[position]);
return convertView;
}
}