我的项目中有一个列表片段,当你从中选择一个项目时。它假设要更改片段,但它似乎根本没有接收到点击。我已经尝试设置后代的可聚焦性,但似乎没有任何东西可以让它甚至注册myclicks。我已经尝试过在这个网站上看过的解决方案,因为它似乎是一个常见的问题,但它们似乎都不允许我的特定程序正常工作
这是我的ListFragment:
import java.util.ArrayList;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class CharacterListFragment extends ListFragment{
ArrayList<String> charList = new ArrayList<String>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.character_list, null, false);
ListView ls = (ListView) view.findViewById(android.R.id.list);
ls.setClickable(false);
return view;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l,v,position,id);
String clickedDetail = (String) l.getItemAtPosition(position);
CharacterFragment myCharFragment = new CharacterFragment();
Bundle bundle = new Bundle();
bundle.putString("KEY_DETAIL", clickedDetail);
myCharFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.simpleFragment, myCharFragment);
fragmentTransaction.commit();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
DatabaseHelper db = new DatabaseHelper(getActivity());
db.open();
charList = db.getArrayList();
db.close();
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, charList));
}
}
我的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false" >
<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:clickable="true"
android:focusable="true">
</ListView>
</LinearLayout>
我不知道它是否简单,我只是在俯视
答案 0 :(得分:0)
public static class FragmentA extends ListFragment {
OnArticleSelectedListener mListener;
...
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Append the clicked item's row ID with the content provider Uri
Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
// Send the event and Uri to the host activity
mListener.onArticleSelected(noteUri);
}
...
这适用于listFragment
中的OnListItemClick