android 中通过ListView 实现类似listview + scrollview滚动效果:
主要有以下三个步骤:
1.main_listview.xml布局。
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F7F7F7"
android:orientation="vertical">
<ListView
android:id="@+id/money_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:layout_weight="1"
android:cacheColorHint="@color/transparent"
android:divider="@null"
android:fadeScrollbars="true"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="@drawable/scroll"/>
</ListView>
2. header.xml布局
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:id="@+id/my_header_rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
>
<TextView
android:id="@+id/my_title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:singleLine="true"
android:textColor="#432312"
android:textSize="@dimen/font_title />
<ImageView
android:id="@+id/my_name_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:src="@drawable/my_name_img"/>
<ImageView
android:id="@+id/my_age_btn"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:padding="5dip"
android:clickable="true"
android:visibility="gone"
android:src="@drawable/my_age_img"/>
</RelativeLayout>
</LinearLayout>
3.在代码中按照如下步骤使用就可以。
1) private View headView;
private ListView listView;
private MonkeyInfo mMoneyInfo;
2)headView = LayoutInflater.from(this).inflate(R.layout.header, null);
3)listView = (ListView)findViewById(R.id.money_lv");
listView.addHeaderView(headView);
adapter = new Adapter(MyActivity.this, mMoneyInfo);
如果想让一个布局同listview一块滚动就可以利用listview的headerView和footerView来把布局添加到listview上面。
这样就可以避免了,在外面嵌套一层scrollview而导致的各种问题。