Here is an example of how you can create a ListView inside another ListView in Java using XML layout files.
First, create the parent layout file (activity_main.xml):
<LinearLayout 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"
android:orientation="vertical">
<!-- Header or other views -->
<ListView
android:id="@+id/parentListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Footer or other views -->
</LinearLayout>
Next, create the child layout file (row_item.xml) for each row of the parent ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Child ListView -->
<ListView
android:id="@+id/childListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Finally, create a custom adapter to bind data to the parent ListView:
public class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<List<String>> parentList;
private Context context;
public CustomAdapter(Context context, List<List<String>> parentList) {
this.context = context;
this.parentList = parentList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return parentList.size();
}
@Override
public Object getItem(int position) {
return parentList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_item, parent, false);
holder = new ViewHolder();
holder.childListView = convertView.findViewById(R.id.childListView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Bind child data to the child ListView
ChildAdapter childAdapter = new ChildAdapter(context, parentList.get(position));
holder.childListView.setAdapter(childAdapter);
return convertView;
}
static class ViewHolder {
ListView childListView;
}
}
Also, create a ChildAdapter to bind data to the child ListView:
public class ChildAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<String> childList;
private Context context;
public ChildAdapter(Context context, List<String> childList) {
this.context = context;
this.childList = childList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return childList.size();
}
@Override
public Object getItem(int position) {
return childList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
holder = new ViewHolder();
holder.textView = convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String item = childList.get(position);
holder.textView.setText(item);
return convertView;
}
static class ViewHolder {
TextView textView;
}
}
In your activity, you can set the parent ListView using your custom adapter:
public class MainActivity extends AppCompatActivity {
private ListView parentListView;
private CustomAdapter adapter;
private List<List<String>> parentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentListView = findViewById(R.id.parentListView);
// Create your data for parent and child ListViews
parentList = new ArrayList<>();
// Add your data to parentList
// Set the adapter to parent ListView
adapter = new CustomAdapter(this, parentList);
parentListView.setAdapter(adapter);
}
}
Make sure to replace android.R.layout.simple_list_item_1 with your own layout file for each item of the child ListView.