Android 如何在实例化片段时传递数据,因为我可以';不重写构造函数吗?

Android 如何在实例化片段时传递数据,因为我可以';不重写构造函数吗?,android,android-fragments,android-adapter,Android,Android Fragments,Android Adapter,我有一个FragmentPageAdapter,我希望它加载具有不同值的相同片段,而不是不同的片段。 这个值只是一个字符串。如何传递它?一个简单的示例,介绍如何传递: // in the adapter private static final String[] VALUES = { "string 1", "string 2", "string 3", "string 4", "string 5", }; @Override public Fragm

我有一个FragmentPageAdapter,我希望它加载具有不同值的相同片段,而不是不同的片段。
这个值只是一个字符串。如何传递它?

一个简单的示例,介绍如何传递:

// in the adapter
private static final String[] VALUES = { 
   "string 1", 
   "string 2", 
   "string 3", 
   "string 4", 
   "string 5",  };

@Override
public Fragment getItem(int position) {
    MyFrag f = new MyFrag();
    Bundle b = new Bundle();
    b.putString("value", VALUES[position]);
    f.setArguments(b);
    return f;
}

// then in the fragment 
private String value;
public void onCreate(Bundle savedInstanceState) {
   value = getArguments.getString("value");
}

下面是一个简单的示例:

// in the adapter
private static final String[] VALUES = { 
   "string 1", 
   "string 2", 
   "string 3", 
   "string 4", 
   "string 5",  };

@Override
public Fragment getItem(int position) {
    MyFrag f = new MyFrag();
    Bundle b = new Bundle();
    b.putString("value", VALUES[position]);
    f.setArguments(b);
    return f;
}

// then in the fragment 
private String value;
public void onCreate(Bundle savedInstanceState) {
   value = getArguments.getString("value");
}

下面是一个简单的示例:

// in the adapter
private static final String[] VALUES = { 
   "string 1", 
   "string 2", 
   "string 3", 
   "string 4", 
   "string 5",  };

@Override
public Fragment getItem(int position) {
    MyFrag f = new MyFrag();
    Bundle b = new Bundle();
    b.putString("value", VALUES[position]);
    f.setArguments(b);
    return f;
}

// then in the fragment 
private String value;
public void onCreate(Bundle savedInstanceState) {
   value = getArguments.getString("value");
}

下面是一个简单的示例:

// in the adapter
private static final String[] VALUES = { 
   "string 1", 
   "string 2", 
   "string 3", 
   "string 4", 
   "string 5",  };

@Override
public Fragment getItem(int position) {
    MyFrag f = new MyFrag();
    Bundle b = new Bundle();
    b.putString("value", VALUES[position]);
    f.setArguments(b);
    return f;
}

// then in the fragment 
private String value;
public void onCreate(Bundle savedInstanceState) {
   value = getArguments.getString("value");
}

您应该为它使用
Bundle
(片段参数)。见示例:

//put yours info to Bundle
Bundle bundl = new Bundle();
bundl.putString("key", "value");
//set bundle to frgments arguments
YoursFragClass frag = new YoursFragClass();
frag.setArguments(bundl);
//add fragment to activity
...
Bundle
中您的信息将显示为
Fragments
livecycle方法的参数

//then in fragments `onCreate()` or `onCreateView()` you receive this Bundle as argument
public class YoursFragClass extends Fragment {

     public View onCreateView(LayoutInflater inflater,
         ViewGroup containerObject,
         Bundle savedInstanceState){
         //here is your arguments
         Bundle bundle=getArguments(); 

        //here is yours info
        String myString = bundle.getString("key");
        System.out.println(myString); //will show "value" in logCat
     }
}

您应该为它使用
Bundle
(片段参数)。见示例:

//put yours info to Bundle
Bundle bundl = new Bundle();
bundl.putString("key", "value");
//set bundle to frgments arguments
YoursFragClass frag = new YoursFragClass();
frag.setArguments(bundl);
//add fragment to activity
...
Bundle
中您的信息将显示为
Fragments
livecycle方法的参数

//then in fragments `onCreate()` or `onCreateView()` you receive this Bundle as argument
public class YoursFragClass extends Fragment {

     public View onCreateView(LayoutInflater inflater,
         ViewGroup containerObject,
         Bundle savedInstanceState){
         //here is your arguments
         Bundle bundle=getArguments(); 

        //here is yours info
        String myString = bundle.getString("key");
        System.out.println(myString); //will show "value" in logCat
     }
}

您应该为它使用
Bundle
(片段参数)。见示例:

//put yours info to Bundle
Bundle bundl = new Bundle();
bundl.putString("key", "value");
//set bundle to frgments arguments
YoursFragClass frag = new YoursFragClass();
frag.setArguments(bundl);
//add fragment to activity
...
Bundle
中您的信息将显示为
Fragments
livecycle方法的参数

//then in fragments `onCreate()` or `onCreateView()` you receive this Bundle as argument
public class YoursFragClass extends Fragment {

     public View onCreateView(LayoutInflater inflater,
         ViewGroup containerObject,
         Bundle savedInstanceState){
         //here is your arguments
         Bundle bundle=getArguments(); 

        //here is yours info
        String myString = bundle.getString("key");
        System.out.println(myString); //will show "value" in logCat
     }
}

您应该为它使用
Bundle
(片段参数)。见示例:

//put yours info to Bundle
Bundle bundl = new Bundle();
bundl.putString("key", "value");
//set bundle to frgments arguments
YoursFragClass frag = new YoursFragClass();
frag.setArguments(bundl);
//add fragment to activity
...
Bundle
中您的信息将显示为
Fragments
livecycle方法的参数

//then in fragments `onCreate()` or `onCreateView()` you receive this Bundle as argument
public class YoursFragClass extends Fragment {

     public View onCreateView(LayoutInflater inflater,
         ViewGroup containerObject,
         Bundle savedInstanceState){
         //here is your arguments
         Bundle bundle=getArguments(); 

        //here is yours info
        String myString = bundle.getString("key");
        System.out.println(myString); //will show "value" in logCat
     }
}

你应该为此使用参数

它们表示为一个
,并通过
setArguments()
进行设置。请记住,它们只能在片段连接到活动之前设置

为方便起见,您可以为片段创建工厂方法,以隐藏创建捆绑包的复杂性:

public class MyFragment extends Fragment {

    public static MyFragment getInstance(<the instantiation parameters here>) {

        // check parameter preconditions
        final Bundle arguments = new Bundle();
        // put parameters into the bundle
        final MyFragment instance = new MyFragment();
        instance.setArguments(arguments);
        return instance;
    }
}
公共类MyFragment扩展了片段{
公共静态MyFragment getInstance(){
//检查参数前提条件
最终Bundle参数=新Bundle();
//将参数放入包中
最终MyFragment实例=新建MyFragment();
setArguments(参数);
返回实例;
}
}

使用参数的另一个好处是,如果正在重新创建片段实例,则会保留参数。

您应该为此使用参数

它们表示为一个
,并通过
setArguments()
进行设置。请记住,它们只能在片段连接到活动之前设置

为方便起见,您可以为片段创建工厂方法,以隐藏创建捆绑包的复杂性:

public class MyFragment extends Fragment {

    public static MyFragment getInstance(<the instantiation parameters here>) {

        // check parameter preconditions
        final Bundle arguments = new Bundle();
        // put parameters into the bundle
        final MyFragment instance = new MyFragment();
        instance.setArguments(arguments);
        return instance;
    }
}
公共类MyFragment扩展了片段{
公共静态MyFragment getInstance(){
//检查参数前提条件
最终Bundle参数=新Bundle();
//将参数放入包中
最终MyFragment实例=新建MyFragment();
setArguments(参数);
返回实例;
}
}

使用参数的另一个好处是,如果正在重新创建片段实例,则会保留参数。

您应该为此使用参数

它们表示为一个
,并通过
setArguments()
进行设置。请记住,它们只能在片段连接到活动之前设置

为方便起见,您可以为片段创建工厂方法,以隐藏创建捆绑包的复杂性:

public class MyFragment extends Fragment {

    public static MyFragment getInstance(<the instantiation parameters here>) {

        // check parameter preconditions
        final Bundle arguments = new Bundle();
        // put parameters into the bundle
        final MyFragment instance = new MyFragment();
        instance.setArguments(arguments);
        return instance;
    }
}
公共类MyFragment扩展了片段{
公共静态MyFragment getInstance(){
//检查参数前提条件
最终Bundle参数=新Bundle();
//将参数放入包中
最终MyFragment实例=新建MyFragment();
setArguments(参数);
返回实例;
}
}

使用参数的另一个好处是,如果正在重新创建片段实例,则会保留参数。

您应该为此使用参数

它们表示为一个
,并通过
setArguments()
进行设置。请记住,它们只能在片段连接到活动之前设置

为方便起见,您可以为片段创建工厂方法,以隐藏创建捆绑包的复杂性:

public class MyFragment extends Fragment {

    public static MyFragment getInstance(<the instantiation parameters here>) {

        // check parameter preconditions
        final Bundle arguments = new Bundle();
        // put parameters into the bundle
        final MyFragment instance = new MyFragment();
        instance.setArguments(arguments);
        return instance;
    }
}
公共类MyFragment扩展了片段{
公共静态MyFragment getInstance(){
//检查参数前提条件
最终Bundle参数=新Bundle();
//将参数放入包中
最终MyFragment实例=新建MyFragment();
setArguments(参数);
返回实例;
}
}
使用参数的另一个好处是,如果正在重新创建片段实例,则会保留参数