Xamarin.android - 将数据传递给我的片段

时间:2017-11-12 21:42:11

标签: android android-fragments xamarin xamarin.android

我有一个包含我的服务的列表,并且在选择的项目上我将服务类型传递给我的活动ServiceDetail,如下所示:

ServiceActivity

void item_selected(object sender, AdapterView.ItemClickEventArgs e) {
    MenuContentItem selectedItem = (MenuContentItem)item[e.Position];
    if(selectedItem.Title == "COLLO") {
        var activity_go = new Intent(this, typeof(ServiceDetailActivity));
        activity_go.PutExtra("service_type", "Collo");
        StartActivity(activity_go);
    }
    if (selectedItem.Title == "SPALLA") {
        var activity_go = new Intent(this, typeof(ServiceDetailActivity));
        activity_go.PutExtra("service_type", "Spalla");
        StartActivity(activity_go);
    }
}

ServiceDetailActivity

protected override void OnCreate(Bundle savedInstanceState) {
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.ServiceDetail);
    //enable navigation mode to support tab layout
    this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
    AddTab("Introduzione", Resource.Mipmap.Icon, new IntroduzioneFragment());
    //intent data 
    string text = Intent.GetStringExtra("service_type") ?? "Data not available";
    IntroduzioneFragment fragment = new IntroduzioneFragment();
    // set data to pass to my fragment
    Bundle bundle = new Bundle();
    bundle.PutString("text", text);
    fragment.Arguments = bundle;
}
// MY FRAGMENT - I would like "CUSTOM" my fragment "IntroduzioneFragment" like this:
class IntroduzioneFragment : Fragment {
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        base.OnCreateView(inflater, container, savedInstanceState);
        var view = inflater.Inflate(Resource.Menu.Tab, container, false);
        var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView);
        var imageView = view.FindViewById<ImageView>(Resource.Id.image_view);
        imageView.SetImageResource(Resource.Mipmap.slide1);
        // Get type of service
        var test = Arguments.GetString("text");
        if (test == "Collo") {
            sampleTextView.Text = "is collooooo";
        } else {
            sampleTextView.Text = "is not collo";
        }
        return view;
    }
}

我不希望为每项服务创建一个活动,我希望只有一个“服务活动详细信息”以及按服务类型定制的文本和图像。

错误:,当我选择商品服务时:

System.NullReferenceException - Object reference not set to an instance of an object. on: 
var test = Arguments.GetString("text");

1 个答案:

答案 0 :(得分:3)

你有两种方法可以做到这一点。

如果这是包含片段的活动,您可以调用

this.Activity

内部片段,并在投射后调用活动的任何方法

AwesomceActivty castetActivity = (AwesomeActivity)this.Activity;
castetActivity.AwesomeMethod(12);

或者你可以通过使用代表来实现:

在Fragment类中定义委托

namespace Awesome.Android {
     public class AwesomeFragment : Fragment {

       public delegate void OnAwesomePress (int number);
       public event OnAwesomePress sendOnAwesomePressEvent;
     }
}

您可以在创建Framgent

时指定它
AwesomeFragment fragment = new AwesomeFragment ();
fragment.OnAwesomePress += OnAwesomePress;

之后,您在活动中实施OnAwesomePress

private void OnAwesomePress (int number) {

}

现在,当你在片段中调用sendOnAwesomePressEvent时,该事件将被传递给Activity。

sendOnAwesomePressEvent (10);