Fragment 中的 ListView 未显示
ListView in Fragment not showing up
我在一个片段中使用这个 library,但是当我 运行 应用程序时,SwipeMenuListView 没有显示它的内容,在堆栈上我发现一些人在 onCreateView 中使用列表视图方法,我试过了,但没用
public class AutorizadasFragment extends Fragment {
private SwipeMenuListView listView;
private CustomArrayAdapter adapter;
private List<String> data;
public AutorizadasFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
data = new ArrayList<String>();
data.add("Teste 1");
data.add("Teste 2");
data.add("Teste 3");
data.add("Teste 4");
data.add("Teste 5");
listView = (SwipeMenuListView) getActivity().findViewById(R.id.list);
adapter = new CustomArrayAdapter(getActivity(), data);
listView.setAdapter(adapter);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "open" item
SwipeMenuItem openItem = new SwipeMenuItem(
getActivity());
// set item background
openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
0xCE)));
// set item width
openItem.setWidth(90);
// set item title
openItem.setTitle("Open");
// set item title fontsize
openItem.setTitleSize(18);
// set item title font color
openItem.setTitleColor(Color.BLACK);
// add to menu
menu.addMenuItem(openItem);
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(
getActivity());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(90);
// set a icon
deleteItem.setIcon(R.drawable.ic_exit);
// add to menu
menu.addMenuItem(deleteItem);
}
};
listView.setMenuCreator(creator);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
}
}
XML:
<LinearLayout
style="@style/FormRegister"
android:orientation="vertical"
android:weightSum="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</LinearLayout>
您可以通过添加私有属性来保存对根视图的引用
例如
private View rootView = null;
并在 onCreateView 中更改以下行:
View rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
到
rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
那你就不用"getActivity().findViewById(R.id.list);"
(它会变成 "rootView.findViewById(R.id.list);")
我提出这个建议是因为我不确定调用 onActivityCreated() 时在 onCreateView 中创建的视图是否真的添加到您的活动根视图中。
替代方法是覆盖 onViewCreated
。 onCreateView
返回的视图传递给 onViewCreated
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (SwipeMenuListView) view.findViewById(R.id.list);
}
我在一个片段中使用这个 library,但是当我 运行 应用程序时,SwipeMenuListView 没有显示它的内容,在堆栈上我发现一些人在 onCreateView 中使用列表视图方法,我试过了,但没用
public class AutorizadasFragment extends Fragment {
private SwipeMenuListView listView;
private CustomArrayAdapter adapter;
private List<String> data;
public AutorizadasFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
data = new ArrayList<String>();
data.add("Teste 1");
data.add("Teste 2");
data.add("Teste 3");
data.add("Teste 4");
data.add("Teste 5");
listView = (SwipeMenuListView) getActivity().findViewById(R.id.list);
adapter = new CustomArrayAdapter(getActivity(), data);
listView.setAdapter(adapter);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "open" item
SwipeMenuItem openItem = new SwipeMenuItem(
getActivity());
// set item background
openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
0xCE)));
// set item width
openItem.setWidth(90);
// set item title
openItem.setTitle("Open");
// set item title fontsize
openItem.setTitleSize(18);
// set item title font color
openItem.setTitleColor(Color.BLACK);
// add to menu
menu.addMenuItem(openItem);
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(
getActivity());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(90);
// set a icon
deleteItem.setIcon(R.drawable.ic_exit);
// add to menu
menu.addMenuItem(deleteItem);
}
};
listView.setMenuCreator(creator);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
}
}
XML:
<LinearLayout
style="@style/FormRegister"
android:orientation="vertical"
android:weightSum="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</LinearLayout>
您可以通过添加私有属性来保存对根视图的引用 例如
private View rootView = null;
并在 onCreateView 中更改以下行:
View rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
到
rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
那你就不用"getActivity().findViewById(R.id.list);" (它会变成 "rootView.findViewById(R.id.list);")
我提出这个建议是因为我不确定调用 onActivityCreated() 时在 onCreateView 中创建的视图是否真的添加到您的活动根视图中。
替代方法是覆盖 onViewCreated
。 onCreateView
返回的视图传递给 onViewCreated
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (SwipeMenuListView) view.findViewById(R.id.list);
}