Reputation: 21
I'm trying to use SherlockListFragment
with a custom made adapter and the onListItemClick method isn't working. I can't click the list at all. Can someone help me fix my onListItemClick
method?
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();
}
The layout of the wallpaper 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" />
the adapter code
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;
}
}
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" />
Upvotes: 0
Views: 74
Reputation: 133560
Add
android:descendantFocusability="blocksDescendants"
to the root element in the xml that you inflate in getView
My guess is ImageButton takes focus on click of list row.
Upvotes: 2
Reputation: 686
onListItemClick only called if row has not click-able child in it.
Code snipt from code
final int pos = position;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wallpapers.remove(pos);
notifyDataSetChanged();
}
});
Your view has onClickListener, so onListItemClick will not work any more.
But There is workaround for such things, you make whole view click-able and set position as Tag of view.
Upvotes: 0