• 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);
        }
    
    }
  • 相关阅读:
    【机器学习】Softmax 和Logistic Regression回归Sigmod
    【LDA】线性判别式分析
    【MLE】最大似然估计Maximum Likelihood Estimation
    n阶方阵A可逆充分必要条件
    【机器学习】K-Means算法
    【X-Forwarded-For】WEB修改访客IP
    【Ubuntu】ubuntu系统下python3和python2环境自由切换
    【Python】打印object对象
    【linux】dpkg info修复及dpkg: warning: files list file for package
    【php】https请求
  • 原文地址:https://www.cnblogs.com/mrszhou/p/7101242.html
Copyright © 2020-2023  润新知