Android: Listview in Listview

android: listview in listview

From the Android documentation - Listview: ListView is a view group that displays a list of scrollable items

You do not really want to scroll that inner list view, you want to scroll the outer listview. However I asume that the inner listview may vary on the amount of elements it contains.

Instead of the inner list view you could use a

  • linear layout, see this tutorial or look at Adding content to a linear layout dynamically?
  • table layout

For the linear layout (some sample code):

// access your linear layout
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
// load the xml structure of your row
View child = getLayoutInflater().inflate(R.layout.row);
// now fill the row as you would do with listview
//e.g. (TextView) child.findViewById(...
...
// and than add it
layout.addView(child);

You should save the linear layout in a view holder (see View Holder pattern). I think the removeAllViews() is only necessary when the current row has lesser inner rows than the reused one, so I would also save the number of rows in the view holder.

If the maximum number of inner rows is not to high you could also think about caching them in the view holder to avoid the inflate and findByViewId (lets say in an ArrayList).

ListView inside a listView Item

Your Code is Working Proper look here

Sample Image

I make first listview item xml file height fix 150dp (card_view) and its working proper here is all code

1) card_view :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

2) ListViewAdapter :

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class ListNewAdapter extends BaseAdapter{

private static final String TAG = "ListNewAdapter";
private final ArrayList<String> topics;
private final Context context;

public ListNewAdapter(@NonNull Context context, ArrayList<String> topics) {
this.context = context;
this.topics = topics;
}

@Override
public int getCount() {
Log.d(TAG, "getCount:"+topics.size());
return topics.size();
}

@Override
public Object getItem(int i) {
return topics.get(i);
}

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

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View listItem = convertView;
if (listItem==null)
listItem = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);

final String currentItem = (String) getItem(position);

TextView topic = (TextView) listItem.findViewById(R.id.topic_text_view);
topic.setText(currentItem);

return listItem;
}
}

3) CardAdapter :

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class CardsAdapter extends BaseAdapter {

private static final String TAG = "CardsAdapter";
private final Context context;
String[][] allTopics = {};

public CardsAdapter(@NonNull Context context, String[][] unitsAndTopics) {
this.context = context;
allTopics = unitsAndTopics;
}

@Override
public int getCount() {
return allTopics.length;
}

@Nullable
@Override
public Object getItem(int position) {
ArrayList<String> all = new ArrayList<String>();
for (int i = 0; i <allTopics[position].length ; i++) {
all.add(allTopics[position][i]);
Log.d(TAG, "getItem:"+all);
}

return all;
}

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

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View listItem = convertView;
if (listItem==null)
listItem = LayoutInflater.from(context).inflate(R.layout.card_view,parent,false);

ArrayList<String> currentItem = (ArrayList<String>) getItem(position);

ListView topic = (ListView) listItem.findViewById(R.id.listView);
ListNewAdapter listNewAdapter = new ListNewAdapter(context,currentItem);
topic.setAdapter(listNewAdapter);

return listItem;
}
}

Fill Listview inside a Listview using adapters

One thing that might be happening is the second list view's height is not being set correctly since it's inside another list view. I might have had to deal with that once.

Try using this method in your first adapter to set the list view's height - (AFTER YOU'VE SET THE ADAPTER)
Something like setListViewHeightBasedOnChildren(holder.listViewKidsContacts);

// Method for Setting the Height of the ListView dynamically

public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;

int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}

If that doesn't work, I would try breakpointing on lines:
KidsInsideContactsAdapter mAdapter = new KidsInsideContactsAdapter(((Activity)mContext)); //This is the adapter(B)
holder.listViewKidsContacts.setAdapter(mAdapter); //Assign the adapter(B) to the ListView(B)

When debugging check to see if mAdapter is null. Also put breakpoints in constructors for KidsInsideContactsAdapter to see if everything is happening okay.

Also check to make sure holder.listViewKidsContacts is the list view you are looking for while debugging.

list view inside list View in android

The problem is you can't get the scroll of both listview at a time. if you what you both at the same time. you need to create a custom list view try something like this instead of your list view (inside list).

  public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}

public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}

Android: is it possible to have a ListView within a ListView?

I would an expandable listview using something similar to the code below:

main class (in fragment)

public class LineupFragment extends Fragment {

View rootView;
ExpandableListView lv;
private String[] groups;
private String[][] children;

public LineupFragment() {

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

groups = new String[] { "Test Header One", "Test Header Two", "Test Header Three", "Test Header Four" };

children = new String [][] {
{ "s simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." },
{ "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of comes from a line in section 1.10.32." },
{ "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)." },
{ "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc." }
};
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_lineup, container, false);

return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

lv = (ExpandableListView) view.findViewById(R.id.expListView);
lv.setAdapter(new ExpandableListAdapter(groups, children));
lv.setGroupIndicator(null);
}

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private final LayoutInflater inf;
private String[] groups;
private String[][] children;

public ExpandableListAdapter(String[] groups, String[][] children) {
this.groups = groups;
this.children = children;

inf = LayoutInflater.from(getActivity());
}

@Override
public int getGroupCount() {
return groups.length;
}

@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}

@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inf.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();

holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.text.setText(getChild(groupPosition, childPosition).toString());

return convertView;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = inf.inflate(R.layout.list_group, parent, false);

holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.lblListHeader);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.text.setText(getGroup(groupPosition).toString());

ImageView Photo = (ImageView) convertView.findViewById(R.id.photo);

//Images for all artists need to be hardcoded below

if (groupPosition == 0) {
Photo.setBackgroundResource(R.drawable.ivan_grech);
}
else if (groupPosition == 1) {
Photo.setBackgroundResource(R.drawable.ira_losco);
}

return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

private class ViewHolder {
TextView text;
}
}
}

Adapter class:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Activity _context;
private List<String> _listDataHeader; // header titles
private HashMap<String, List<String>> _listDataChild;// child data in format of header title, child title

public ExpandableListAdapter(Activity context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

txtListChild.setText(childText);
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}

TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

Hope this helps :)

Android Listview with Listview item

Instead of using Nested ListViews, you can use Expandable ListView.
Here are some useful links for playing with expandable ListView.

http://www.dreamincode.net/forums/topic/270612-how-to-get-started-with-expandablelistview/

http://android-adda.blogspot.com/2011/06/custom-expandable-listview.html

Flutter - Listview.builder inside another Listview

Add shrinkWrap: true in listView.builder & Remove the top most Container or replace it with Column.



Related Topics



Leave a reply



Submit