我的导航栏中有一个片段,其中包含一个列表视图,该列表视图应该包含要选择的项目,但运行应用程序我会“思考屏幕”
这里是FragmentClub.java
public class FragmentClubs extends ListFragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
ListView listView;
String[] names;
final String LOG_TAG = "myLogs";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public FragmentClubs() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static FragmentClubs newInstance(String param1, String param2) {
FragmentClubs fragment = new FragmentClubs();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;}@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public void onDetach () {
super.onDetach();
mListener = null;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
super.onCreate(savedInstanceState);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.list:
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.array.typesoffood, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
});
}
这里是FragmentClubs.xml
<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"
tools:context="com.example.vlad.navig2.fragments.FragmentClubs"
android:weightSum="1"
android:clickable="true"
android:contextClickable="true"
android:nestedScrollingEnabled="true">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/texttoeat"
android:id="@+id/textView2"
android:layout_toStartOf="@+id/button2"
android:textSize="20sp"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:layout_alignParentTop="true"
android:elegantTextHeight="false"
android:textAlignment="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:id="@+id/button2"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="54dp"
android:layout_marginEnd="54dp"
android:layout_marginBottom="77dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_alignTop="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Введіть..." />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_above="@+id/button2"
android:layout_toRightOf="@+id/editText3"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:layout_below="@+id/textView2" />
答案 0 :(得分:0)
首先看到片段生命周期,您在“super.onCreate(savedInstanceState);
内部调用onActivityCreated()
”,恰好发生在onCreateView
之后和onCreate()
方法之后,这会导致您的应用落入无尽的循环,
和你没有在片段中夸大你必须在onCreateView()
中完成的布局这会导致你的片段显示默认的空列表视图而不是你想要的那个