Java 将活动及其布局放置在启动时的主活动上

Java 将活动及其布局放置在启动时的主活动上,java,android,Java,Android,目标:模块化方法 我试图理解一种方法,其中主要活动的行为类似于一个阶段,在这个阶段中,我使用某种模块化方法在其上堆叠子组件 MainActivity.java(父级) 活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

目标:模块化方法

我试图理解一种方法,其中主要活动的行为类似于一个阶段,在这个阶段中,我使用某种模块化方法在其上堆叠子组件

MainActivity.java(父级)

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.components.UserList">

    <ListView
        android:id="@+id/userList"
        android:layout_width="395dp"
        android:layout_height="715dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
问题

如何在应用程序启动时将
UserList
添加到MainActivity

编辑


我遵循同样的命名惯例。这里有一个错误:

java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView


尝试使用片段

Activity Main code,

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadData();
}

private void loadData() {

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame_layout,new ListViewFragment())
            .commit();

}
}
活动\主布局:(需要使用框架布局来实现片段)

}

和片段布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListViewFragment">

<ListView
    android:id="@+id/userList"
    android:layout_width="395dp"
    android:layout_height="715dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

文本视图的代码,我们需要在其中放置listview数据

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/givenName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="TextView"
android:textSize="18sp"
xmlns:android="http://schemas.android.com/apk/res/android" />


您必须在片段中实现适配器代码,我刚刚使用了静态数据,并且能够获得您想要的功能。如果你需要进一步的帮助,我可以为你做下面的评论。谢谢。

您想在mainactivity中放大listview?您读过关于s的内容吗?是的,在一个单独的
UserList
类中隔离
listview
相关代码时。您不能合并单独的活动。如果您希望
MainActivity
成为多个不同组件的宿主,那么请将
UserList
和其他任何组件制作成
Fragment
s,这些组件可以在
MainActivity
@deHaar中进行交易。要保持简单,Fragments目前对我来说是高级的。或者,如果没有简单的解决方案,那么我可以用我的代码提供一个简单的示例吗?当然可以@Developer。我已经添加了静态数据,请使用您的代码进行操作。如果这是您要求的答案,请接受我的回答。我遇到了一个问题
Android资源编译失败错误:未找到任何元素
。对于
工具:context=“.ListViewFragment”>
。我的ListViewFragment在一个包中,这有关系吗?哦,我已经将我的片段命名为ListViewFragment..你需要将你的片段名称放在工具中,否则你可以删除该行@DeveloperI。如果你遵循相同的命名约定,请参见我问题中的edit下的屏幕截图。你需要将其命名为.components.ListViewFragment@开发者。你必须提到父文件夹。
Activity Main code,

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadData();
}

private void loadData() {

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame_layout,new ListViewFragment())
            .commit();

}
}
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame_layout"/>
public class ListViewFragment extends Fragment {

private String listItems[] = {"vfvd","vdvvfv","vvddv","vfddvvd"}; //static data
ListView userList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_list_view, container, false);



    userList = view.findViewById(R.id.userList);

    ArrayAdapter arrayAdapter = new 
    ArrayAdapter(getActivity(),R.layout.user_list_items,listItems);
    userList.setAdapter(arrayAdapter);

    return view;

}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListViewFragment">

<ListView
    android:id="@+id/userList"
    android:layout_width="395dp"
    android:layout_height="715dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/givenName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="TextView"
android:textSize="18sp"
xmlns:android="http://schemas.android.com/apk/res/android" />