2015-06-24 26 views
0

我使用SlideMenu,我已經實現了Fragment和ListView,我現在需要的是檢測用戶從幻燈片菜單中點擊了哪一行。我怎麼可能做到這一點? 這是列表視圖的代碼:如何檢測ListView行點擊使用SlideMenu

public class listFragment extends ListFragment { 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.menu, null); 
} 

public void onActivityCreated(Bundle savedInstanceState) { 
super.onActivityCreated(savedInstanceState); 
    SampleAdapter adapter = new SampleAdapter(getActivity()); 
    adapter.add(new SampleItem("menu left A", android.R.drawable.ic_menu_search)); 
    adapter.add(new SampleItem("menu left B", android.R.drawable.ic_menu_search)); 
    adapter.add(new SampleItem("menu left C", android.R.drawable.ic_menu_search)); 
    adapter.add(new SampleItem("menu left D", android.R.drawable.ic_menu_search)); 
    adapter.add(new SampleItem("menu left A", android.R.drawable.ic_menu_search)); 
    setListAdapter(adapter); 

} 

private class SampleItem { 
    public String tag; 
    public int iconRes; 
    public SampleItem(String tag, int iconRes) { 
     this.tag = tag; 
     this.iconRes = iconRes; 
    } 
} 

public class SampleAdapter extends ArrayAdapter<SampleItem> { 

    public SampleAdapter(Context context) { 
     super(context, 0); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null); 
     } 
     ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon); 
     icon.setImageResource(getItem(position).iconRes); 
     TextView title = (TextView) convertView.findViewById(R.id.row_title); 
     title.setText(getItem(position).tag); 

     return convertView; 
    } 

    } 
    } 
+0

@HovercraftFullOfEels你們後悔加入了android標籤 – Signo

+0

請檢查我的更新答案 –

回答

0

我用下面的方法,而無需更改代碼和它的工作:

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 

    Toast.makeText(getActivity(), "Row clicked", 
       Toast.LENGTH_LONG).show(); 
} 
1

據我所知,方法

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
return inflater.inflate(R.layout.menu, null); 

將返回的ListView對象。所以你可以調用setOnItemClickListener()方法。例如:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
ListView list = (ListView) inflater.inflate(R.layout.menu, null); 
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // write your click handler here 
    } 
}); 
return list; 
+0

的代碼不給任何錯誤,但它不工作,我想裏面設置一個斷點onItemClick,但是當我點擊一行時,它不會被調用 – Signo

+0

相反,嘗試在getView中添加一個點擊監聽器到View(convertView)。任何視圖可以有一個clickistener,並應該工作,而無需實現onItemClick –

0

覆蓋您的listfragment的onListItemClick()。

@Override 
     public void onListItemClick(ListView l, View v, int position, long id) { 
      super.onListItemClick(l, v, position, id); 
      //here "position" is clicked item position 
      } 

希望這對你有幫助。

+0

問題是,我無法檢索listView – Signo

1

ListFragment都默認了自己的ListView,但似乎要覆蓋缺省實現您的自定義功能onCreateView。你需要確定這個佈局(R.layout.menu)裏面有ListView並且它的id被設置爲android.R.id.list。然後,通過調用ListFragment.getListView(),您應該能夠在onCreateView中找到對該列表的引用,方法是在片段內的任何位置查找按ID或稍後的視圖。

然後,您可以:

  1. 覆蓋public void onListItemClick (ListView l, View v, int position, long id)應該自動爲您
  2. 得到參考ListView和在其上手動設置setOnItemClickListener(AdapterView.OnItemClickListener listener)被調用(只是一定要設置其整個佈局膨大和可用之後你)

如果不會爲你工作,你也應該把這裏R.layout.menu內容,以幫助別人幫你:)

最好的問候,

Darek