Question

Hi, I am newbie to Android. Is it possible to add two ListView parallel on the same screen? I already use one ListView and displaying item using ListAdapter, but actually I need it to this: When it displays the item and scroll horizontal, I want the first TextView to freeze, and when I scroll vertical, it will scroll.

xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal">
<!-- Main ListView 
     Always give id value as list(@android:id/list)
-->


<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:id="@+id/horizontalScrollView">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="#ffffff"
                  android:orientation="vertical">


        <TableRow
                android:id="@+id/tableRow1"
                android:scrollbarAlwaysDrawHorizontalTrack="true"
                android:layout_height="wrap_content"
                android:layout_width="match_parent">

            <TextView
                    android:id="@+id/TextView2"
                    android:text="GRPName"

                    android:background="#A513FF"

                    android:textColor="#000000"
                    android:layout_width="150dp"
                    android:padding="3dip"
                    />
            <TextView
                    android:id="@+id/TextView3" android:text="QTY"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"

                    android:padding="3dip" />
            <TextView
                    android:id="@+id/TextView4" android:text="BUD"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"
                    android:padding="3dip" />
            <TextView
                    android:id="@+id/TextView5" android:text="STK"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"
                    android:padding="3dip" />
            <TextView
                    android:id="@+id/TextView2" android:text="DIF"
                    android:background="#A513FF"
                    android:gravity="right"

                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:padding="3dip"
                    />
            <TextView
                    android:id="@+id/TextView3" android:text="DIF%"
                    android:background="#A513FF"
                    android:textColor="#000000"
                    android:layout_width="60dp"
                    android:gravity="right"

                    android:padding="3dip" />


        </TableRow>
        <ListView
                android:id="@android:id/list"
                android:smoothScrollbar="true"
                android:layout_width="fill_parent"
                android:layout_weight="1"

                android:layout_height="fill_parent"/>

    </LinearLayout>


</HorizontalScrollView>

ListAdapter:

ListAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.list_item,
            new String[] {  TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P, },
            new int[] {
                     R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7});
                      setListAdapter(adapter);
                      ListView lv = getListView();
                      }

I want to freeze TAG_GRPNAME and R.id.l2.

Was it helpful?

Solution

Try something like this:
main.xml

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

        <LinearLayout android:layout_weight="1" 
                    android:layout_height="fill_parent" 
                    android:layout_width="fill_parent">

                <ListView   android:id="@+id/listview1" 
                            android:layout_height="fill_parent" 
                            android:layout_width="fill_parent">

                </ListView>
    </LinearLayout>

<LinearLayout android:layout_weight="1" 
                android:layout_height="fill_parent" 
                android:layout_width="fill_parent">

            <ListView   android:id="@+id/listview2" 
                        android:layout_height="fill_parent" 
                        android:layout_width="fill_parent">

            </ListView>
</LinearLayout>

</LinearLayout>

Where 2 LinearLayout's will have weight 1.
And from code behind:

ListView ls1,ls2;
String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};
private String phones[] = {"Android", "Blackberry", "iPhone","Windows Phone"};

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ls1 = (ListView) findViewById (R.id.listview1);
    ls2 = (ListView) findViewById (R.id.listview2);

    ls1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, colors));
    ls2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, phones));
 }

OTHER TIPS

a little change on @ridoy's code is here and may your work done...!

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

    <ListView
        android:id="@+id/list1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight=".50" >
    </ListView>

    <ListView
        android:id="@+id/list2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight=".50" >
    </ListView>

</LinearLayout>

still is there any query, plz ask.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top