我正在尝试将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" />
答案 0 :(得分:2)
添加
android:descendantFocusability="blocksDescendants"
到您在getView
我的猜测是ImageButton专注于点击列表行。
答案 1 :(得分:0)
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将不再起作用。
但是有针对此类事情的解决方法,您可以将整个视图点击并将位置设置为视图标记。