0

I want to slide acivities with view pager not by gesture Listener in android. I am able to add view pager on image view but not on activities so please help me ... Thanks in advance!

4

3 回答 3

3

为寻呼机中的每个视图使用片段。

将下面的代码写onCreate()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);

示例片段定义:

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;
 }
}
于 2013-04-22T11:31:25.127 回答
1

ViewPager只有持有viewsactivity可选地支持fragments,持有activity另一个activity被正式贬值。

于 2013-04-22T11:42:58.250 回答
1

像这样使用 overridePendingTransition

startActivity(YourNewActivity);
overridePendingTransition(R.anim.right_to_left,
                            R.anim.left_to_right);

right_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="300"
        android:fromXDelta="100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />

</set>

left_to_right.xml

<translate
    android:duration="300"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="-100%"
    android:toYDelta="0%" />

于 2013-04-22T11:34:15.640 回答