当前位置:   article > 正文

Android Studio ListView框架+优化安卓应用市场实例_android studio list框架

android studio list框架

直接使用ListView的基本步骤

获取数据
创建适配器
绑定适配器
需要实现重写的函数:
getCount() 获取数据长度
getItem (int position) 获取项资源
getItemId(int position) 返回解析数据项ID
getView () 把项资源解析成可见视图

直接使用ListView的源代码

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private ListView mListView;
    //需要适配的数据
    private String[] names = {"京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫",
            "UC浏览器", "微信"};
    //图片集合
    private int[] icons = {R.drawable.jd, R.drawable.qq, R.drawable.dz,
            R.drawable.xl, R.drawable.tm, R.drawable.uc,R.drawable.wx};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化ListView控件
        mListView = (ListView) findViewById(R.id.lv);
        //创建一个Adapter的实例
        MyBaseAdapter mAdapter = new MyBaseAdapter();
        //设置Adapter
        mListView.setAdapter(mAdapter);
    }
    class MyBaseAdapter extends BaseAdapter {
        //得到item的总数
        @Override
        public int getCount() {
            //返回ListView Item条目的总数
            return names.length;
        }
        //得到Item代表的对象
        @Override
        public Object getItem(int position) {
            //返回ListView Item条目代表的对象
            return names[position];
        }
        //得到Item的id
        @Override
        public long getItemId(int position) {
            //返回ListView Item的id
            return position;
        }
        //得到Item的View视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getApplicationContext()).
                        inflate(R.layout.list_item,parent,false);
                holder = new ViewHolder();
                holder.mTextView = (TextView)convertView.findViewById
                        (R.id. item_tv);
                holder.imageView=(ImageView) convertView.findViewById(R.id.item_image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTextView.setText(names[position]);
            holder.imageView.setBackgroundResource(icons[position]);
            return convertView;
        }
        class ViewHolder {
            TextView mTextView;
            ImageView imageView;
        }


    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

自定义ListView步骤

父适配器:
继承自BaseAdapter

package com.example.androidapplicationmarket;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


public abstract class DefaultAdapter<T> extends BaseAdapter {
    private String Info[]=null;
    Context context;
    public DefaultAdapter(String[] Info, Context context) {
        this.Info = Info;
        this.context = context;
    }

    public DefaultAdapter() {
        super();
    }

    @Override
    public int getCount() {
        return Info.length;
    }

    @Override
    public Object getItem(int position) {
        return Info[position];
    }

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


    public abstract BaseHolder<T>getHolder();
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       BaseHolder<T>holder=null;
        if (convertView == null) {
           holder=getHolder();
           //holder.initView();
           convertView=holder.getView();
        }
        holder=(BaseHolder<T>)convertView.getTag();
        holder.initData(position);
        return convertView;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

自定义适配器:
为方便使用,隐藏四个需要重写的函数,只需继承父适配器

package com.example.androidapplicationmarket;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends DefaultAdapter {

    public MyAdapter(String[] Info, Context context,String names[],int icons[]) {
        super(Info, context);
        this.names=names;
        this.icons=icons;
    }

    //需要适配的数据
    private String[] names ;
    //图片集合
    private int[] icons ;
//
//    public String[] getNames() {
//       return names;
//   }
//
//   public void setNames(String[] names) {
//        this.names = names;
//   }
//
//    public int[] getIcons() {
//        return icons;
//   }
//
//    public void setIcons(int[] icons) {
//        this.icons = icons;
//   }


    @Override
    public BaseHolder getHolder() {
        return new MyHolder();
    }
    private class MyHolder extends BaseHolder{
    //绑定数据
        private TextView tv;
        private ImageView iv;
        @Override
        public void initView() {
            view= View.inflate(context,R.layout.list_item,null);
            tv=(TextView)view.findViewById(R.id.item_tv);
            iv=(ImageView)view.findViewById(R.id.item_image);
            view.setTag(this);
        }

        @Override
        public void initData(int position) {
            tv.setText(names[position]);
            iv.setBackgroundResource(icons[position]);

        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
package com.example.androidapplicationmarket;

import android.view.View;

public abstract class BaseHolder<T> {
    protected View view=null;
    public BaseHolder(){
        initView();
    }
    public abstract void initView() ;

    public View getView() {
        return view;
    }

    public abstract void initData(int position) ;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

绑定数据模板类(数据初始化)

package com.example.androidapplicationmarket;

import android.view.View;

public abstract class BaseHolder<T> {
    protected View view=null;
    public BaseHolder(){
        initView();
    }
    public abstract void initView() ;

    public View getView() {
        return view;
    }

    public abstract void initData(int position) ;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

界面交互函数
使用自定义适配器

package com.example.androidapplicationmarket;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ListView mListView;
    //需要适配的数据
    private String[] names = {"京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫",
            "UC浏览器", "微信"};
    //图片集合
    private int[] icons = {R.drawable.jd, R.drawable.qq, R.drawable.dz,
            R.drawable.xl, R.drawable.tm, R.drawable.uc,R.drawable.wx};
    //MyAdapter myAdapter=new MyAdapter(names,icons);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化ListView控件
        mListView = (ListView) findViewById(R.id.lv);
        //创建一个Adapter的实例
      MyAdapter myAdapter = new MyAdapter(names,this,names,icons);
        //设置Adapter
        mListView.setAdapter(myAdapter);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

运行结果

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号