0

So basically you can't place a ListView in a ScrollView because the Scrolling ability clashes in both layouts. When I tried to do it, the ListView becomes completely useless and many other problems arise.

How has Facebook done it?

As you can see, the work section is a ListView and it's also a Scrollable layout so that the user can scroll down to view the Education section, which is also a ListView.

enter image description here

My code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:layout_marginBottom="40dp">

               <!-- More layouts -->

                <ListView
                    android:id="@+id/work_list"
                    android:layout_below="@+id/recentpic"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                </ListView>

</ScrollView >

I do not want a ListView Scroll bar

Therefore the scrolling dilemma is completely removed from the equation. Even when I disable the scroll bars, the problem persists.

Solution that I have in mind:

Generating the XML rows (Each Workplace of the ListView) and injecting it to the layout and avoiding the use of ListViews, similar to HTML Code Generation using Javascript.

What method do you think Facebook has used in their android app to get this done and what changes should I make to my code? :)

Jay
  • 4,873
  • 7
  • 72
  • 137
  • 1
    the solution you are thinking about is best way to go with. also it is not a good idea to use list views inside scroll view [as per the guy who created ListView](http://stackoverflow.com/a/3496042/1529129). – Rahul Tiwari Sep 27 '15 at 14:09
  • @RahulTiwari I managed to code it. Check out my answer if you are interested :) – Jay Sep 27 '15 at 16:20

2 Answers2

1

Have you tried using NestedScrollView? I think it's a NestedScrollView which contains a ListView and the whole thing is enclosed in a ScrollView. This link might help: http://ivankocijan.xyz/android-nestedscrollview/

sri
  • 744
  • 7
  • 20
1

Okay so I managed to code my own idea I mentioned. It's a very 'sexy' code and it gets the job done :D

Here you go, guys. I hope it helps someone :)

So basically I'm inflating a Parent Layout with multiple Child Layouts dynamically and completely getting rid of ListViews in the view. Which makes is very simple to use it with a ScrollView and forget about that dilemma.

Parent Layout:

<RelativeLayout
        android:id="@+id/work_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</RelativeLayout>

Child Layout - work_single_item.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <ImageView
        android:id="@+id/work_pic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:src="@mipmap/image_placeholder"/>

</RelativeLayout>

Coded the following lines in the OnCreate function of the Parent Layout.

RelativeLayout parent = (RelativeLayout) findViewById(R.id.work_list);

//array containing all children ids
ArrayList<Integer> children = new ArrayList<>();

//adding 10 children to the parent
for(int i=0;i<10;i++) {

    RelativeLayout child = new RelativeLayout(this);
    View tempchild = getLayoutInflater().inflate(R.layout.work_single_item, null);
    child.addView(tempchild);

    child.setId(i); //setting an id for the child
    children.add(i); //adding the child's id to the list

    if(i!=0) //if it isn't the 1st child, stack them below one another, since the 1st child does not have a child to stack below
    {
                RelativeLayout.LayoutParams params 
                           = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.BELOW, children.get(i - 1)); //stack it below the previous child
                child.setLayoutParams(params);
    }

    parent.addView(child); //add the new child
}
Jay
  • 4,873
  • 7
  • 72
  • 137