android ListView_Tiger

本文通过一个具体的例子展示了如何在Android应用中使用ListView组件。包括定义ListView布局、创建适配器及填充数据的过程。同时介绍了ListView的工作原理及其在MVC模式中的角色。

xml设计

<?xml version="1.0"?>

-<LinearLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="horizontal" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

<ListView android:id="@+id/lv_tiger1" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1"/>

<ListView android:id="@+id/lv_tiger2" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1"/>

<ListView android:id="@+id/lv_tiger3" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1"/>

</LinearLayout>
View Code

 

 

Main java

package com.itheima.tiger;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        //1.找到控件
        ListView lv_tiger1 = (ListView) findViewById(R.id.lv_tiger1);
        ListView lv_tiger2 = (ListView) findViewById(R.id.lv_tiger2);
        ListView lv_tiger3 = (ListView) findViewById(R.id.lv_tiger3);
        //创建adapter设置给listviev
        TigerAdapter tigerAdapter = new TigerAdapter();
        lv_tiger1.setAdapter(tigerAdapter);
        lv_tiger2.setAdapter(tigerAdapter);
        lv_tiger3.setAdapter(tigerAdapter);
    }

    class TigerAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 500;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            TextView view = null;
            //复用convertView
            if(convertView != null){
                view = (TextView) convertView;
            }else{
                view =     new TextView(mContext);
            }

            Random random = new Random();
            int number = random.nextInt(100);
            if(number <20){
                view.setTextColor(Color.parseColor("#ff00ff"));//设置textview文字颜色
                view.setText("桃");
            }else if(number < 40){
                view.setTextColor(Color.YELLOW);//设置textview文字颜色
                view.setText("杏");
            }else if(number <60){
                view.setTextColor(Color.GREEN);//设置textview文字颜色
                view.setText("梨");
            }else if(number <80){
                view.setTextColor(Color.RED);//设置textview文字颜色
                view.setText("枣");
            }else {
                view.setTextColor(Color.parseColor("#666666"));//设置textview文字颜色
                view.setText("瓜");
            }
            
            view.setTextSize(58);
            
            return view;
        }

    }
}
View Code

 

 

老师笔记

 

#7 listview---Tiger
    
    javaweb mvc
    m....mode....javabean
    v....view....jsp
    c....control...servlet
    
    listview mvc
    m....mode....Bean
    v....view....listview
    c....control...adapter
     

#8 listview显示原理 (了解)
    1.要考虑listview显示的条目数    getcount
    2.考虑listview每个条目显示的内容   getview
    3.考虑每个item的高度,因为屏幕的多样化
    4.还要考虑listview的滑动,监听一个旧的条目消失,一个新的条目显示。

转载于:https://www.cnblogs.com/liangqiyuan/p/5692284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值