gpt4 book ai didi

java - onListItemClick 方法不起作用

转载 作者:行者123 更新时间:2023-12-02 06:17:48 26 4
gpt4 key购买 nike

我尝试将 SherlockListFragment 与自定义适配器一起使用,但 onListItemClick 方法不起作用。我根本无法点击列表。有人可以帮我修复我的 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的布局

<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;
}

}

简单的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" />

最佳答案

添加

 android:descendantFocusability="blocksDescendants"

到您在 getView 中扩充的 xml 中的根元素

我的猜测是 ImageButton 将焦点放在列表行的单击上。

关于java - onListItemClick 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21293268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com