gpt4 book ai didi

android - ViewPager 显示 fragment 的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:43 25 4
gpt4 key购买 nike

我正在尝试使用 ViewPager 获取显示三个选项卡的 fragment 。最初我曾经使用 FragmentMgr 从 Activity 中实例化 fragment ,这工作正常。当我使用 ViewPager 转换这个导航时,这个 Fragment 不再显示。

主 Activity .java

当我像这样启动 Fragment 时,它会显示出来。例如:

LaunchListFragment fragment = new LaunchListFragment();
fragment.newInstance(LIST_TYPE);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container,
fragment).commit();

我尝试对上面的代码进行此更改以如下方式使用 ViewPager:

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new LaunchPageAdapter(getSupportFragmentManager(),LIST_TYPE));
mViewPager.setCurrentItem(0);

LaunchPageAdapter 调用 LaunchListFragment 的地方。

LaunchPageAdapter.java

public class LaunchPageAdapter extends FragmentPagerAdapter {
private static String select="";

public LaunchPageAdapter(FragmentManager fm, String type) {
super(fm);
select=type;
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return LaunchListFragment.newInstance(select);
case 1:
return AddTeamFragment.newInstance();

}
return null;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}

这里是LaunchListFragment.java

public class LaunchListFragment extends ListFragment implements ActionBar.TabListener {
public static String LIST_TYPE = "invalid";
GenericListData g_data[] = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View result = inflater.inflate(R.layout.fragment_launch_list,
container, false);

setActionTabs();
setList();
return (result);
}


public static Fragment newInstance(String type) {
Fragment frag = new LaunchListFragment();
Bundle args = new Bundle();
LIST_TYPE=type;
args.putString(LIST_TYPE, type);
frag.setArguments(args);
return (frag);
}

这是 MainActivity 使用的 main.xml 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools= "match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >

<fragment
android:id="@+id/item_list"
android:name="com.teammanager.ItemListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />

<FrameLayout
android:id="@+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</FrameLayout>

LaunchListFragment使用的fragment_launch_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" >
</ListView>

<TextView
android:id="@+id/itemName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="22dp"
android:textStyle="bold" />

当我调试它时,我可以看到 LaunchListFragment 正在被实例化,onCreate 和 onCreate View 被调用,只是不会显示。 :(

如果我在我的 ViewPager 实现中遗漏了什么,这里的任何人都可以告诉我吗?

更新

当我在不使用 Viewpager 的情况下调用 LaunchListFragment 时,我首先设置内容 View 并传递 R.id.item_detail_container,这是我希望显示 fragment 的 FrameLayout 的 ID。请引用前面的main.xml

 setContentView(R.layout.main.xml);
LaunchListFragment fragment = new LaunchListFragment();
fragment.newInstance(LIST_TYPE);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container,
fragment).commit();

当我将其更改为使用 ViewPager 时,我不确定要将 Fragment 显示在何处。在我的 onView fragment 中,我正在膨胀 fragment_launch_list 并返回 View 。但是 View 寻呼机如何知道在哪里设置 fragment View 。它在 id pager 里面吗?

我是一个绝对的初学者,如果所有这些都是幼稚的问题,我深表歉意。

干杯。

最佳答案

问题描述非常适合我刚遇到的问题。我知道这个问题很老,但这可能会帮助其他面临同样问题的人。 @Geoplex 没有显示 LaunchPageAdapter 类的代码。所以我不确定这是否能解决他的问题。但对我来说这很管用。

在我的 PagerAdapter 中,public boolean isViewFromObject(View view, Object object) 被覆盖。我删除了该方法并允许 superClass 处理它。这开始给出我预期的结果。

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

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