普通的listview适配器

Posted Gradle官方文件

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了普通的listview适配器相关的知识,希望对你有一定的参考价值。

优化:
1:listview宽高定值活充满
2:复用convertview
3:使用内部类,避免重复找ID

适配器

public class MyAdapter extends BaseAdapter 
    private List<Student> datas;
    private LayoutInflater inflater;
    public MyAdapter(Context context,List<Student> datas) 
        this.datas = datas;
        inflater = LayoutInflater.from(context);
    

    @Override
    public int getCount() 
        return datas == null ? 0 : datas.size();
    

    @Override
    public Object getItem(int position) 
        return datas.get(position);
    

    @Override
    public long getItemId(int position) 
        return position;
    

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        ViewHolder viewHolder;
        if (convertView == null)
            convertView = inflater.inflate(R.layout.lv_item,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
            //对于listview,注意添加这一行,即可在item上使用高度
            AutoUtils.autoSize(convertView);
        else 
            viewHolder = (ViewHolder) convertView.getTag();
        
        Student student = datas.get(position);
        viewHolder.getTv1().setText(student.getName());
        viewHolder.getTv2().setText(student.getNum());
        viewHolder.getRlItem().setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if (null != onItemClickListener)
                    onItemClickListener.onItemClickListener(position);
                
            
        );
        return convertView;
    

    public class ViewHolder
        private TextView tv1;
        private TextView tv2;
        private RelativeLayout rlItem;
        private View root;

        public ViewHolder(View root) 
            this.root = root;
        
        public RelativeLayout getRlItem()
            if (rlItem == null && root != null)
                rlItem = (RelativeLayout) root.findViewById(R.id.item_rl);
            
            return rlItem;
        
        public TextView getTv1()
            if (tv1 == null && root != null)
                tv1 = (TextView) root.findViewById(R.id.item_name);
            
            return tv1;
        
        public TextView getTv2()
            if (tv2 == null && root != null)
                tv2 = (TextView) root.findViewById(R.id.item_num);
            
            return tv2;
        
    

    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener)
        this.onItemClickListener = onItemClickListener;
    
    public interface OnItemClickListener
        public void onItemClickListener(int position);
    

单行布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/item_rl"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="108px"
    android:background="#ffffff">
    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24px"
        android:layout_marginLeft="22px"
        android:textSize="28px"
        android:textColor="#333"
        android:text="张林周 - ZHANGLINZHOU"/>
    <TextView
        android:id="@+id/item_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="22px"
        android:layout_marginBottom="16px"
        android:textColor="#999"
        android:textSize="26px"
        android:text="身份证:3729231991999999999999"/>
</RelativeLayout>

使用:

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="26px">
    </ListView>
        mLV = ((ListView) findViewById(R.id.lv));
        mList = new ArrayList<>();
        mAdapter = new MyAdapter(this, mList);
        mLV.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() 
            @Override
            public void onItemClickListener(int position) 
                Toast.makeText(MainActivity.this, "点击了:" + position, Toast.LENGTH_SHORT).show();
            
        );
更新数据

    private void initData() 
        for (int i = 0; i < 5; i++) 
            final Student s = new Student(i,"张林周 - ZHANGLINZHOU" + i, "身份证 :3729231999999999999999" + i);
            mList.add(s);
            new Thread()
                @Override
                public void run() 
                    super.run();
                    DBUtils.getInstance().saveStudent(s);
                
            ;
        
        mAdapter.notifyDataSetChanged();
    

以上是关于普通的listview适配器的主要内容,如果未能解决你的问题,请参考以下文章

Android 自定义 ListView/Adapter

打造万能Adapter(适配器)——适用于ListViewGridListView

如何从适配器访问 ListView

Android的ListView——适配器模式

无法设置 ListView 的适配器,因为 ListView 有一个空对象引用

为 listview 定义 onlick:在 listView 级别的 Onclick 与自定义视图适配器内的 Onclick