2014-01-22 52 views
0

我試圖使用SherlockListFragment與自定義的適配器和onListItemClick方法不起作用。我根本無法點擊列表。有人可以幫我修理我的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

發佈您的適配器代碼 – Raghunandan

+0

剛剛添加了我的適配器代碼 – user2892143

+0

post'wallpaper_row.xml'。我猜你有一個視圖,當你點擊列表項 – Raghunandan

回答

2

添加

android:descendantFocusability="blocksDescendants" 

在XML您在getView

膨脹的根元素我的猜測是ImageButton的需要着眼於列表行的點擊。

+0

是的,它修復它,感謝man.and順便說一下,工作的方法是onItemClick而不是onListItemClick – user2892143

0

如果行已沒有可點擊的孩子在裏面onListItemClick只叫。從代碼

代碼snipt

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將不再工作。

但是,有這樣的事情的解決方法,你使整個視圖可點擊並將位置設置爲視圖標籤。

+0

嘿人,我暫時刪除這個監聽器,但仍列表是unclickable.any其他選項? – user2892143

+0

複選框,Radiobutton,ImageButton,按鈕是默認點擊... –

+0

@ user2892143檢查我的帖子,並將其添加到相對佈局 – Raghunandan