我最终试图找到解决这个问题的方法。我无法使OnItemClickListener与我的listview一起工作。虽然类似的代码与gridview完美配合。这是我的代码。
public class Songs extends ListFragment implements /*AdapterView.OnItemClickListener,*/ LoaderManager.LoaderCallbacks<Cursor> {
SongsAdapter nAdapter;
private static final String ARG_POSITION = "position";
private int position;
public static Songs newInstance(int position) {
Songs f = new Songs();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View myFragmentView = inflater.inflate(R.layout.songs, container, false);
nAdapter = new SongsAdapter(getActivity(), null);
setListAdapter(nAdapter);
/* listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
});*/
return myFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
//ListView listView = (ListView) getActivity().findViewById(android.R.id.list);
//ListView listView = (ListView) getActivity().findViewById(android.R.id.list);
FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
fab.attachToListView(getListView());
//getListView().setOnItemClickListener(this);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "Clicked " + position, Toast.LENGTH_SHORT).show();
}
/*
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
// startActivity(new Intent(getActivity(), MusicPlayer.class).putExtra("position", position).putExtra("orderby", orderby).putExtra("selection", selection).putExtra("val", val));
}
*/
我已经尝试了一切,包括实现OnItemClickListener来覆盖OnListItemClick。但绝对没有任何效果。
这是片段的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/content_frame"
android:layout_height="match_parent"
android:background="@color/metalList">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@drawable/listdivider"
android:dividerHeight="0.5dp"
android:layout_gravity="right|top"
android:clickable="false"
android:focusable="false"
android:fadeScrollbars="true"
android:fastScrollEnabled="true"
android:scrollbarStyle="insideInset"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:listSelector="@drawable/songlist_selector"/>
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="26dp"
android:layout_marginBottom="16dp"
android:src="@drawable/shuffle_48"
fab:fab_colorNormal="@color/mm1"
fab:fab_colorPressed="@color/mm2"
fab:fab_shadow="true"
/>
</FrameLayout>
答案 0 :(得分:0)
试试这个
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Item clicked", Toast.LENGTH_SHORT).show();
}
});
希望它有效:)
答案 1 :(得分:0)
您的OnItemClickListener()
如下
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int itm=arg0.getItemAtPosition(arg2);
switch (itm) {
case 0:
Toast.makeText(m_context, "Item clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(getActivity(), SecondActivity.class);
startActivity(intent1);
break;
}
});