ListView和RecycleView使用

ListView

  1. 定义list中基本项的布局方式

    
    
    
        
    
        
    
    
  2. 定义list中基本项对应的类

    public class Fruit {
        private String name;
    
        public Fruit(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
  3. 在需要添加list的活动布局内添加ListView

    
    
    
    
        
    
    
    
  4. 编写一个适配器类,该适配器类继承自ArrayAdapter类,适配器类内重写需要的构造方法,并且需要重写ArrayAdapter类的getView方法

    public class FruitAdapter extends ArrayAdapter {
        private int resourceId;
    
        public FruitAdapter(@NonNull Context context, int resource, @NonNull List objects) {
            super(context, resource, objects);
            resourceId = resource;
        }
        
         @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
         Fruit fruit = getItem(position);
            ViewHolder viewHolder;
            //减少实例化view次数
            if (convertView == null) { 
                view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                //减少
                viewHolder = new ViewHolder();
                viewHolder.imageView = (ImageView) view.findViewById(R.id.fruit_item_iv_1);
                viewHolder.textView = (TextView) view.findViewById(R.id.fruit_item_tv_1);
                view.setTag(viewHolder)
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.imageView.setImageResource(R.drawable.ic_launcher);
            viewHolder.textView.setText(fruit.getName());
            //TextView textView = (TextView) findViewById(R.id.fruit_item_tv_1);
            //textView.setText(fruit.getName());
            return view;
        }
        
        class ViewHolder {
            ImageView imageView;
            TextView textView;
        }
    }
    
    
    

    实例化该适配器类时需要传入需要传入基本项布局资源id,将该布局资源id保存起来;重写getVIew方法内,首先获取了传入的 objects对应 position位置的基本项类;判断基本布局项之前是否已经加载过,如果没有,通过LayoutInflater找到基本项布局并将其实例化,如果有的化,直接获取该基本布局项即可;通过基本项布局找到textview控件,将控件中的内容设置为拿到实体类内对应的内容;最后将实例化的布局返回;

  5. 再活动内使用

    public class FruitActivity extends AppCompatActivity {
        private String[] data = {"Apple", "Banana", "Orange", "Pear", "Grape"};
        private List fruits = new ArrayList<>();
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fruit);
            init();
            FruitAdapter fruitAdapter = new FruitAdapter(this, R.layout.fruit_item, fruits);
            ListView listView = (ListView) findViewById(R.id.activity_fruit_lv_1);
            listView.setAdapter(fruitAdapter);
            //添加监听事件
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    Fruit fruit = fruits.get(position);
                    Toast.makeText(FruitActivity.this, "111111111111", Toast.LENGTH_SHORT).show();
                }
            });
        }
     
     //往fruits内填充数据
        public void init() {
            for (int i = 0; i< 4; i++) {
                for (int j=0; j< data.length; j++) {
                    fruits.add(new Fruit(data[j]));
                }
            }
        }
    }
    

RecycleView

  1. 添加recycleView依赖

    `implementation 'androidx.recyclerview:recyclerview:1.1.0'

  2. 定义list中基本项的布局方式,同上

    
    
    
        
    
        
    
    
  3. 在需要添加list的活动布局中添加recycleView

    
    
    
        
    
    
    
    
  4. 创建一个适配器类,该类继承自RecyclerView.Adapter,并重写其onCreateViewHolder, onBindViewHolder, getItemCount方法

    public class RvFruitAdapter extends RecyclerView.Adapter {
        private List mFruitList;
    
        static class ViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView textView;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = (ImageView) itemView.findViewById(R.id.fruit_item_iv_1);
                textView = (TextView) itemView.findViewById(R.id.fruit_item_tv_1);
            }
        }
    
        public RvFruitAdapter(List fruits) {
            mFruitList = fruits;
        }
    
     //若需要添加监听事件,在该函数内添加即可(通过view拿到组件添加监听事件)
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Fruit fruit = mFruitList.get(position);
            holder.imageView.setImageResource(R.mipmap.ic_launcher);
            holder.textView.setText(fruit.getName());
        }
    
        @Override
        public int getItemCount() {
            return mFruitList.size();
        }
    
    
    }
    

    getItemCount方法需要返回列表长度,该方法将影响界面最终返回的基本项数量

  5. 在活动内引用

    public class RecycleViewActivity extends AppCompatActivity {
        private String[] data = {"Apple", "Banana", "Orange", "Pear", "Grape"};
        private List fruits = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycle_view);
            init();
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_recycle_view_rv_1);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            //网格布局
            //GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
            //瀑布布局
            //StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(new RvFruitAdapter(fruits));
        }
    
        public void init() {
            for (int i = 0; i< 4; i++) {
                for (int j=0; j< data.length; j++) {
                    fruits.add(new Fruit(data[j]));
                }
            }
        }
    }
    

你可能感兴趣的:(ListView和RecycleView使用)