Android中ViewPager和Fragment的使用

小案例

XML中




创建Fragment

 fragments = new ArrayList<>();
 ConversationFragment fragment1 = new ConversationFragment();
 GroupFragment fragment2 = new GroupFragment();
 SearchFragment fragment3 = new SearchFragment();
 fragments.add(fragment1);
 fragments.add(fragment2);
 fragments.add(fragment3);
 adapter = new MainPagerAdapter(getSupportFragmentManager(), fragments);
 viewPager.setAdapter(adapter);

adapter

public class MainPagerAdapter extends FragmentPagerAdapter{

  List fragmentList;

  public MainPagerAdapter(FragmentManager fm, List fragmentList) {
    super(fm);
    this.fragmentList = fragmentList;
  }

  @Override
  public Fragment getItem(int position) {
    return fragmentList.get(position);
  }

  @Override
  public int getCount() {
    return fragmentList.size();
  }
}

OnPageChangeListener

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    int distance = positionOffsetPixels / 3;
    //一旦fragment滑动,这里的position实际是前一个的
    ViewPropertyAnimator.animate(v_indicate_line).translationX(distance + position * v_indicate_line.getWidth()).setDuration(0);
  }

  @Override
  public void onPageSelected(int position) {
    textLightAndSize();
  }


  @Override
  public void onPageScrollStateChanged(int state) {

  }
});

配合其他点击事件

//这里是注意setCurrentItem的用法
switch (view.getId()) {
  case R.id.ly_conversation:
    viewPager.setCurrentItem(0);
    break;
  case R.id.ly_group:
    viewPager.setCurrentItem(1);
    break;
  case R.id.ly_search:
    viewPager.setCurrentItem(2);
    break;
}

官方案例

R.layout.fragment_pager



  
  

  
    
    
  


R.layout.fragment_pager_list



  

  
  
    
    

    
    

  



public class FragmentPagerSupport extends FragmentActivity {
  static final int NUM_ITEMS = 10;

  MyAdapter mAdapter;

  ViewPager mPager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_pager);

    mAdapter = new MyAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    // Watch for button clicks.
    Button button = (Button)findViewById(R.id.goto_first);
    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mPager.setCurrentItem(0);
      }
    });
    button = (Button)findViewById(R.id.goto_last);
    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mPager.setCurrentItem(NUM_ITEMS-1);
      }
    });
  }

  public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
      super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
      return ArrayListFragment.newInstance(position);
    }
  }

  public static class ArrayListFragment extends ListFragment {
    int mNum;

    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static ArrayListFragment newInstance(int num) {
      ArrayListFragment f = new ArrayListFragment();

      // Supply num input as an argument.
      Bundle args = new Bundle();
      args.putInt("num", num);
      f.setArguments(args);

      return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
      View tv = v.findViewById(R.id.text);
      ((TextView)tv).setText("Fragment #" + mNum);
      return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      setListAdapter(new ArrayAdapter(getActivity(),
          android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
      Log.i("FragmentList", "Item clicked: " + id);
    }
  }
}

注意

3.0之前的Activity是不能用fragment的。为了能使用fragment(supportV4中),才有了FragmentActivity。FragmentActivity继承的Activity。

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。

你可能感兴趣的:(Android中ViewPager和Fragment的使用)