0

I have two lisView in RelativeLayout , when the first list is exceed the second list goes down the screen and when i change the screen to landscap mode the second list again goes down. So I want to make the screen scrollable.

Here's my xml code,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.neemshade.moneyflow.phase1.MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/purchase"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PURCHASE"/>

    <ListView
        android:id="@+id/outstandingPurchaseList"
        android:paddingTop="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/purchase"
        android:scrollbars="none">

    </ListView>


    <TextView
        android:id="@+id/sale"
        android:layout_centerHorizontal="true"
        android:paddingTop="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SALE"
        android:layout_below="@+id/outstandingPurchaseList"/>


    <ListView
        android:id="@+id/outstandingSaleList"
        android:paddingTop="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sale"
        android:scrollbars="none">

    </ListView>

Narandhran Thangavel
  • 1,392
  • 1
  • 12
  • 20

2 Answers2

2

Using Two ListView and scrolling your page at a time is bad practice and can cause performance issue. Try to use NestedScrollView insted of just ListView and ScrollView.

Refer this link : https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
1

The one you are using is not the best practice ever. Having multiple scrolling items with the same scroll direction is usually complicated.

The first and probably easier solution for you would probably be using a single ListView instead of two.

To achieve this, you can create a generic object containing values of both your items.

Example:

class myItem1{
  private String name;
  private int age;
  //...
}

class MyItem2{
  private String surname;
  private boolean male;
  //...
}

Now create a new class containing both object's variables.

class MyGenericItem{
  private boolean isItem1;
  private String name;
  private int age;
  private String surname;
  private boolean male;

  //could be useful creating the two constructors accepting the items
  public MyGenericItem(MyItem1 item){
    this.name = item.name;
    this.age = item.age;
    this.isItem1 = true;
  }
  public MyGenericItem(MyItem2 item){
    this.surname = item.surname;
    this.male = item.male;
    this.isItem1 = false;
  }
  //...

Now when you create the list of items to be put inside your list, do like this.

ArrayList<MyGenericItem> items = new ArrayList();
//now add all Items1 as MyGenericItem (cast them) to the list
//now add all Items2 as MyGenericItem (cast them) to the list

Now create an adapter accepting this new Generic Item and manage inside the adapter itself if the item is Item1 or if it's Item2 (use the boolean).

You will need to create a layout with both possibilities and change visibilities and fields's names depending on if the item is an Item1 or an Item2.


Another possibility is using a LinearLayout instead of a ListView.

In this way you simply use two LinearLayout and they will be placed one below the other. You can refer from this SO question on how to use a LinearLayout as a listview. The result will be the same, they scroll but since they are LinearLayouts they will scroll until the end of the first before showing the second.

Hope this helps, good luck.

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • Iam new to android, your reff is very usefull to me. Thanks for your kind reference. – Narandhran Thangavel Dec 27 '16 at 11:33
  • @Narendhran glad it helped solving your problem :) – Pier Giorgio Misley Dec 27 '16 at 11:51
  • I have one doubt, whether iam using as you said, how can i specify some spaces between one list to another and a header for the specific list? is this possible while using your solution?? – Narandhran Thangavel Dec 27 '16 at 12:32
  • @Narendhran If you are using the single listView solution, you have to add a custom header as a line. You have to add another item where you want the new header and you have to manage it manually to customize it. If you are using the LinearLayut solution, simply add anything you need between the two layouts – Pier Giorgio Misley Dec 27 '16 at 13:03