zoukankan      html  css  js  c++  java
  • listview显示固定条数

    看了很多网上其他大神的,感觉还是在listview的adapter中的getCount中下手比较好点

    毕竟计算高度等等,那会让辅助的布局会一团糟,例如下面的搜索历史只显示四条,布局中有横向listview

    有gridview

    package com.tfot.hotel.yichengyiyu.Activity.zhou_activity.adapter;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.tfot.hotel.yichengyiyu.Activity.zhou_activity.data.RecordSQLiteOpenHelper;
    import com.tfot.hotel.yichengyiyu.R;
    
    import java.util.List;
    
    /**
     * Created by qpz on 2017-6-24.
     */
    
    public class SouSuoListAdapter extends BaseAdapter{
        private static final int MAX_ITEM_COUNT = 4;
    
        private Context context;
        private LayoutInflater layoutInflater;
        private List sousuo;
        private MyClickListener mListener;
    
        private SQLiteDatabase db;
        private RecordSQLiteOpenHelper helper;
        public SouSuoListAdapter(Context context,List sousuo, MyClickListener mListener){
            this.context = context;
            this.layoutInflater=LayoutInflater.from(context);
            helper = new RecordSQLiteOpenHelper(context);
            this.sousuo = sousuo;
            this.mListener = mListener;
        }
    
    
    
        /**
         * 组件集合,对应xml中的控件
         * @author Administrator
         */
        public final class DanGe{
            public  TextView activity_sousuolishi_neirong_tv;
            public ImageView sousuo_shanchuyitiao_iv;
        }
    
        /**
         * 设置listview显示的条数,为4条
         * @return
         */
        @Override
        public int getCount() {
            if (sousuo == null)
                return 0;
            return Math.min(MAX_ITEM_COUNT,sousuo.size());
        }
    
    
        @Override
        public Object getItem(int position) {
            if(sousuo.get(position)!=null&&!sousuo.isEmpty()){
                return sousuo.get(position);
            }
              return  null;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            DanGe danGe=null;
                if(convertView==null) {
                    danGe=new DanGe();
                    //获得组件,实例化组件
                    convertView =layoutInflater.inflate(R.layout.activity_sousuolishi, null);
                    danGe.sousuo_shanchuyitiao_iv = (ImageView) convertView.findViewById(R.id.sousuo_shanchuyitiao_iv);
                    danGe.activity_sousuolishi_neirong_tv = (TextView) convertView.findViewById(R.id.activity_sousuolishi_neirong_tv);
                    convertView.setTag(danGe);
                }else{
                    danGe = (DanGe) convertView.getTag();
                }
                danGe.activity_sousuolishi_neirong_tv.setText((String)sousuo.get(position));
                danGe.sousuo_shanchuyitiao_iv.setOnClickListener(mListener);
                danGe.sousuo_shanchuyitiao_iv.setTag(position);
                return convertView;
    
        }
        /**
         * 用于回调的抽象类
         */
        public static abstract class MyClickListener implements View.OnClickListener {
            /**
             * 基类的onClick方法
             */
            @Override
            public void onClick(View v) {
                myShanChuOnClick((Integer) v.getTag(), v);
            }
            public abstract void myShanChuOnClick(int position, View v);
        }
    
    }
  • 相关阅读:
    JVM原理---------------1.开篇
    mysql开启事务的方式,命令学习
    mysql中的锁
    mysql索引底层原理
    mysql的常见存储引擎与常见日志类型,以及4种线程的作用
    Mutex
    委托和匿名委托
    线程通信
    同步锁
    [ValidateInput(false)]
  • 原文地址:https://www.cnblogs.com/mrszhou/p/7101242.html
Copyright © 2011-2022 走看看