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

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

问题描述

我使用了Android的开始,当你开始一个新的Andr​​oid应用程序的默认选项卡刷卡片段code。我已经想通了如何修改我的数据,并使用该卡,但我还没有想出如何简单的片段添加到什么是已经存在。我想补充一个信息栏上的选项卡上方或下方他们。我敢肯定,这很简单,我只是不知道如何笑。我学会用实例。

我试着加入了以viewpager,但我的应用程序崩溃,并且我相信我完全做错了。

在我MainActivity: ......

 保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    //设置操作栏。
    最后的动作条动作条= getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mSectionsPagerAdapter =新SectionsPagerAdapter(
            getSupportFragmentManager());

    //设置的ViewPager与部分适配器。
    mViewPager =(ViewPager)findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    SubBarFragment信息栏=新SubBarFragment();
    mViewPager.addView(infobar.getView());
..... [额外的code菜单和东西]

公共类SectionsPagerAdapter扩展FragmentPagerAdapter {

    公共SectionsPagerAdapter(FragmentManager FM){
        超(FM);
    }

    @覆盖
    公共片段的getItem(INT位置){
... [code表现出特定的标签,并返回该标签的片段]
 

infobarfragment.java

 公共类InfobarFragment扩展片段{

      @覆盖
      公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
          捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.infobar,集装箱,假);
        返回查看;
      }

}
 

activity_main.xml

 < XML版本=1.0编码=UTF-8&GT?;
< android.support.v4.view.ViewPager的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=。MainActivity/>
 

infobar.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>

    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/ ic_wifi/>

    <的TextView
        机器人:ID =@ + ID / statusText1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_horizo​​ntal
        机器人:文本=无线状态信息/>

< / LinearLayout中>
 

解决方案

您可以按照这种方式:

只是用片段在寻呼机中每个视图。

写跌破code。在的onCreate()的方法 FragmentActivity

 名单,其中,片断>碎片=新矢量<片断>();

//为每个片段要添加到寻呼机
捆绑页=新包();
page.putString(URL,网址);
fragments.add(Fragment.instantiate(这一点,MyFragment.class.getName(),页));

//添加后,所有的片段写如下的行

this.mPagerAdapter =新PagerAdapter(super.getSupportFragmentManager(),片段);

mPager.setAdapter(this.mPagerAdapter);
 

  

一个样品片段的定义:

 公共类MyFragment扩展片段{


公共静态MyFragment的newInstance(字符串IMAGEURL){

最后MyFragment MF =新MyFragment();

    最终捆绑的args =新包();
    args.putString(somedata,somedata);
    mf.setArguments(参数);

    返回MF;
}

公共MyFragment(){}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    字符串数据= getArguments()的getString(somedata)。
}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
    //充气并找到主要的ImageView
    最终的视图V = inflater.inflate(R.layout.my_fragment_view,集装箱,假);
    // ...
    返回伏;
}
 

  

FragmentPagerAdapter

 公共类MyFragmentAdapter扩展FragmentPagerAdapter {

公共静态INT POS = 0;

私人列表<片断> myFragments;

公共MyFragmentAdapter(FragmentManager FM,名单,其中,片断> myFrags){
    超(FM);
    myFragments = myFrags;
}

@覆盖
公共片段的getItem(INT位置){

    返回myFragments.get(位置);

}

@覆盖
公众诠释getCount将(){

    返回myFragments.size();
}

@覆盖
公共CharSequence的getPageTitle(INT位置){

    setPos(位置);

    字符串的PageTitle =;

    开关(POS)
    {
        情况下0:
                的PageTitle =第1页;
                打破;
        情况1:
                的PageTitle =2页;
                打破;
        案例2:
                的PageTitle =3页;
                打破;
        案例3:
                的PageTitle =4页;
                打破;
        壳体4:
                的PageTitle =第5页;
                打破;
        壳体5:
                的PageTitle =6页;
                打破;
        情况6:
                的PageTitle =7页;
                打破;
    }
    返回的PageTitle;
}

公共静态INT GETPOS(){
    返回POS机;
}

 公共静态无效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天全站免登陆