我的应用程序在dialogFragment中有listview,当用户选择了应该Toast
的项目时,我的案例项目无法点击。
DialogueFragment
public class HistoryDialogue extends DialogFragment {
ListView list ;
Cursor c ;
HistoryAdapter adapter ;
Context context ;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.history,null);
list = (ListView)view.findViewById(R.id.listView);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
ContentResolver resolver = getActivity().getContentResolver();
c=resolver.query(Contract.SaveRunning.CONTENT_URI,null,null,null,null);
adapter= new HistoryAdapter(getActivity(),c,0);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"keep going ",Toast.LENGTH_LONG).show();
}
});
builder.setView(list);
return builder.create();
}
}
的CursorAdapter
public class HistoryAdapter extends CursorAdapter {
public HistoryAdapter(Context context, Cursor c, int flags) {
super(context,c, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.historylist, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView text = (TextView) view.findViewById(R.id.text);
String body = cursor.getString(cursor.getColumnIndex("name"));
text.setText(body);
}
}
history.xml
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_gravity="center_horizontal" />
historylist.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text"
android:layout_margin="5dp"
android:layout_weight="1"/>
<Button
android:layout_width="30dp"
android:layout_height="35dp"
android:background="@drawable/delete"
android:id="@+id/btn"
android:layout_margin="10dp"
android:layout_weight="0"
/>
答案 0 :(得分:1)
将此行添加到CursorAdapter的bindView:
((ViewGroup)row).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
如果不够,请将clickable="false"
添加到historylist.xml
按钮