简体   繁体   English

在启动时将具有其布局的 Activity 放置在 Main Activity 上

[英]Place an activity with its layout on the Main Activity on launch

Objective: Modular approach目标:模块化方法

I am trying to understand an approach where the Main Activity sort of acts like a stage where I stack sub components on top of it having some sort of modular approach.我试图理解一种方法,其中 Main Activity 有点像一个阶段,我将子组件堆叠在它之上,具有某种模块化方法。

MainActivity.java (Parent) MainActivity.java (父)

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml活动_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>

UserList.java (child) UserList.java (子)

public class UserList extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
    }

    public void initView(ArrayList<UserVO> users) {
        this.users = users;
        userList.setAdapter(new UserListAdapter());
    }

    class UserListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return users.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = layoutInflater.inflate(R.layout.user_list, null);

            TextView givenName = view.findViewById(R.id.givenName);
            givenName.setText(users.get(i).givenName());
            return view;
        }
    }
}

activity_user_list.xml活动用户列表.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=".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>

I believe ListView requires this somewhere, each cell has a textField我相信ListView在某处需要这个,每个单元格都有一个 textField

<TextView
    android:id="@+id/givenName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:text="TextView"
    android:textSize="18sp" />

Question

How can I add UserList on app launch to the MainActivity?如何在应用程序启动时将UserList添加到 MainActivity?

Edit编辑

I've followed the same naming convention.我遵循了相同的命名约定。 Here is an error:这是一个错误:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView java.lang.IllegalStateException: ArrayAdapter 要求资源 ID 为 TextView

在此处输入图片说明

Try this using fragments,使用片段试试这个,

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

}
}

activity_main layout:(need to use frame layout to implement fragments) activity_main layout:(需要使用frame layout来实现fragments)

<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"/>

Here comes the fragment code:下面是片段代码:

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;

}

} }

And fragment layout和片段布局

<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" />

code for textview where we need to put the listview data textview 的代码,我们需要在其中放置列表视图数据

<?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" />

You have to implement the adapter code in the fragment I just used the static data and I was able to get the feature you wanted.您必须在我刚刚使用静态数据的片段中实现适配器代码,并且我能够获得您想要的功能。 If you need further help I can do it for you comment below.如果您需要进一步的帮助,我可以为您在下方评论。 Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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