2016-12-27 4 views
0

RelativeLayoutに2つのlisViewがあります。最初のリストを超えると2番目のリストが画面に表示され、画面をランドスケープモードに変更すると再び2番目のリストに戻ります。だから私は画面をスクロール可能にしたい。2つのリストビューでスクロール可能なアクティビティを作成します

ここに私のXMLコードは、だ

<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> 

+0

セクションリストを別々にスクロールしたくない場合 –

+0

https://github.com/commonsguy/cwac-merge 1つのListView、その中に2つのアダプタ。 –

答えて

2

つのListViewを使用して、一度にページをスクロールすることは悪い習慣で、パフォーマンス上の問題を引き起こす可能性があります。 ListViewとScrollViewのNestedScrollViewインスタンスを使用してみてください。使用している1が今までベストプラクティスではありません https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html

+0

'NestedScrollView'は本当に私の問題を解決しました。それは、ポートレートモードとランドスケープモードの両方で機能しました。ありがとうございました** Prag's ** – Narendhran

+0

@Narendhran喜んで助けてください。 –

1

は、このリンクを参照してください。同じスクロール方向を有する複数のスクロールアイテムを有することは、通常複雑である。

2つではなく1つのListViewを使用している可能性があります。

これを達成するには、両方のアイテムの値を含むgeneric objectを作成することができます。

例:

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

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

今すぐ両方のオブジェクトの変数を含む新しいclassを作成します。あなたはlistの内側に置くべきlist of itemsを作成するときに

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; 
    } 
    //... 

は今、次のように行います。

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 

は今、この新しいGeneric Itemを受け入れるadapterを作成し、アイテムがItem1であるか、それはItem2だ場合(ブール値を使用する)場合adapter自体の内部管理します。

あなたは両方の可能性を持つ layoutを作成し、アイテムが Item1Item2ある場合に応じた可視性と fields's namesを変更する必要があります


別の可能性は、代わりListViewLinearLayoutを使用しています。

このように、2つのLinearLayoutを使用すると、それらは2つずつ上下に配置されます。 You can refer from this SO question on how to use a LinearLayout as a listview。結果は同じになりますが、スクロールしますが、LinearLayoutsであるため、最初の最後までスクロールして2番目を表示します。

希望、これは幸運を助けます。

+0

私はアンドロイドに新しい、あなたのreffは私にとってとても便利です。あなたの親切な参照をありがとう。 – Narendhran

+0

@ Narendhranあなたの問題を解決するのに役立ちました:) –

+0

私はあなたが言ったように私が疑問に思っていますが、どのように私は別のリストと特定のリストのヘッダーの間にいくつかのスペースを指定できますか?あなたのソリューションを使用している間、これは可能ですか? – Narendhran

関連する問題