我在片段中使用了这个http://www.knockmeout.net/2013/01/simple-editor-pattern-knockout-js.html,但是当我运行应用程序时,SwipeMenuListView没有显示其内容,在堆栈中我发现有些人在onCreateView方法中使用listview,我尝试了但是它不起作用
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>
答案 0 :(得分:4)
您可以通过添加私有属性来保存对根视图的引用 e.g。
private View rootView = null;
并更改onCreateView中的以下行:
View rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
到
rootView = inflater.inflate(R.layout.fragment_autorizadas, container, false);
然后你不需要使用&#34; getActivity()。findViewById(R.id.list);&#34; (它将成为&#34; rootView.findViewById(R.id.list);&#34;)
我建议这样做是因为我不确定在调用onActivityCreated()时,onCreateView中创建的视图是否实际上已添加到您的活动根视图中。