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

时间:2019-04-08 07:39:21

标签: java android

目标:模块化方法

尝试绕过这种方法,在这种情况下,Main Activity就像一个阶段,在该阶段中,我将具有某种模块化方法的子组件堆叠在其上。

MainActivity.java (父级)

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

activity_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 (子级)

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

我相信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" />

如果有帮助,可以提供更多有关Adapter的代码。

问题 如何在应用启动时将UserList添加到MainActivity?

修改

我遵循相同的命名约定,请查看

  

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

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试使用片段

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布局:(需要使用框架布局来实现片段)

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

我们需要在其中放置列表视图数据的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" />

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