gpt4 book ai didi

android - FragmentPagerAdapter 中的 fragment

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:50 25 4
gpt4 key购买 nike

我想要一个类中有 4 个可滚动选项卡的选项卡。我实现了以下示例并且它有效:

mAdapter = new MyAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);

...

public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS; //NUM_ITEMS=4
}
@Override
public ArrayListFragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
@Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0:
return "TAB1";
case 1:
return "TAB2";
case 2:
return "TAB3";
case 3:
return "TAB4";
default:
return "ERROR";
}
}
}

...

    public static class ArrayListFragment extends ListFragment {
int mNum;

static ArrayListFragment newInstance(int num) {
ArrayListFragment f = new ArrayListFragment();

Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);

return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
switch(mNum) {
case 0:
View v0 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v0;
case 1:
View v1 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v1;
case 2:
View v2 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v2;
case 3:
View v3 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v3;
default:
View vd = inflater.inflate(R.layout.fragment_pager_list, container, false);
return vd;
}
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Titles));
}
//Titles={"TAB1", "TAB2", "TAB3", "TAB4"}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}


}

我在每个 View 中放置的 fragment_pager_list 布局如下:

<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="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 list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it -->
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>

<!-- 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>

问题是我想在每个选项卡的每个 View 中放置不同的布局。我尝试修改代码,使“ArrayListFragment”继承自“Fragment”而不是“ListFragment”,以加载不包含“ListView”但包含“RelativeLayout”或“LinearLayout”的布局,但无法使其正常工作。

拜托,如果有人能帮助我,我需要在选项卡上加载不同的 View ,并且它们包含除 ListView 之外的其他元素。

非常感谢。

最佳答案

你最好为不同的布局制作每个 fragment 。您可以将 addFragment(Fragment fragment) 方法添加到 MyAdapter

mAdapter = new MyAdapter(getSupportFragmentManager());
for(int i=0; i<4; i++) {
Fragment fragment = null;
switch(i) {
case 0:
fragment = FirstFragment.newInstance();
break;
case 1:
fragment = SecondFragment.newInstance();
break;
case 2:
fragment = ThirdFragment.newInstance();
break;
case 3:
fragment = FourthFragment.newInstance();
break;
}
mAdapter.addFragment(fragment);
}
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);

MyAdapter:Adapter 是一个简单的容器。

public static class MyAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList;

public MyAdapter(FragmentManager fm) {
super(fm);
mFragmentList = new ArrayList<Fragment>();
}

publid void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}

@Override
public int getCount() {
return mFragmentList.size();
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public CharSequence getPageTitle(int position) {
return ((CustomFragment) mFragmentList.get(position)).getPageTitle();
}
}

CustomFragment : 放置每个 fragment 的通用方法。

public abstract class CustomFragment extends Fragment {
public abstract Fragment newInstance();
public abstract String getPageTitle();
}

FirstFragment : 为每个 fragments inflate 不同的布局。

public class FirstFragment extends CustomFragment {

@Override
public Fragment newInstance() {
Fragment f = new FirstFragment();
return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

View view = inflater.inflate(R.layout.LAYOUT_ID_FOR_FIRST_FRAGMENT, container, false);

// Some codes for layout such as findViewById

return view;
}

@Override
public String getPageTitle() {
// return TITLE_FOR_FRAGMENT
}
}

希望这个回答对您有所帮助。

关于android - FragmentPagerAdapter 中的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19027887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com