Android之旅第四站——初识ListView…

这一章主要是提到adapterview和adapter。

什么是adapterview?猜也猜能猜到,是一种控件View,也是我们经常见到的一些视图,列表,网格,下拉列表等,,而adapter

就是适配器,用来自动填充内容的,这章有难度,做好准备。

来看一下类图:

这里写图片描述

在这里我们这章只说一些常用的ListView(列表),GridView(网格)和Spinner(下拉列表)。

ListView就类似我们手机QQ好友,聊天界面那样,通过手指上下滑动可以显示。

有多行,每一行是一个组信息,其实叫做一个Item。

这里写图片描述

由于是第一次写ListView,可以先不用adapter适配器填充内容,用string字符串数组填充,来看一下效果:

这里写图片描述

数据够多超过一个页面放不下可以上下滑动,也可以点右边滚动条滑动。

便于观察每一个Item,我们设置了item的监听事件,使用Log日志打印一下:

这里写图片描述

由此也可以看到,第一个Item的位置是0,和数组一样是从0开始算起的。

下面附下代码:

Mainactivity:

package com.example.lv01;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.main_lv);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                Log.i("lpl", "你点击了第" + position + "个item");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

界面的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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lv01.MainActivity" >

    <ListView
        android:id="@+id/main_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0f0"
        android:cacheColorHint="#0000000"
        android:divider="@android:drawable/divider_horizontal_bright"
        android:dividerHeight="2dp"
        android:entries="@array/lvcontent"
        android:fastScrollEnabled="true"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true"
        android:scrollbars="vertical" >
    </ListView>

</RelativeLayout>

里面有ListView的独特属性:

cacheColorHint是用来设置它的透明度,由于在滑动过程中可能会出现颜色的渐变,为防止发生冲突,最好加上这个透明度为0。

divider是用来设置分割线的样式。我调用了android本地的图片样式。

dividerHeight 是分割线的高度。

entries用来在xml中填充ListView每一个item。lvcontent这个是存放在values里面的strings中的(方便观察,删除了部分item):

这里写图片描述

fastScrollEnabled 是否出现快速滚动条

注意:默认只有当ListView的内容大于4页时,才会显示快速滑动块。

footerDividersEnabled 在尾部是否显示分割线

headerDividerEnabled 在头部是否显示分割线

scrollbars 出现滚动条的方式(none(没有),vertical(垂直), horizontal(水平))

Android应用开发过程中,有时候我们需要实现全屏滚动的效果,或者在滚动页面中嵌入ListView等组件。下面就来介绍一下如何实现这些效果。 一、全屏滚动 实现全屏滚动需要用到Android系统提供的ScrollView组件。ScrollView可以包含多个子视图,并且可以在垂直方向上进行滚动。下面是一个简单的例子: ``` <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第一行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第二行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第三行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第四行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第五行"/> </LinearLayout> </ScrollView> ``` 上面的代码中,我们将ScrollView作为根布局,然后在ScrollView内部添加了一个垂直方向的LinearLayout,这个LinearLayout包含了多个TextView,每个TextView显示一行文本。运行这个应用,可以看到整个页面可以在垂直方向上滚动。 二、在滚动页面中嵌入ListView 有时候我们需要在滚动页面中嵌入ListView,这时候可以使用Android系统提供的NestedScrollView组件。NestedScrollView是ScrollView的子类,可以包含多个子视图,并且可以在垂直方向上进行滚动。和ScrollView不同的是,NestedScrollView可以嵌套其他可滚动的组件,例如ListView等。 下面是一个简单的例子: ``` <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第一行"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@null" android:dividerHeight="0dp" android:scrollbars="none"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第三行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第四行"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是第五行"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> ``` 上面的代码中,我们将NestedScrollView作为根布局,然后在NestedScrollView内部添加了一个垂直方向的LinearLayout。这个LinearLayout包含了多个TextView和一个ListView。由于ListView也可以滚动,所以我们需要将它的滚动条隐藏掉,然后就可以在滚动页面中嵌入ListView了。 以上就是Android开发中如何实现全屏滚动和在滚动页面中嵌入ListView的方法。希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值