簡體   English   中英

在啟動時將具有其布局的 Activity 放置在 Main Activity 上

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

目標:模塊化方法

我試圖理解一種方法,其中 Main Activity 有點像一個階段,我將子組件堆疊在它之上,具有某種模塊化方法。

MainActivity.java (父)

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

活動_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;
        }
    }
}

活動用戶列表.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" />

如何在應用程序啟動時將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();

}
}

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

下面是片段代碼:

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

您必須在我剛剛使用靜態數據的片段中實現適配器代碼,並且我能夠獲得您想要的功能。 如果您需要進一步的幫助,我可以為您在下方評論。 謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM