0

我正在使用Tab Layout & View Pager其中包含一些視圖。ViewPager with Fragment Android(Func <T,T,T>替代Java(Android))

我爲Xamarin Android開發了項目,現在我想用Android Studio創建項目。

我已經實現了與之前的項目相同的FragmentPagerAdapter,但問題是在Fragment類中使用Func。

我的前級是這樣的:

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); 
    } 
} 

,我能夠創造片段,並添加到FragmentList這樣的:那麼

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

,你有什麼建議,以便能夠使用同樣的邏輯在我的活動?

謝謝。

+1

請勿發佈代碼圖片。將代碼發佈爲文本,使用「{}」按鈕進行格式化。 – Blorgbeard

+0

@Blorgbeard完成,謝謝。 – user3817558

回答

0

好的問題已經由我自己解決了。

基本上我已經用必要的參數實現了Func到Java。

1)創建在片段接口

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

}

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)利用類

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)設置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); 

所以這種解決方案並不常見,但使過程非常簡單。