サンプルになると良いのですが.
何処かに不具合があるかもしれないのは予めご了承ください.
MainActivity.java
java
1import androidx.appcompat.app.AppCompatActivity;
2import androidx.fragment.app.FragmentTransaction;
3
4import android.content.Context;
5import android.os.Bundle;
6import android.view.*;
7import android.widget.*;
8import java.util.*;
9
10class Item {
11 long id;
12 String fixed, comment;
13}
14class Adapter extends BaseAdapter {
15 private class ViewHolder {
16 TextView text1;
17 TextView text2;
18 }
19 private LayoutInflater inflater;
20 private List<Item> list = new ArrayList<>();
21 Adapter(Context context, List<String> fixedData) {
22 inflater = LayoutInflater.from(context);
23 long id = 0;
24 for(String data : fixedData) {
25 Item item = new Item();
26 item.id = id++;
27 item.fixed = data;
28 item.comment = "";
29 list.add(item);
30 }
31 }
32 void setComment(long id, String text) {
33 Item item = getItem(id);
34 if(item != null) {
35 item.comment = text;
36 notifyDataSetChanged();
37 }
38 }
39 Item getItem(long id) {
40 for(Item item : list) if(item.id == id) return item;
41 return null;
42 }
43 @Override
44 public int getCount() {
45 return list.size();
46 }
47 @Override
48 public Object getItem(int position) {
49 return list.get(position);
50 }
51 @Override
52 public long getItemId(int position) {
53 return list.get(position).id;
54 }
55 @Override
56 public View getView(int position, View convertView, ViewGroup parent) {
57 ViewHolder holder = null;
58 if(convertView == null) {
59 convertView = inflater.inflate(R.layout.list_item_layout, null);
60 holder = new ViewHolder();
61 holder.text1 = convertView.findViewById(R.id.text1);
62 holder.text2 = convertView.findViewById(R.id.text2);
63 convertView.setTag(holder);
64 } else {
65 holder = (ViewHolder)convertView.getTag();
66 }
67 holder.text1.setText(list.get(position).fixed);
68 holder.text2.setText(list.get(position).comment);
69 return convertView;
70 }
71};
72public class MainActivity extends AppCompatActivity implements ListFragment.ListModel, ItemFragment.Regist {
73 private Adapter adapter;
74 @Override
75 protected void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
77 setContentView(R.layout.activity_main);
78 adapter = new Adapter(this, Arrays.asList("AAA","BBB","CCC","DDD"));
79 showListFragment();
80 }
81 void showListFragment() {
82 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
83 transaction.replace(R.id.flagmentContainer, ListFragment.createInstance());
84 transaction.commit();
85 }
86 void showItemFragment(long id, String fixed, String comment) {
87 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
88 transaction.replace(R.id.flagmentContainer, ItemFragment.createInstance(id, fixed, comment));
89 transaction.addToBackStack(null);
90 transaction.commit();
91 }
92 @Override
93 public ListAdapter getAdapter() {
94 return adapter;
95 }
96 @Override
97 public void onSelect(long id) {
98 Item item = adapter.getItem(id);
99 showItemFragment(id, item.fixed, item.comment);
100 }
101 @Override
102 public void onRegist(long id, String text) {
103 adapter.setComment(id, text);
104 showListFragment();
105 }
106}
ListFragment.java
java
1import android.content.Context;
2import android.os.Bundle;
3import android.view.*;
4import android.widget.*;
5
6import androidx.annotation.NonNull;
7import androidx.annotation.Nullable;
8import androidx.fragment.app.Fragment;
9
10public class ListFragment extends Fragment {
11 static ListFragment createInstance() {
12 return new ListFragment();
13 }
14 interface ListModel {
15 ListAdapter getAdapter();
16 void onSelect(long id);
17 }
18 private ListModel listModel;
19 private ListView listView;
20 @Nullable
21 @Override
22 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
23 View v = inflater.inflate(R.layout.list_fragment, container, false);
24 listView = v.findViewById(R.id.listView);
25 listView.setAdapter(listModel.getAdapter());
26 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
27 @Override
28 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
29 listModel.onSelect(id);
30 }
31 });
32 return v;
33 }
34 @Override
35 public void onAttach(Context context) {
36 super.onAttach(context);
37 if(context instanceof ListModel) {
38 listModel = (ListModel)context;
39 }
40 }
41}
ItemFragment.java
java
1import android.content.Context;
2import android.os.Bundle;
3import android.view.*;
4import android.widget.*;
5
6import androidx.annotation.NonNull;
7import androidx.annotation.Nullable;
8import androidx.fragment.app.Fragment;
9
10public class ItemFragment extends Fragment {
11 private static final String ARGS_ID = "id";
12 private static final String ARGS_FIXED = "fixed";
13 private static final String ARGS_COMMENT = "comment";
14 static ItemFragment createInstance(long id, String fixed, String comment) {
15 ItemFragment fragment = new ItemFragment();
16 Bundle args = new Bundle();
17 args.putLong(ARGS_ID, id);
18 args.putString(ARGS_FIXED, fixed);
19 args.putString(ARGS_COMMENT, comment);
20 fragment.setArguments(args);
21 return fragment;
22 }
23 interface Regist {
24 void onRegist(long id, String text);
25 }
26 @Override
27 public void onCreate(@Nullable Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 }
30 private Regist regist;
31 @Nullable
32 @Override
33 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
34 View v = inflater.inflate(R.layout.item_fragment, container, false);
35 TextView fixed = v.findViewById(R.id.fixed);
36 final EditText editText = v.findViewById(R.id.editText);
37 Bundle args = getArguments();
38 if(args != null) {
39 fixed.setText(args.containsKey(ARGS_FIXED) ? args.getString(ARGS_FIXED) : "");
40 editText.setText(args.containsKey(ARGS_COMMENT) ? args.getString(ARGS_COMMENT) : "");
41 }
42 Button button = v.findViewById(R.id.button);
43 button.setOnClickListener(new View.OnClickListener() {
44 @Override
45 public void onClick(View v) {
46 Bundle args = getArguments();
47 if(args != null && args.containsKey(ARGS_ID)) {
48 regist.onRegist(args.getLong(ARGS_ID), editText.getText().toString());
49 }
50 }
51 });
52 return v;
53 }
54 @Override
55 public void onAttach(final Context context) {
56 super.onAttach(context);
57 if(context instanceof Regist) {
58 regist = (Regist)context;
59 }
60 }
61}
activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context=".MainActivity"
7 android:orientation="horizontal"
8 android:id="@+id/flagmentContainer"/>
list_fragment.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent">
5
6 <ListView
7 android:id="@+id/listView"
8 android:layout_width="match_parent"
9 android:layout_height="match_parent" />
10</LinearLayout>
list_item_layout.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical">
6
7 <TextView
8 android:id="@+id/text1"
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"/>
11 <TextView
12 android:id="@+id/text2"
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"/>
15</LinearLayout>
item_fragment.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical">
7
8 <TextView
9 android:id="@+id/fixed"
10 android:layout_width="match_parent"
11 android:layout_height="wrap_content"
12 android:layout_weight="0"
13 app:layout_constraintTop_toTopOf="parent"
14 app:layout_constraintLeft_toLeftOf="parent"
15 app:layout_constraintRight_toRightOf="parent"/>
16 <EditText
17 android:id="@+id/editText"
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:layout_weight="1"
21 android:ems="10"
22 android:gravity="start|top"
23 android:inputType="textMultiLine"
24 app:layout_constraintTop_toBottomOf="@id/fixed"
25 app:layout_constraintBottom_toTopOf="@id/button"
26 app:layout_constraintLeft_toLeftOf="parent"
27 app:layout_constraintRight_toRightOf="parent"/>
28 <Button
29 android:id="@+id/button"
30 android:layout_width="wrap_content"
31 android:layout_height="wrap_content"
32 android:layout_weight="0"
33 android:text="Regist"
34 app:layout_constraintBottom_toBottomOf="parent"
35 app:layout_constraintLeft_toLeftOf="parent"
36 app:layout_constraintRight_toRightOf="parent"/>
37</androidx.constraintlayout.widget.ConstraintLayout>