0

我有一个片段,它拥有一个视图寻呼机。 视图寻呼机的每一页都包含一个片段。查看寻呼机 - 获得每页的寻呼机号码

是否有可能获得内容片段中当前页面 的索引号?

或者我必须在创建时传递每个片段的编号吗?

或者我必须使用片段的静态“newInstance”方法来传递数据吗?

+1

使用setArgument来分割你可以实现这个 –

回答

1

实例化使用静态工厂方法视图寻呼机/子片段

private String fragmentId= ""; 

/** 
    * The argument keys for creating a fragment. 
    */ 
private static final String ARG_ONE = "arg1"; 

/** 
    * Factory method for this fragment class. Constructs a new fragment with the given arguments. 
    */ 
    public static MyFragment create(String fragmentId) { 
      MyFragment tab = new MyFragment(); 
      Bundle bundle = new Bundle(1); 
      bundle.putString(ARG_ONE, fragmentId); 
      tab.setArguments(bundle); 
      return tab; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.tab_home_screen, container, false); 
     this.fragmentId=getArguments().getString(ARG_ONE); 
     return view; 
    } 

稍后,您可以使用fragmentId了解片段。

0

你可以通过设置标记,即可片段做这个或标记设置为任何片段的视图对象,并得到您的片段里面