【问题标题】:Change fragment from ListActivity in Android Xamarin从 Android Xamarin 中的 ListActivity 更改片段
【发布时间】:2017-09-27 10:06:08
【问题描述】:

我正在尝试从 listActivity 的对话框中选择项目后更改当前片段

这是我的 listActivity.cs 类:

 [Activity(  Theme = "@android:style/Theme.Dialog" , Label ="Number list") ]
    class NumberListActivity : ListActivity 
    {
        string[] data;
        ArrayAdapter adapter;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            this.data = Intent.GetStringArrayExtra("list_numbers");
            adapter = new ArrayAdapter(this, Resource.Layout.specific_contact_number_list_layout, data);
            ListAdapter = adapter;
        }
        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {
            base.OnListItemClick(l, v, position, id);


            //Intent callFragment = new Intent(this, typeof(barMenu));
            //callFragment.PutExtra("callNumber", data[position]);
            //StartActivity(callFragment);

            ISharedPreferences preferences = GetSharedPreferences(SignInActivity.userSessionPref, FileCreationMode.Private);
            String UserNamevalueSession = preferences.GetString("UserNamevalue", "");
            String UserphonevalueSession = preferences.GetString("Userphonevalue", "");
            CallService.phone.Connect(data[position], UserphonevalueSession);
            DateTime now = DateTime.Now;
            SqliteDB db = new SqliteDB();
            db.Insert(data[position], now.ToString());


            var frag = new MainActivity();
            FragmentManager.BeginTransaction().Replace(Resource.Id.conent_fragment, frag).Commit();
            Finish();
        }
    }

这里的一切都很好,我只需要在点击项目后更改片段

我试过了

var frag = new MainActivity();
FragmentManager.BeginTransaction().Replace(Resource.Id.conent_fragment, frag).Commit();

但它给了我

09-27 12:57:00.247 E/AndroidRuntime(11446): java.lang.IllegalArgumentException:未找到 id 0x7f080079 的视图 (TurkeyanaCall.TurkeyanaCall:id/conent_fragment) 用于片段 MainActivity{4b067018 #0 id=0x7f080079}

我的 specific_contact_number_list_layout.xmal 代码

<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/number_list"
  android:textColor="#37c837"
  android:textSize="20dip"
  android:textStyle="italic"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginLeft ="20dp"
  android:padding ="10dp"
          />

这是包含我想要更改其命名为 bar_menu.xmal

的片段的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/maintActivity_drawerlayout">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/linearView">
        <fragment
            class="TwilioClientTest.Android.MainMenuFragment"
            android:id="@+id/conent_fragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/maintActivity_navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/shadow"
        app:menu="@menu/left_menus"
        app:theme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 能否把NumberListActivity的详细代码和xaml代码贴出来?以便我们知道您要更改哪个片段?
  • 我添加了我的 xaml 页面布局代码
  • 我正在从 android.support.v4.widget.DrawerLayout 更改片段,它运行良好,但我的应用程序中有电话簿,我希望当点击联系人姓名时我希望它呼叫联系人并转到我现在可以调用的调用片段,因为它使用服务进行调用,但我想在调用期间更改片段

标签: c# android visual-studio android-fragments xamarin


【解决方案1】:

这里出了点问题。

首先,您尝试将活动用作片段。

var frag = new MainActivity();

其次,默认情况下ListActivity 为您设置内容视图,其中包含ListView。因此没有conent_fragment 视图。

如果你只想显示 MainActivity 你可以替换

var frag = new MainActivity();
FragmentManager.BeginTransaction().Replace(Resource.Id.conent_fragment, frag).Commit();

StartActivity(typeof(MainActivity));

如果你真的想使用片段,你将不得不改变一些事情。如果是这种情况,我可以更新我的答案。

片段编辑

创建一个名为number_list的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/number_list_content"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</FrameLayout>

这里重要的是添加一个ListView,其ID 为android:id="@android:id/list"。这将替换默认值。

然后在你的OnCreate 中添加SetContentView(Resource.Layout.number_list);

那么当你想展示片段的时候

var frag = new MainActivity();
FragmentManager.BeginTransaction().Replace(Resource.Id.number_list_content, frag).Commit();

【讨论】:

  • 我正在开发应用程序,所以我将主要活动转为碎片 MainActivity 现在是 Fragment 而不是 Activity
  • 我想使用片段,我的 mainActivity 现在是 barMenu.cs
  • 我已经做了所有这些我的问题是我想从另一个类中更改片段目前我从 NavigationView 项目中很好地更改了片段但现在我需要在单击项目后再次更改片段我的问题是我不能在另一个类或接收器内部使用 FragmentManager
  • 您确定添加了我的建议吗?最初的问题是为什么“没有为 id 0x7f080079 找到视图”,原因是没有“conent_fragment”。我不明白你现在在说什么,但最好在新线程中发布。
  • 我只需要从接收器或任何其他地方更改片段,除了我的 mainActivity 类的 barMenu
猜你喜欢
  • 1970-01-01
  • 2016-11-03
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
相关资源
最近更新 更多