laitimes

ListView分页(带图片)显示用法案例

listview是android中最为常用的列表类型控件,listview中的选择项目中样式很多有的是纯文字的、有的还可以带有图片。它的继承关系如下:

java.lang.object

   ↳ android.view.view

     ↳ android.view.viewgroup

       ↳ android.widget.adapterview<t extends android.widget.adapter>

         ↳ android.widget.abslistview

           ↳ android.widget.listview

android.widget.listview继承了android.view.viewgroup。

首先看一个纯文本的listview例子,案例运行后会出现一个城市列表如图6-8所示,选择某个城市,弹出一个toast,关于toast的概念和使用会在下一节中介绍。

ListView分页(带图片)显示用法案例

图6-8 listview

程序代码请参考代码清单6-4:

【代码清单6-4】 chapter6_3/src/com/work/listview_1_activity.java

public class listview_1_activity extends activity {

private listview listview;

@override

    public void oncreate(bundle savedinstancestate) {

        super.oncreate(savedinstancestate);

        setcontentview(r.layout.listview_activity);

        listview = (listview)findviewbyid(r.id.listview01);

        arrayadapter<string> adapter = new arrayadapter<string>(this,

                android.r.layout.simple_list_item_1, mstrings);

        listview.setadapter(adapter);

   listview.setonitemclicklistener(new adapterview.onitemclicklistener() {

        @override

    public void onitemclick(adapterview<?> parent, view v, int pos,

    long id) {

        toast.maketext(listview_1_activity.this, mstrings[pos],

toast.length_short).show();

    }

    });

    }

    private string[] mstrings = {

            "北京市", "天津市", "上海", "重庆", "乌鲁木齐", …};

}

对于arrayadapter应该已经很熟悉了,其中的android.r.layout.simple_list_item_1是使用系统的布局样式。android系统本身提供了很多的这样的布局文件,但是有的适合于listview控件,有的适合于spinner控件,有的适合于它的列表控件,这是使用时需要注意的。

在这种方式下,需要在布局文件listview_activity.xml中添加listview控件:

<listview android:id="@+id/listview01" android:layout_width="wrap_content"

android:layout_height="wrap_content"></listview>

由于listview在android中是很常用的列表类型控件,只要是有多条信息需要显示的时候都可以考虑使用listview展示出来,正是由于listview使用的普遍,所以android又提供了一个列表类型的activity——listactivity,来简化listview开发。

通过继承listactivity类而实现一个简单的listview功能,而不要直接使用listview控件。同样上面案例如果使用listactivity请参考代码清单6-5的写法:

【代码清单6-5】 chapter6_3/src/com/work/listview_1.java

public class listview_1 extends listactivity {

    /** called when the activity is first created. */

    @override

        setlistadapter(new arrayadapter<string>(this,

                android.r.layout.simple_list_item_1, mstrings));

        getlistview().setonitemclicklistener(new adapterview.onitemclicklistener() {

        toast.maketext(listview_1.this, mstrings[pos],

查看代码不难发现这里没有使用布局文件,那就意味着不需要使用r文件来获得控件,所以在程序中使用了getlistview()方法来获得listview控件。处理listview的项目点击事件有两种方法,一种是通过与listview对象设置setonitemclicklistener方式实现,代码如下:

getlistview().setonitemclicklistener(new adapterview.onitemclicklistener() {

     @override

});

另外一种是覆盖listactivity的onlistitemclick(listview l, view v, int position, long id)方法实现,代码如下所示。

protected void onlistitemclick(listview l, view v, int position, long id) {

toast.maketext(listview_1.this, mstrings[position], toast.length_short)

.show();

再看一个自定义adapter的例子,这是一个带有图标的listview,程序运行结果如图6-9所示。

ListView分页(带图片)显示用法案例

图6-9 自定义adapter

相关程序代码请参考代码清单6-6:

【代码清单6-6】 chapter6_3/src/com/work/listviewicon_3.java

public class listviewicon_3 extends listactivity {

        setlistadapter(new efficientadapter(this));

    private static final string[] data = {

    "北京市", "天津市", "上海", "重庆", "哈尔滨",

         "石家庄", "秦皇岛", "济南", "青岛", "南京",

         "三亚", "昆明", "成都", "长沙", "武汉",

         "九江", "香港", "澳门","兰州","张家口" };

自定义的adapter是efficientadapter,efficientadapter的相关代码请参考代码清单6-7:

【代码清单6-7】 chapter6_3/src/com/work/listviewicon_3.java

private static class efficientadapter extends baseadapter {

        private layoutinflater minflater;

        private bitmap micon0;

        private bitmap micon1;

…  …

        public efficientadapter(context context) {

            minflater = layoutinflater.from(context);

            micon0 = bitmapfactory.decoderesource(context.getresources(), r.drawable.noicon);

            micon1 = bitmapfactory.decoderesource(context.getresources(), r.drawable.beijing);

…   …

      }

        public int getcount() {

            return data.length;

        }

        public object getitem(int position) {

            return data[position];

        public long getitemid(int position) {

            return position;

        public view getview(int position, view convertview, viewgroup parent) {

            viewholder holder;

            if (convertview == null) {

                convertview = minflater.inflate(r.layout.main, null);

                holder = new viewholder();

                holder.text = (textview) convertview.findviewbyid(r.id.textview);

                holder.icon = (imageview) convertview.findviewbyid(r.id.icon);

                convertview.settag(holder);

            } else {

                holder = (viewholder) convertview.gettag();

            }

            holder.text.settext(data[position]);

            switch(position)

            {

            case 0:

            holder.icon.setimagebitmap(micon1);

            break;

            case 1:

            holder.icon.setimagebitmap(micon2);

          …

            default:

            holder.icon.setimagebitmap(micon0);

            return convertview;

        static class viewholder {

            textview text;

            imageview icon;

编写自定义adapter可以继承baseadapter类,如果是数据库使用可以继承cursoradapter。在本例中继承了baseadapter类,baseadapter是一个抽象类,必须在它的子类中实现下面的方法:

• int getcount() 返回总数据源中总的记录数;

• object getitem(int position) 根据选择的项目的位置,获得选择的数据源中某个项目的数据;

• long getitemid(int position) 根据选择的项目的位置;

• view getview(int position, view convertview, viewgroup parent) 获得要展示的项目view对象。

这里最为麻烦的方法就是getview(),getview()方法是listview的每个列表项目绘制在屏幕上时被调用。该方法其中的一个参数是convertview,在listview第一次显示列表项目的时候,convertview是null值。当向上滑动屏幕时候,屏幕上面的列表项目退出屏幕,屏幕下面原来不可见的列表项目会进入屏幕,这个时候的convertview不是null值,下面代码的处理对于提供listview控件提高性能是至关重要的。

if (convertview == null) {

convertview = minflater.inflate(r.layout.main, null);

holder = new viewholder();

holder.text = (textview) convertview

.findviewbyid(r.id.textview);

holder.icon = (imageview) convertview.findviewbyid(r.id.icon);

convertview.settag(holder);

} else {

holder = (viewholder) convertview.gettag();

只有在convertview为null时才去实例化控件,创建convertview对象、holder对象,其中convertview对象是通过minflater.inflate(r.layout.main, null)方法,从一个main.xml布局文件中加载并创建的。

而在convertview非null的时候不会实例化控件,否则每次都要实例化控件,当列表项目很多时,用户反复滑动屏幕会有“卡”的感觉,不再流畅了。

viewholder类是将每一个项目中的控件封装起来的类,可以在convertview 为null时候创建viewholder类的实例holder,然后通过convertview.settag(holder);把它放到convertview中,而在convertview非null时候,再通过convertview.gettag()过的一个viewholder类的实例,这样在翻屏的时候就不会反复创建viewholder实例对象了,就本例而言只是创建了9个viewholder实例。

                                                                                       出自《android开发案例驱动教程》第六章

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Everyone has a love for beauty. But in the same middle-aged woman, some people look young and beautiful when they dress up, and they are particularly temperamental, but they look old-fashioned and greasy and cheap after wearing them, what are the reasons? It is a question that many people are concerned about. Let's take a look at how amateur bloggers can dress themselves up more beautifully.

For example, the amateur blogger below uses a brown knitted vest to layer a white shirt and a pleated skirt to create an elegant visual effect

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

First, try to wear loose but not tight

Why is it recommended that everyone dress up, try to wear loose and not tight, the reason is also very simple, after all, at this stage of middle age, many women's figures are out of shape, more small belly or wide hips and thick legs and other problems are exposed, if you are still wearing tight clothes at this time or leggings tight skirts, then it is strange that it does not look rustic and cheap.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Under normal circumstances, we have to choose clothes that suit us according to our body proportions, for example, the following blogger chooses a sleeveless shirt or polka dot shirt, with a straight skirt, and the corners of the shirt are tucked into the skirt, which is a good way to enhance the waistline, look tall and thin, and it is also a small amateur blogger, it looks not short, but also very tall and thin. This is one of the charms of dressing up.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

When choosing a skirt, try to cover the position from the knee to the calf, and expose the ankle, so that you don't press down after wearing it, the right steak can extend the leg line, and after the shallow single shoes, your lower body will be more slender.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Wearing a skirt also has an advantage, it can hide the thick legs and wide hips and other problems well, which is one of the reasons why it is recommended that you wear loose and not tight, and it is also recommended that you choose a pleated skirt or a pleated skirt.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Second, dress up as much as possible without fancy colors

Sometimes the more simple the color of the clothes we choose, the more you can wear a sense of luxury, if your clothes are too fancy, you will look rustic and cheap after wearing them, we observe that many women around you are very good at dressing up, and the color of the dress is relatively simple.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

For example, the following blogger uses a white shirt with a blue undershirt and a white skirt, such a solid color combination is very atmospheric, and the second set is matched with a yellow shirt and a light yellow skirt, so that the color of the dress modifies the skin tone to be white and tender.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Of course, you can also choose a black dress directly, for example, the following blogger used a black dress with a shirt to match, plus a small black belt, you can easily enhance the waistline, and the other set is also matched with an apricot dress, so that the basic tone who wears it looks good.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

If you want to better modify your body proportions and make your height and figure more coordinated, we can also use the matching skills of outer short and inner length, for example, the following blogger uses an apricot blazer with a light green dress inside, and the style of the dress is a high-waisted and waist-cinching version, so that you can achieve the matching of outer short and inner length.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Of course, the length of the skirt is not absolute, for example, you are indeed a very self-disciplined woman, often go to the gym to work out or do yoga and aerobics at home, and you can also choose a short skirt with a good body proportion.

For example, the amateur blogger on the left below chooses a small fragrant skirt with boots, such a dressing style belongs to the lower body fashion, and the dressing skills show off his long legs very well, and he is tall and thin.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

We can also use the matching technique of tight top and bottom reversal, such as using a green shirt with a floral skirt, that is, putting the cumbersome skirt on the lower body, your visual center of gravity will move downward, and it will not look too cumbersome, very delicate.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

However, when choosing a floral skirt or a printed skirt, you need to pay attention to the material and design style of the skirt, and it can't look too messy, as tired as the blogger on the right, using a light yellow sleeveless shirt, with a large printed skirt, and a pair of shallow shoes on his feet, this kind of dressing still looks very atmospheric, especially suitable for middle-aged women.

If your age looks younger and your figure is better, you can try this set on the left, with a light yellow chiffon shirt and a floral A-line skirt, this kind of dressing is also very atmospheric, very exquisite, girly, light and beautiful.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Third, what shoes look better with a skirt?

I personally recommend that you choose low heels, try not to wear high heels, after wearing high heels on the feet, the stability is not good, easy to grind the feet or broken feet, daily life or wear low heels, flat shoes, Mary Jane shoes, loafers are more suitable.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

If you really like to wear stilettos, try to control the heel at 3 to 5 cm is the best, you, the top pair of high-heeled sandals, or the lower pair of low-heeled shoes, and the floral skirt, plus the shirt combination, the visual effect is indeed more feminine.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

However, the choice of the material of the single shoe is also very important, such as choosing more sheepskin materials, cowhide material pumps to wear more difficult to deform, and very durable, but also to enhance the level of the whole outfit.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

We should also add some accessories when dressing, such as adding a straw hat, so that the whole look is full of atmosphere. If you're wearing a V-neck shirt or knitwear, add a small necklace around your neck to make your look more sophisticated.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

Therefore, if you want the whole dressing style to look better and more fashionable, you have to work the details, whether it is hairstyle or makeup, we have to try more, and you can also directly find your own style and make up for your shortcomings according to the successful cases of amateur bloggers, which is also a method.

is also a middle-aged woman, why do others look young, but you look old? The reasons are all in these 3 points

The easier it is to dress up, the less likely it is to make mistakes. For example, the following amateur bloggers' outfits give such a clear answer, if you also like the above amateur bloggers' dressing skills, try it quickly, you can also become beautiful.

Statement: The original picture of the article comes from the Internet, if there is any infringement, please contact to delete, thank you.

最近更新