一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

解决ListView中的Item点击事件和子控件的冲突或者item点击没有反应

时间:2022-11-14 22:17:09 编辑:袖梨 来源:一聚教程网

fragment中添加了button和checkbox这些控件,此时这些子控件会将焦点获取到,所以常常当点击item时变化的是子控件,item本身的点击没有响应。

这时候就可以使用descendantFocusability来解决啦,API描述如下:
android:descendantFocusability

该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。

属性的值有三种:

beforeDescendants:viewgroup会优先其子类控件而获取到焦点

afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

通常我们用到的是第三种,即在Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性。

写这个demo顺便复习一下BaseAdapter

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Audition1 extends Activity {

private ListView listView;
a
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audition1);
initComponents();

}

private void initComponents() {
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MyAdapter(this));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "item", 300).show();

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_audition1, menu);
return true;
}

public final class ViewHolder {
public TextView textView;
public Button button;
public ImageView imageView;
}

class MyAdapter extends BaseAdapter {

private LayoutInflater mInflater;

public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return 3;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();

convertView = mInflater.inflate(R.layout.item, null);
holder.imageView = (ImageView) convertView
.findViewById(R.id.imageView);
holder.textView = (TextView) convertView
.findViewById(R.id.textViewId);
holder.button = (Button) convertView.findViewById(R.id.button);
holder.textView.setText("shit");
holder.button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
Audition1.this);
builder.setMessage("dialog");
builder.setTitle("title");
builder.create();
builder.show();
}
});
convertView.setTag(holder);
} else {
convertView.getTag();
}

return convertView;
}

}

}


xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_ >

android:id="@+id/listView"
android:layout_
android:layout_
android:padding="@dimen/padding_medium"
tools:context=".Audition1"
android:divider/>

?

xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="horizontal">

android:id="@+id/textViewId"
android:layout_
android:layout_
android:text="@string/hello_world"
/>
android:id="@+id/imageView"
android:layout_
android:layout_
android:src="@drawable/ic_launcher"
android:contentDescription="@string/app_name"
/>

热门栏目