Java 如何在不使用列表的情况下使用FragmentStatePagerSupport?
嗨,我遵循这个例子,我让它工作。 但问题是,我没有要显示的列表,所以我想知道如何修改函数,使它们不需要xml中的列表元素来工作 我希望我的“R.layout.fragment\u pager\u list”看起来像这样Java 如何在不使用列表的情况下使用FragmentStatePagerSupport?,java,android,Java,Android,嗨,我遵循这个例子,我让它工作。 但问题是,我没有要显示的列表,所以我想知道如何修改函数,使它们不需要xml中的列表元素来工作 我希望我的“R.layout.fragment\u pager\u list”看起来像这样 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="mat
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/gallery_thumb">
<TextView android:id="@+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/hello_world"/>
<!-- The frame layout is here since we will be showing either
the empty view or the list view. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="No items."/>
</FrameLayout>
</LinearLayout>
也就是说,不必有
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>
在里面。如何修改
public static class ArrayListFragment extends ListFragment {
int mNum;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static ArrayListFragment newInstance(int num) {
ArrayListFragment f = new ArrayListFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
公共静态类ArrayListFragment扩展了ListFragment{
国际货币基金组织;
/**
*创建CountingFragment的新实例,提供“num”
*作为论据。
*/
静态ArrayListFragment newInstance(int num){
ArrayListFragment f=新的ArrayListFragment();
//提供num输入作为参数。
Bundle args=新Bundle();
args.putInt(“num”,num);
f、 设置参数(args);
返回f;
}
/**
*创建时,从实例的参数中检索该实例的编号。
*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mNum=getArguments()!=null?getArguments().getInt(“num”):1;
}
/**
*片段的UI只是一个简单的文本视图,显示其
*实例号。
*/
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图v=充气机。充气(R.layout.fragment\u pager\u list,container,false);
查看电视=v.findviewbyd(R.id.text);
(文本视图)tv.setText(“片段”+mNum);
返回v;
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(新的ArrayAdapter(getActivity()),
android.R.layout.simple_list_item_1,Cheeses.sCheeseStrings));
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
Log.i(“碎片列表”,“单击的项目:”+id);
}
}
}
为了实现这一点?如果您不想要列表,请不要对页面使用
ListFragment
。使用自己的页面UI创建自己的片段
。例如,它使用片段,每个片段承载一个EditText
小部件