Java onListItemClick方法不起作用
我正在尝试将SherlockListFragment与自定义适配器一起使用,但onListItemClick方法不起作用。我根本无法点击列表。有人能帮我修复我的Java onListItemClick方法不起作用,java,android,list,Java,Android,List,我正在尝试将SherlockListFragment与自定义适配器一起使用,但onListItemClick方法不起作用。我根本无法点击列表。有人能帮我修复我的onListItemClick方法吗 public class Wallpaper extends SherlockListFragment implements OnItemClickListener { WallpaperAdapter wAdapter; ArrayList<WallpaperHolder> wa
onListItemClick
方法吗
public class Wallpaper extends SherlockListFragment implements
OnItemClickListener {
WallpaperAdapter wAdapter;
ArrayList<WallpaperHolder> wallpapers;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
wallpapers = new ArrayList<WallpaperHolder>();
for (int i = 0; i < 4; i++) {
final int pos = i;
wallpapers.add(new WallpaperHolder() {
{
title = "Title" + pos;
location = "";
}
});
}
wAdapter = new WallpaperAdapter(getActivity(), wallpapers);
setListAdapter(wAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_wallpaper, container, false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
public void PassIntent() {
Intent intent = new Intent(getActivity(), ImageViewDialog.class);
intent.putExtra("LOC", "/storage/sdcard/pic/Desert.jpg");
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "OnItemClick", Toast.LENGTH_LONG).show();
}
公共类墙纸扩展SherlockListFragment实现
麦克利克监听器{
瓦达普特壁纸;
ArrayList壁纸;
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onActivityCreated(savedInstanceState);
壁纸=新阵列列表();
对于(int i=0;i<4;i++){
最终int pos=i;
墙纸。添加(新墙纸架(){
{
title=“title”+pos;
位置=”;
}
});
}
wAdapter=新墙纸适配器(getActivity(),墙纸);
setListAdapter(wAdapter);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//TODO自动生成的方法存根
返回充气机。充气(R.layout.activity_墙纸、容器、假);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
super.onListItemClick(左、右、位置、id);
}
公共目的{
Intent Intent=新的Intent(getActivity(),ImageViewDialog.class);
intent.putExtra(“LOC”,“/storage/sdcard/pic/dester.jpg”);
星触觉(意向);
getActivity().重写EndingTransition(R.anim.fadein,R.anim.fadeout);
}
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
Toast.makeText(getActivity(),“OnItemClick”,Toast.LENGTH_LONG.show();
}
墙纸SherlockListFragment的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAFAFA"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Wallpaper" >
<ListView
android:id="@android:id/list"
android:layout_width="500dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="151dp" >
</ListView>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:focusable="false"
android:text="Enable wallpaper Changer" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignLeft="@android:id/list"
android:layout_alignTop="@+id/checkBox1"
android:scaleType="fitCenter"
android:src="@drawable/ic_images" />
适配器代码
public class WallpaperAdapter extends BaseAdapter {
List<WallpaperHolder> wallpapers;
List<WallpaperHolder> wallpapers2;
LayoutInflater inflater;
Context c;
public WallpaperAdapter(Context context,List<WallpaperHolder> wallHolder){
this.wallpapers = wallHolder;
this.wallpapers2 = wallHolder;
this.c = context;
this.inflater = (LayoutInflater) this.c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return wallpapers.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stubs
WallpaperHolder item = wallpapers.get(position);
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.wallpaper_row, null);
TextView txtTitle = (TextView) vi.findViewById(R.id.textView1);
ImageButton btn = (ImageButton) vi.findViewById(R.id.deleteButtonWallpaper);
btn.setBackgroundColor(Color.TRANSPARENT);
txtTitle.setText(item.title);
final int pos = position;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wallpapers.remove(pos);
notifyDataSetChanged();
}
});
return vi;
}
公共类适配器扩展BaseAdapter{
列出壁纸;
列表2;
充气机;
上下文c;
公共墙纸适配器(上下文、列表墙夹){
这个。墙纸=墙纸架;
this.wallpers2=墙夹;
c=上下文;
this.inflater=(LayoutInflater)this.c.getSystemService(Context.LAYOUT\u inflater\u SERVICE);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回壁纸。大小();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
墙纸架项目=墙纸。获取(位置);
LayoutInflater充气机=(LayoutInflater)c
.getSystemService(上下文布局\充气机\服务);
视图vi=转换视图;
if(convertView==null)
vi=充气机。充气(R.layout.wallpaper_行,空);
TextView txtTitle=(TextView)vi.findViewById(R.id.textView1);
ImageButton btn=(ImageButton)vi.findViewById(R.id.deleteButtonWallpaper);
btn.setBackgroundColor(颜色.透明);
txtTitle.setText(项目名称);
最终int pos=位置;
btn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
壁纸。移除(pos);
notifyDataSetChanged();
}
});
返回vi;
}
}
simple row.xml
<ImageButton
android:id="@+id/deleteButtonWallpaper"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_delete"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/deleteButtonWallpaper"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:layout_toLeftOf="@+id/deleteButtonWallpaper"
android:text="TextView" />
仅当行中没有可单击的子行时,才调用onListItemClick 从代码中偷取代码
final int pos = position;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wallpapers.remove(pos);
notifyDataSetChanged();
}
});
您的视图有onClickListener,因此onListItemClick将不再工作
但对于这样的事情,有一个解决方法,您可以使整个视图都可以单击,并将位置设置为视图标记。添加
android:descendantFocusability="blocksDescendants"
到您在getView
我猜ImageButton的焦点是单击列表行。发布你的适配器代码刚刚添加了我的适配器代码post
wallpaper_row.xml
。我猜当你单击列表项时,你有一个可以聚焦的视图嘿,伙计,我临时删除了这个侦听器,但列表仍然不可点击。还有其他选项吗?@user2892143检查我的帖子并添加与相对布局相同从listView行删除可单击视图,然后删除onListItem..Work是的,它修复了它,谢谢。顺便说一句,有效的方法是onItemClick,而不是onListItemClick