Search code examples
androidlistviewcustom-adapter

Can't scroll ListView inside ListView in Android


While trying to create news feed in my app, I've created custom ListView Adapter. My single ListView item has an ImageView, WebView and ListView. So I have a ListView inside a ListView. The problem is, when I try to scroll the inner ListView, the outer ListView is being scrolled. What should I change to avoid the problem?

My custom adapter:

public class TestCustomAdapter extends BaseAdapter {

    private List<String> imageNames = null;
    private List<String> descriptions = null;
    private List<String> innerLVitems = null;
    private LayoutInflater mInflater;
    private Context context;

    public FirstFragmentLVAdapter(Context context, List<String> imageNames, List<String> descriptions, List<String> innerLVitems) {
        this.imageNames = imageNames;
        this.descriptions = descriptions;
        this.innerLVitems = innerLVitems;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return innerLVitems.size();
    }


    public Object getItem(int position) {
        List <String> returnedList = new ArrayList<>();
        returnedList.add(imageNames.get(position));
        returnedList.add(descriptions.get(position));
        returnedList.add(innerLVitems.get(position));
        return returnedList;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.firstfragment_listview_item, null);

            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.eventImage);
            holder.description = (WebView) convertView.findViewById(R.id.eventDescriptionWV);
            holder.LV = (ListView) convertView.findViewById(R.id.LV);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }



            holder.image.setImageResource(context.getResources().getIdentifier(imageNames.get(position), "drawable", context.getPackageName()));

            String webViewText = "<html><body><p align=\"justify\">"+
                    descriptions.get(position)+"</p></body></html>";
            holder.description.loadDataWithBaseURL(null, webViewText, "text/html", "UTF-8", null);

            String [] items = innerLVitems.get(position).split("\\s*,\\s*");
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_1, android.R.id.text1, items);
            holder.LV.setAdapter(adapter);


        return convertView;
    }

    static class ViewHolder {
        ImageView image;
        WebView description;
        ListView LV;
    }

}

My listview item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">


<ImageView
    android:id="@+id/eventImage"
    android:layout_width="wrap_content"
    android:layout_height="125dp"
    app:srcCompat="@drawable/autumn" />

<WebView
    android:id="@+id/eventDescriptionWV"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:layout_below="@id/eventImage"></WebView>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/eventDescriptionWV"
    android:text="Text" />

<ListView
    android:divider="@null"
    android:dividerHeight="0dp"
    android:id="@+id/LV"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_below="@id/label" />


Solution

  • I was able to find a solution for my case. I needed to add this line to the inner ListView XML:

    android:nestedScrollingEnabled="true"
    

    This only works only for Lollipop and up though.