zoukankan      html  css  js  c++  java
  • android ListView_Tiger

    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的滑动,监听一个旧的条目消失,一个新的条目显示。

  • 相关阅读:
    【IDEA插件】—— 代码量统计工具Statistic
    【Funny Things】001——QQ循环发送消息
    【jmeter测试范例】001——TCP测试
    【Jmeter源码解读】003——TCP采样器代码解析
    【Jmeter源码解读】002——程序入口类NewDriver.java
    Eclipse点击空格总是自动补全代码怎么办,如何自动补全代码,代码提示
    路径中关于斜杠/和反斜杠 的区别
    eclipse查看JDK源码
    Navicat premium如何使用Oracle的OCI
    斐波那契查找不再迷惑
  • 原文地址:https://www.cnblogs.com/liangqiyuan/p/5692284.html
Copyright © 2011-2022 走看看