Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – ViewPager显示碎片的麻烦大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用ViewPager显示带有三个选项卡的片段.最初我曾经使用FragmentMgr从Activity中实例化片段,这很好用.当我使用ViewPager转换此导航时,不再显示此片段.

@H_628_5@mainActivity.java

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

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

我在上面的代码中尝试了这个更改以使用ViewPager,如下所示:

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

其中LaunchPageAdapter@L_618_5@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
    publi@R_618_8592@ 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://scheR_66_11845@as.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://scheR_66_11845@as.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被@L_618_5@,它不会被显示.

本图文内容来源于网友网络收集整理提供,作为学习参使用,版权属于原作者。

大佬总结

以上是大佬教程为你收集整理的android – ViewPager显示碎片的麻烦全部内容,希望文章能够帮你解决android – ViewPager显示碎片的麻烦所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。