15

我正在尝试制作一个具有滑动导航类型的应用程序。这是我走了多远:

片段活动:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_control extends Fragment {

String tag = this.getClass().getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(tag, "onCreate");
    super.onCreate(savedInstanceState);
    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.i(tag, "onCreateView");

    View view = inflater.inflate(R.layout.fragment_controle,container, false ); 

    return view;
  }

  }

片段页面适配器:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

  public class Fragment_Pager extends FragmentPagerAdapter {

final int PAGE_COUNT = 3;
public Fragment_Pager(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {

    Fragment_control myFragment = new Fragment_control();
    Bundle data = new Bundle();
    data.putInt("current_page", arg0+1);
    myFragment.setArguments(data);
    return myFragment;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PAGE_COUNT;
}

}

主要活动:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

String tag = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(tag, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /** Getting a reference to the ViewPager defined the layout file */
    ViewPager pager = (ViewPager) findViewById(R.id.pager);

    /** Getting fragment manager */
    FragmentManager fm = getSupportFragmentManager();

    /** Instantiating FragmentPagerAdapter */
    Fragment_Pager pagerAdapter = new Fragment_Pager(fm);

    /** Setting the pagerAdapter to the pager object */
    pager.setAdapter(pagerAdapter);

}
}

但我的问题是我无法弄清楚如何将片段添加到代码中。我想用按钮和东西滑动到下一个活动。我怎么做 ?

4

2 回答 2

17

PageAdapter使用您getItem()在 之间切换Fragments,这是您要声明哪个页面做什么的地方。“位置”0是第一页,1是第二页,依此类推。如果您愿意,您可以简单地返回其他实例的新实例Fragments或传递参数。

例如:

@Override
public Fragment getItem(int position) {
    switch (position) {
    case 0:
        // Your current main fragment showing how to send arguments to fragment
        Fragment_control myFragment = new Fragment_control();
        Bundle data = new Bundle();
        data.putInt("current_page", position+1);
        myFragment.setArguments(data);
        return myFragment;
    case 1:
        // Calling a Fragment without sending arguments
        return new MySecondFragment();
    case 2:
        return new MyThirdFragment();
    default:
        return null;
    }
}

Fragment然后,您将为要合并的每个人创建一个类。MySecondFragment在我的示例中,您将有一个类MyThirdFragment

public class MySecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second_fragment, null); 
        return view;
    }

}

public class MyThirdFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.third_fragment, null); 
        return view;
    }

}

任何片段都可以简单地扩充一个标准的 xml 布局文件,要访问布局中的子项,您必须记住使用以下内容,我将使用以下内容MySecondFragment作为示例。

假设您有两个Buttons带有 id 的R.id.button1,并且R.id.button2在一个名为“second_fragment”的布局文件中:

public class MySecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second_fragment, null);

        // Make sure to add the parent inflater before and layout child call
        Button btn_one = (Button)view.findViewById(R.id.button1);
        Button btn_two = (Button)view.findViewById(R.id.button2);

        return view;
    }

}

编辑

要从其他页面0或第一个位置开始,您只需setCurrentItem()在 中使用MainAvticity,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(tag, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    Fragment_Pager pagerAdapter = new Fragment_Pager(fm);
    // Here you would declare which page to visit on creation
    pager.setAdapter(pagerAdapter);
    pager.setCurrentItem(1);

}
于 2013-04-04T12:55:14.030 回答
0

我在片段类中添加了这个..

我得到的问题是无法解决无法解决获取支持片段管理器()。

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(tag, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
Fragment_Pager pagerAdapter = new Fragment_Pager(fm);
// Here you would declare which page to visit on creation
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(1);

}
于 2016-11-18T06:16:17.463 回答