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

  • 相关阅读:
    jQuery 源码解析(二十四) DOM操作模块 包裹元素 详解
    jQuery 源码解析(二十三) DOM操作模块 替换元素 详解
    jQuery 源码解析(二十二) DOM操作模块 复制元素 详解
    jQuery 源码分析(二十一) DOM操作模块 删除元素 详解
    jQuery 源码分析(二十) DOM操作模块 插入元素 详解
    jQuery 源码分析(十九) DOM遍历模块详解
    python 简单工厂模式
    python 爬虫-协程 采集博客园
    vue 自定义image组件
    微信小程序 image组件坑
  • 原文地址:https://www.cnblogs.com/liangqiyuan/p/5692284.html
Copyright © 2011-2022 走看看