如何将片段添加到 ViewPager?addView 使我的应用程序崩溃 [英] How can I add a fragment to a ViewPager? addView crashes my app

查看:21
本文介绍了如何将片段添加到 ViewPager?addView 使我的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是默认的 Swipe Tab Fragment 代码,当您启动一个新的 Android 应用程序时,Android 开始使用该代码.我已经想出了如何修改我的数据并使用选项卡,但我还没有想出如何将一个简单的片段添加到已经存在的内容中.我想在选项卡的顶部或正下方添加一个信息栏.我确定这很简单,我只是不知道如何大声笑.我通过例子学习.

我已尝试向 viewpager 添加视图,但我的应用程序崩溃了,我认为我完全做错了.

在我的主活动中:...

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//设置操作栏.最终 ActionBar actionBar = getActionBar();actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);mSectionsPagerAdapter = 新的 SectionsPagerAdapter(getSupportFragmentManager());//使用部分适配器设置 ViewPager.mViewPager = (ViewPager) findViewById(R.id.pager);mViewPager.setAdapter(mSectionsPagerAdapter);SubBarFragment infobar = new SubBarFragment();mViewPager.addView(infobar.getView());.....[菜单和东西的额外代码]公共类 SectionsPagerAdapter 扩展 FragmentPagerAdapter {公共 SectionsPagerAdapter(FragmentManager fm) {超级(调频);}@覆盖公共片段 getItem(int position) {....[显示特定标签并返回标签片段的代码]

infobarfragment.java

public class InfobarFragment extends Fragment {@覆盖公共视图 onCreateView(LayoutInflater inflater, ViewGroup 容器,捆绑savedInstanceState) {查看视图 = inflater.inflate(R.layout.infobar, container, false);返回视图;}}

activity_main.xml

信息栏.xml

<图像视图android:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_wifi"/><文本视图android:id="@+id/statusText1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizo​​ntal"android:text="Wifi 状态信息"/></LinearLayout>

解决方案

你可以按照这个方法:

只需为寻呼机中的每个视图使用片段.

FragmentActivityonCreate()方法中编写如下代码.

Listfragment = new Vector();//对于要添加到寻呼机的每个片段捆绑页面 = new Bundle();page.putString("url", url);fragment.add(Fragment.instantiate(this,MyFragment.class.getName(),page));//添加所有片段后写入以下几行this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragment);mPager.setAdapter(this.mPagerAdapter);

<块引用>

示例片段定义:

public class MyFragment extends Fragment {公共静态 MyFragment newInstance(String imageUrl) {final MyFragment mf = new MyFragment();最终捆绑 args = 新捆绑();args.putString("somedata", "somedata");mf.setArguments(args);返回 mf;}公共 MyFragment() {}@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);字符串数据 = getArguments().getString("somedata");}@覆盖公共视图 onCreateView(LayoutInflater inflater, ViewGroup 容器,捆绑savedInstanceState) {//膨胀并定位主 ImageView最终视图 v = inflater.inflate(R.layout.my_fragment_view, container, false);//...返回 v;}

<块引用>

FragmentPagerAdapter

public class MyFragmentAdapter extends FragmentPagerAdapter {公共静态 int pos = 0;私人列表<片段>我的片段;public MyFragmentAdapter(FragmentManager fm, List myFrags) {超级(调频);myFragments = myFrags;}@覆盖公共片段 getItem(int position) {返回 myFragments.get(position);}@覆盖公共 int getCount() {返回 myFragments.size();}@覆盖公共 CharSequence getPageTitle(int position) {设置位置(位置);String PageTitle = "";开关(位置){案例0:PageTitle = "第 1 页";休息;情况1:PageTitle = "第 2 页";休息;案例2:PageTitle = "第 3 页";休息;案例3:PageTitle = "第 4 页";休息;案例4:PageTitle = "第 5 页";休息;案例5:PageTitle = "第 6 页";休息;案例6:PageTitle = "第 7 页";休息;}返回页面标题;}公共静态 int getPos() {返回位置;}公共静态无效 setPos(int pos) {MyFragmentAdapter.pos = pos;}}

I'm using the default Swipe Tab Fragment code that Android starts with when you start a new Android application. I've figured out how to modify my data and use the tabs, but I haven't figured out how to add a simple fragment to what's already there. I wanted to add a infobar on the top of the tabs or right below them. I'm sure it's simple, I just can't figure out how lol. I learn by examples.

I've tried adding a view to viewpager, but my app is crashing, and I believe I'm totally doing it wrong.

In my MainActivity: ...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    SubBarFragment infobar = new SubBarFragment();
    mViewPager.addView(infobar.getView());      
.....[extra code for menus and things]

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
....[code to show specific tabs and return the tab's fragment]

infobarfragment.java

public class InfobarFragment extends Fragment {

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

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

infobar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_wifi" />

    <TextView
        android:id="@+id/statusText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Wifi Status Information" />

</LinearLayout>

解决方案

you can follow this approach:

just Use fragments for each view in a pager.

write the below code in onCreate() method of the FragmentActivity.

List<Fragment> fragments = new Vector<Fragment>();

//for each fragment you want to add to the pager
Bundle page = new Bundle();
page.putString("url", url);
fragments.add(Fragment.instantiate(this,MyFragment.class.getName(),page));

//after adding all the fragments write the below lines

this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);

mPager.setAdapter(this.mPagerAdapter);

A sample fragment definition:

public class MyFragment extends Fragment {


public static MyFragment newInstance(String imageUrl) {

final MyFragment mf = new MyFragment ();

    final Bundle args = new Bundle();
    args.putString("somedata", "somedata");
    mf.setArguments(args);

    return mf;
}

public MyFragment() {}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String data = getArguments().getString("somedata");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate and locate the main ImageView
    final View v = inflater.inflate(R.layout.my_fragment_view, container, false);
    //... 
    return v;
}

FragmentPagerAdapter

public class MyFragmentAdapter extends FragmentPagerAdapter {

public static int pos = 0;

private List<Fragment> myFragments;

public MyFragmentAdapter(FragmentManager fm, List<Fragment> myFrags) {
    super(fm);
    myFragments = myFrags;
}

@Override
public Fragment getItem(int position) {

    return myFragments.get(position);

}

@Override
public int getCount() {

    return myFragments.size();
}

@Override
public CharSequence getPageTitle(int position) {

    setPos(position);

    String PageTitle = "";

    switch(pos)
    {
        case 0:
                PageTitle = "page 1";
                break;
        case 1:
                PageTitle = "page 2";
                break;
        case 2:
                PageTitle = "page 3";
                break;
        case 3:
                PageTitle = "page 4";
                break;
        case 4:
                PageTitle = "page 5";
                break;
        case 5: 
                PageTitle = "page 6";
                break;
        case 6:
                PageTitle = "page 7";
                break;
    }
    return PageTitle;
}

public static int getPos() {
    return pos;
}

 public static void setPos(int pos) {
    MyFragmentAdapter.pos = pos;
 }
}

这篇关于如何将片段添加到 ViewPager?addView 使我的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆