膨胀片段类

时间:2014-05-15 18:16:29

标签: java android android-fragments

我正在尝试使用ViewPager应用程序并且我想要给类充气,我只能像这样膨胀一个xml:

@Override
public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v = null;
    switch(position){
        case 0:
            v = inflater.inflate(R.layout.lista, null);
            break;
        case 1:
            v = inflater.inflate(R.layout.dos, null);
            break;
        case 2:
            v = inflater.inflate(R.layout.tres, null);
            break;
    }
    ((ViewPager)container).addView(v, 0);

    return v;

}

但我想要这样的事情

 v = inflater.inflate(ListaFragment(), null);

这就是我的ListaFragment活动

package com.germankatz.etacerfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ListaFragment extends Fragment {

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

    TextView Numero = (TextView) getView().findViewById(R.id.textView1);
    Numero.setText("Funciono!!!!");

    return v;
   }
} 

我没有找到这样的东西,所以这就是我要问的原因。

提前致谢!

更新

我弄明白该怎么做

public class MainActivity extends FragmentActivity {
/**
 * The number of pages (wizard steps) to show in this demo.
 */
private static final int NUM_PAGES = 3;

/**
 * The pager widget, which handles animation and allows swiping horizontally to access previous
 * and next wizard steps.
 */
private ViewPager mPager;

/**
 * The pager adapter, which provides the pages to the view pager widget.
 */
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
}

@Override
public void onBackPressed() {
    if (mPager.getCurrentItem() == 0) {
        // If the user is currently looking at the first step, allow the system to handle the
        // Back button. This calls finish() on this activity and pops the back stack.
        super.onBackPressed();
    } else {
        // Otherwise, select the previous step.
        mPager.setCurrentItem(mPager.getCurrentItem() - 1);
    }
}

/**
 *  Esto sirve para que ande el click y llevar a tal pagina ((MainActivity)getActivity()).setCurrentItem (1, true); que aparece en
 *  un onclicklistener de cada fragment
 */
public void setCurrentItem (int item, boolean smoothScroll) {
    mPager.setCurrentItem(item, smoothScroll);
}

/**
 * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
 * sequence.
 */
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment b = null;

        switch(position){
        case 0:
            b = new ListaFragment();
            break;
        case 1:
            b = new MainFragment();
            break;
        case 2:
            b = new nextColeFragment();
            break;
        }
        return b;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}
}

2 个答案:

答案 0 :(得分:0)

如果您想ViewPager Fragments extendsPagerAdapterFragmentPagerAdapterFragmentStatePagerAdatperViewPager 。这是guide中显示如何使用Fragments实现{{1}}的部分。

答案 1 :(得分:0)

为您的viewpager创建一个扩展FragmentStatePagerAdapter的适配器。 Here是可能对您有帮助的链接。