简体   繁体   中英

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.

I've implemented FragmentPagerAdapter same as former project, but problem is using Func in Fragment class.

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 :

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 ?

Thanks.

Okay problem's been solved by myself.

Basically I've implemented Func to Java with necessary parameters.

1) Creating an Interface

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

}

2) Implementing Interface

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

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

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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