Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

listview - Android multiple list views that don't scroll independently

I want to create a page that has 3 lists and 3 headers but I don't want to scroll them independently, I want the whole page to scroll. Does anyone know how I could achieve this?

Essentially I want it to look like this:

header
smlheader
list
smlheader
list
smlheader
list

With this code I've tried to achieve it but it's not that good.

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

<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

   <include layout="@layout/header" />
   <include layout="@layout/redcell" />

   <TableRow

       android:id="@+id/tableRow1"
       android:layout_width="wrap_content"
       android:layout_height="60dp" >


      <TextView

        android:id="@+id/fixtures_text"
        android:layout_width="match_parent"
        android:layout_height="60dp"

        android:gravity="center"
        android:text="@string/leagues"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"/>




   </TableRow>

   <include layout="@layout/redcell" />

   <TableRow
       android:id="@+id/tableRow2"
       android:layout_width="wrap_content"
       android:layout_height="60dp" >


      <TextView

        android:id="@+id/results_text"
        android:layout_width="match_parent"
        android:layout_height="60dp"

        android:gravity="center"
        android:text="@string/leagues"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"/>

   </TableRow>

    <include layout="@layout/redcell" />


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

</TableLayout>
<ScrollView/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would try CommonsWare MergeAdapter as pointed to by Rajesh. It sounds like exactly what you need.

It will take multiple views (including listviews) and put them together, then you set the merge adapter to a listview and presto, you have several listviews in one.

Quoting from the docs for it:

MergeAdapter accepts a mix of Adapters and Views and presents them as one contiguous whole to whatever ListView it is poured into. This is good for cases where you have multiple data sources, or if you have a handful of ordinary Views to mix in with lists of data, or the like.

Simply create a MergeAdapter and call addAdapter(), addView(), or addViews() (latter accepting a List), then attach your adapter to the ListView.

EDIT

1) Download the .jar from here
2) Download the .jar for CWAC SackOfViewsAdapter as the MergeAdapter requires it.
2) Create a directory in your project called "libs" (on the same level as src & res)
3) place both of the .jar files in that folder (Eclipse should automatically take things from there as far as setting it up to be included in the build)
4) Use the MergeAdapter per the instructions from the first link in my original response.

Pseudo-code example:

myMergeAdapter = new MergeAdapter();
myMergeAdapter.addView(HeaderView);
myMergeAdapter.addView(SmallHeaderView1);
myMergeAdapter.addAdapter(listAdapter1);
myMergeAdapter.addView(SmallHeaderView2);
myMergeAdapter.addAdapter(listAdapter2);
myMergeAdapter.addView(SmallHeaderView3);
myMergeAdapter.addAdapter(listAdapter3);

setListAdapter(myMergeAdapter);

EDIT 2

Adding your header which is a complete layout:

View Header = getLayoutInflater.inflate(R.layout.red_cell);
myMergeAdapter.addView(Header);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...