简体   繁体   English

带Fragment Android的ViewPager(Func <T,T,T> Java(Android)中的替代方法)

[英]ViewPager with Fragment Android ( Func<T,T,T> alternative in Java (Android))

I am working with Tab Layout & View Pager which contains some views. 我正在使用“标签布局和视图分页器”,其中包含一些视图。

I've developed project for Xamarin Android and now I want to create with Android Studio. 我已经为Xamarin Android开发了项目,现在我想使用Android Studio创建项目。

I've implemented FragmentPagerAdapter same as former project, but problem is using Func in Fragment class. 我已经实现了与以前的项目相同的FragmentPagerAdapter,但是问题是在Fragment类中使用Func。

My former class were like this : 我以前的课是这样的:

public class SelectDateViewFragment : Android.Support.V4.App.Fragment
{
    readonly Func<LayoutInflater, ViewGroup, Bundle, View> view;

    public SelectDateViewFragment(Func<LayoutInflater, ViewGroup, Bundle, View> _view)
    {
        view = _view;
    }

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
        base.OnCreateView(inflater, container, savedInstanceState);
        return view(inflater, container, savedInstanceState);
    }
}

And I was able to create fragment and add to FragmentList like this : 我能够创建片段并添加到FragmentList中,如下所示:

viewPagerAdapter.addFragmentView((i, v, b) =>
              {
                  var view = i.Inflate(Resource.Layout.SelectDateRecyclerViewLayout, v, false);

So, What would you recommend to be able to use same logic in my Activity ? 那么,您建议在我的Activity中使用相同的逻辑是什么?

Thanks. 谢谢。

Okay problem's been solved by myself. 好的问题已经由我自己解决了。

Basically I've implemented Func to Java with necessary parameters. 基本上,我已经用必要的参数实现了Func to Java。

1) Creating an Interface 1)创建接口

public interface Func<B,N,K, View> {
View invoke(B arg1,N arg2,K arg3);

} }

2) Implementing Interface 2)实现接口

class FuncImpl<B,N,K, View> implements Func<B,N,K, View> {
Func<B,N,K, View> function;
public FuncImpl(Func<B,N,K, View> function) {
    this.function = function;
}
@Override
public View invoke(B arg1,N arg2,K arg3) {
    return this.function.invoke(arg1, arg2, arg3);
}}

3) Using class in Fragment 3)在Fragment中使用类

public class SelectDateViewFragment extends Fragment {

Func<LayoutInflater,ViewGroup,Bundle,View> view;

public SelectDateViewFragment( Func<LayoutInflater,ViewGroup,Bundle,View> view){
    this.view = view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    return view.invoke(inflater,container,savedInstanceState);

}}

4) Setting ViewPager Adapter 4)设置ViewPager适配器

 selectDateAdapter = new SelectDateAdapter(getSupportFragmentManager(),charSequences);

    selectDateAdapter.addFragmentView(new Func<LayoutInflater, ViewGroup, Bundle, View>() {
        @Override
        public View invoke(LayoutInflater arg1, ViewGroup arg2, Bundle arg3) {
            View view = arg1.inflate(R.layout.layout_select_date_recyclerview,arg2,false);
            return view;
        }
    });

    viewPagerView.setAdapter(selectDateAdapter);

So this solution was uncommon but makes process quite simple. 因此,这种解决方案并不常见,但是使过程非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM