Android FragmentStatePagerAdapter示例

时间:2015-08-05 20:11:37

标签: android fragmentstatepageradapter

我正在尝试实现此示例http://developer.android.com/intl/es/reference/android/support/v4/app/FragmentStatePagerAdapter.html这是代码

//FragmentStatePagerSupport.java
package com.mycompany.fragmentslide;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentStatePagerSupport extends Activity {
  static final int NUM_ITEMS = 10;

  MyAdapter mAdapter;

  ViewPager mPager;

  private static String[] cheeses = {"American", "Cheddar", "Jack", "Gamonedo", "Lancashire", "Limburger", "Pepperjack", "Skyr", "Feta", "Asiago"};

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

    mAdapter = new MyAdapter(getFragmentManager());

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

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

  public static class MyAdapter extends FragmentStatePagerAdapter {
    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<String>(getActivity(), android.R.layout.simple_list_item_1, cheeses));
    }

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

//fragment_pager.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:padding="4dip"
              android:gravity="center_horizontal"
              android:layout_width="match_parent" android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

    <LinearLayout android:orientation="horizontal"
                  android:gravity="center" android:measureWithLargestChild="true"
                  android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:layout_weight="0">
        <Button android:id="@+id/goto_first"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="@string/first">
        </Button>
        <Button android:id="@+id/goto_last"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="@string/last">
        </Button>
    </LinearLayout>
</LinearLayout>

//fragment_pager_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@android:drawable/gallery_thumb">

    <TextView android:id="@+id/text"
              android:layout_width="match_parent" android:layout_height="wrap_content"
              android:gravity="center_vertical|center_horizontal"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/hello_world"/>

    <!-- The frame layout is here since we will be showing either
    the empty view or the list view.  -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
        <!-- Here is the list. Since we are using a ListActivity, we
             have to call it "@android:id/list" so ListActivity will
             find it -->
        <ListView android:id="@android:id/list"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:drawSelectorOnTop="false"/>

        <!-- Here is the view to show if the list is emtpy -->
        <TextView android:id="@android:id/empty"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:text="No items."/>

    </FrameLayout>

</LinearLayout>
MyAdapter中的MyAdapter(android.support.v4.app.FragmentManager)无法应用于android.app.FragmentManager

这是我的gradle配置

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.mycompany.fragmentslide"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:support-v4:+'
}

我尝试更改gradle依赖项,但我无法使其工作。是否有其他方式或更新的方法来做到这一点。

1 个答案:

答案 0 :(得分:0)

您使用android.app.FragmentManager中的fragmentManager和android.support.v4.app.Fragment。您应import android.support.v4.app.FragmentManager并删除import android.app.FragmentManager此外,您必须将mAdapter = new MyAdapter(getFragmentManager());替换为mAdapter = new MyAdapter(getSupportFragmentManager());