ListView控件+的ImageButton + descendantFocusability [英] ListView + ImageButton + descendantFocusability
问题描述
我有一个列表视图,而我添加行1的ImageButton ..我试图设置的ImageButton的setfocusable虚假的,但仍无法正常工作。
item_list.xml
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =160dp
机器人:descendantFocusability =blocksDescendants
机器人:ID =@ + ID / RL_item>
<的ImageButton
机器人:layout_width =200dp
机器人:layout_height =FILL_PARENT
机器人:ID =@ + ID / ImageButton的
机器人:layout_alignParentTop =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentBottom =真/>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / LL_Installed
机器人:layout_alignRight =@ + ID / textView2
机器人:layout_alignBottom =@ + ID / TextView的
机器人:layout_alignParentBottom =真
机器人:paddingBottom会=@扪/ item_list_left_right
机器人:以下属性来=@扪/ item_list_left_right
机器人:可聚焦=假
机器人:focusableInTouchMode =假>
< ImageView的
机器人:layout_width =20dp
机器人:layout_height =20dp
机器人:ID =@ + ID / ImageView的
机器人:背景=@可绘制/滴答
机器人:可聚焦=假
机器人:focusableInTouchMode =FALSE/>
<的TextView
机器人:layout_height =FILL_PARENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=@字符串/ list_item_installed
机器人:ID =@ + ID / textView3
机器人:以下属性来=6DP
机器人:文字颜色=#FFFFFF
风格=@风格/ TextShadow
机器人:重力=center_vertical | fill_vertical
机器人:layout_weight =1
机器人:layout_width =0dip
机器人:可聚焦=假
机器人:focusableInTouchMode =FALSE/>
< / LinearLayout中>
<进度
风格=机器人:ATTR / progressBarStyleHorizontal
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /进度
机器人:layout_centerVertical =真
机器人:layout_alignParentLeft =真
机器人:layout_alignParentRight =真
机器人:能见度=隐形
机器人:可聚焦=假
机器人:focusableInTouchMode =FALSE/>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文本=
机器人:ID =@ + ID / textView2
机器人:layout_alignParentTop =真
机器人:layout_alignParentRight =真
风格=@风格/ TextShadow
机器人:paddingTop =@扪/ item_list_top
机器人:paddingRight =@扪/ item_list_left_right
机器人:可聚焦=假
机器人:focusableInTouchMode =FALSE/>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:文本=
机器人:ID =@ + ID / TextView的
机器人:layout_alignParentTop =真
机器人:layout_alignParentLeft =真
风格=@风格/ TextShadow
机器人:layout_toLeftOf =@ + ID / textView2
机器人:以下属性来=@扪/ item_list_left_right
机器人:paddingTop =@扪/ item_list_top
机器人:可聚焦=假
机器人:focusableInTouchMode =FALSE/>
< / RelativeLayout的>
Item_ListAdapter.java
公共类Item_ListAdapter扩展了BaseAdapter {
私人最终活动的活动;
私人最终的ArrayList< Item_List>项目;
私人的VI;
私人的ImageButton按钮;
公共Item_ListAdapter(活动活动,ArrayList的< Item_List>项目){
this.activity =活动;
this.items =项目;
}
@覆盖
公众诠释getCount将(){
返回items.size();
}
@覆盖
公共对象的getItem(INT位置){
返回items.get(位置);
}
@覆盖
众长getItemId(INT位置){
返回items.get(位置).getId();
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
VI = convertView;
如果(convertView == NULL){
LayoutInflater充气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
VI = inflater.inflate(R.layout.item_list,NULL);
}
Item_List项目= items.get(位置);
// Colocar埃尔TITULO
// Colocar洛杉矶出生日期
// Colocar EL背景
// Hacer可见Ø无形EL布局德instalado
TextView的txtTitulo =(TextView中)vi.findViewById(R.id.textView);
TextView的txtFecha =(TextView中)vi.findViewById(R.id.textView2);
的LinearLayout LL_Installed =(的LinearLayout)vi.findViewById(R.id.LL_Installed);
txtTitulo.setText(item.getTitle());
txtFecha.setText(item.getFecha());
如果(item.getInstalled()){
LL_Installed.setVisibility(View.VISIBLE);
资源RES = vi.getResources();
点阵位图= BitmapFactory.de codeFILE(item.getRutaImagen());
最后BitmapDrawable BD =新BitmapDrawable(资源,位图);
// ----------------------------------
按钮=(的ImageButton)vi.findViewById(R.id.imageButton);
//button.setBackgroundDrawable(bd);
button.setBackground(BD);
button.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
如果(event.getAction()== MotionEvent.ACTION_DOWN){
v.getBackground()setColorFilter(Color.parseColor(#B7B2B0),PorterDuff.Mode.MULTIPLY)。
Log.d(AAA,DOWN);
返回true;
}否则,如果(event.getAction()== MotionEvent.ACTION_UP){
。v.getBackground()clearColorFilter();
v.invalidate();
Log.d(AAA,向上);
返回true;
}
返回false;
}
});
} 其他 {
LL_Installed.setVisibility(View.INVISIBLE);
}
返回六;
}
这是我的'主'code ..我想在这里检测到preSS
的ListView LV =(ListView控件)findViewById(R.id.listView);
Item_ListAdapter适配器=新Item_ListAdapter(ListActivity.this,项目);
lv.setAdapter(适配器);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<> parnet,android.view.View认为,INT位置,长的id){
//阙项目公顷内幕交易pulsado
Toast.makeText(getApplicationContext(),将String.valueOf(位置),Toast.LENGTH_SHORT).show();
Log.d(AAA,将String.valueOf(位置));
}
});
我相信,以同时拥有的ListView行和ImageButton的点击,你就需要设置这两个的ImageButton的对象:
机器人:可调焦=假
机器人:focusableInTouchMode =假
把那一个镜头。我也想尝试使用onClickListener您的ImageButton,而不是touchListener。
I have a listview to which I add rows with 1 imagebutton .. I tried to set the imagebutton the setfocusable false but still not working ..
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:descendantFocusability="blocksDescendants"
android:id="@+id/RL_item">
<ImageButton
android:layout_width="200dp"
android:layout_height="fill_parent"
android:id="@+id/imageButton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/LL_Installed"
android:layout_alignRight="@+id/textView2"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/item_list_left_right"
android:paddingLeft="@dimen/item_list_left_right"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView"
android:background="@drawable/tick"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/list_item_installed"
android:id="@+id/textView3"
android:paddingLeft="6dp"
android:textColor="#ffffff"
style="@style/TextShadow"
android:gravity="center_vertical|fill_vertical"
android:layout_weight="1"
android:layout_width="0dip"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
style="@style/TextShadow"
android:paddingTop="@dimen/item_list_top"
android:paddingRight="@dimen/item_list_left_right"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
style="@style/TextShadow"
android:layout_toLeftOf="@+id/textView2"
android:paddingLeft="@dimen/item_list_left_right"
android:paddingTop="@dimen/item_list_top"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
Item_ListAdapter.java
public class Item_ListAdapter extends BaseAdapter {
private final Activity activity;
private final ArrayList<Item_List> items;
private View vi;
private ImageButton button;
public Item_ListAdapter(Activity activity, ArrayList<Item_List> items) {
this.activity = activity;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
vi=convertView;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.item_list, null);
}
Item_List item = items.get(position);
// Colocar el titulo
// Colocar la fecha
// Colocar el background
// Hacer visible o invisible el layout de instalado
TextView txtTitulo = (TextView) vi.findViewById(R.id.textView);
TextView txtFecha = (TextView) vi.findViewById(R.id.textView2);
LinearLayout LL_Installed = (LinearLayout) vi.findViewById(R.id.LL_Installed);
txtTitulo.setText(item.getTitle());
txtFecha.setText(item.getFecha());
if (item.getInstalled()) {
LL_Installed.setVisibility(View.VISIBLE);
Resources res = vi.getResources();
Bitmap bitmap = BitmapFactory.decodeFile(item.getRutaImagen());
final BitmapDrawable bd = new BitmapDrawable(res, bitmap);
// ----------------------------------
button = (ImageButton) vi.findViewById(R.id.imageButton);
//button.setBackgroundDrawable(bd);
button.setBackground(bd);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(Color.parseColor("#B7B2B0"), PorterDuff.Mode.MULTIPLY);
Log.d("aaa","DOWN");
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().clearColorFilter();
v.invalidate();
Log.d("aaa","UP");
return true;
}
return false;
}
});
} else {
LL_Installed.setVisibility(View.INVISIBLE);
}
return vi;
}
This is my 'main' code.. i want detect the press here
ListView lv = (ListView)findViewById(R.id.listView);
Item_ListAdapter adapter = new Item_ListAdapter(ListActivity.this, items);
lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parnet, android.view.View view, int position, long id) {
// Que item ha sido pulsado
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
Log.d("aaa", String.valueOf(position) );
}
});
I believe in order to have BOTH the listView row and the ImageButton clickable, you'll need to set both of these on the ImageButton object:
android:focusable="false"
android:focusableInTouchMode="false"
Give that a shot. I'd also try using an onClickListener on your ImageButton instead of the touchListener.
这篇关于ListView控件+的ImageButton + descendantFocusability的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!