首页 > 代码库 > listview显示固定条数

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);
    }

}

 

listview显示固定条数