adding ListView children to a HorizontalScrollView

2019-08-16 00:44发布

问题:

i have a HorizontalScrollView, and i am adding a bunch of ListView as its children. my XML file looks something like the following. for brevity, i will omit some of the attributes.

<HorizontalScrollView>
 <LinearLayout>
  <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
 </LinearLayout>
</HorizontalScrollView>

in my java code, i then populate the ListView with data. If there is only 1 or 2 records, the ListView does not extend all the way to the bottom of the screen. i see what seems to be a transparent background. how do i get rid of this behavior.

i also tried using this component from http://www.matt-reid.co.uk/blog_post.php?id=62. again, i have the same problem.

any help is appreciated.

below is the XML file. i've simply copied/pasted the control code into my package as demo.fling8.HorizontalPager.

 <demo.fling8.HorizontalPager 
android:id="@+id/hp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

  <TextView
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:text= "Etiam et dui volutpat purus gravida consequat. Nullam sodales velit a nunc pretium ut tempor urna molestie. Maecenas metus enim, venenatis vel volutpat non, iaculis ut justo. Nulla venenatis malesuada quam at tincidunt. Nunc at sem eros, convallis eleifend arcu. Curabitur gravida velit nec nunc condimentum feugiat. In ultrices orci sit amet purus fermentum placerat in et lacus. Praesent sit amet sodales lectus. Morbi vehicula condimentum purus eu vulputate. Morbi risus enim, rhoncus a iaculis et, porta vitae justo." 
    />
<ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"></ListView>
 </demo.fling8.HorizontalPager>

in my MainActivity.java class, this is my code to populate the ListView.

 List<MyObject> dataList = DataAccessService.getDummyData();
 ListView listView = (ListView)findViewById(R.id.lv);
 listView.setAdapter(new MyAdapter(this, R.id.textViewTitle, dataList));

if you "fling" or "swipe", you will notice that the TextView does not suffer from this effect of having a transparent background (the whole screen is black). when you fling to the ListView, if there are only a few rows, then from where the ListView ends to the bottom of the screen, there is a transparent background.

here is a link to the project file: http://www.box.net/shared/5pybcpj8mkbfhv3j3ach. please let me know if you cannot access it. if you deploy to your device you should see the effect (of a transparent background).

here is a link to another project file: http://www.box.net/shared/i9vs6yq9j2s4ysp994et. this project file uses only a HorizontalScrollView (not the component referenced above). as you will probably observe, there is the transparent background.

回答1:

If you are looking to implement horizontal paging see the ViewPager widget in the compatibility library: http://developer.android.com/sdk/compatibility-library.html It works all the way back to devices running Android 1.6. It also allows you to only keep a few pages "live" at any given time, keeping your view hierarchy much simpler and allowing you to work with large data sets.



回答2:

You may omit HorizontalScrollView as ListView automatically expand and if there is no room it Scroll automatically. Simply use

    <LinearLayout>
    <ListView android:id="@+id/listView1"/>
    </LinearLayout>


回答3:

I'm not sure if this will help, but it might

<HorizontalScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
 <LinearLayout>
  <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
 </LinearLayout>
</HorizontalScrollView>