ListView/RecyclerView-Android

ListView

  • 为ListView的子项指定一个我们自定义的布局,在layout目录中新建fruit_item.xml
  • 创建一个自定义的适配器,这个适配器继承自ArrayAdapter
public class FruitAdapter extends ArrayAdapter {
    public FruitAdapter(@NonNull Context context, int resource, @NonNull List objects) {
        super(context, resource, objects);
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        FruitModel fruit = (FruitModel) getItem(position);
        View view; 
        ViewHolder viewHolder;
       //提升ListView的运行效率,将之前加载好的布局进行缓存,convertView不为null则对convertView进行重用
        if (convertView == null) {
            //第三个参数设为false,表示只让我们在父布局中声明layout属性生效,但不为这个view添加父布局,因为一旦View有了父布局之后,它就不能再添加到ListView中了
            view = LayoutInflater.from(getContext()).inflate(R.layout.fruit_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.fIndex = view.findViewById(R.id.fruit_index);
            viewHolder.fName = view.findViewById(R.id.fruit_name);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.fIndex.setText(position + ":");
        viewHolder.fName.setText(fruit.getName());
        return view;
    }

    //提升ListView的运行效率,对控件的示例进行缓存,没必要每次都通过findViewById()方法来获取控件实例
    class ViewHolder {
        TextView fIndex;
        TextView fName;
    }
}

RecyclerView

ListView的本身的性能非常差,我们不得不用一些技巧来提升它的运行效率。还有ListView的扩展性也不够好,它只能实现数据纵向滚动的效果,如果想实现横向滚动的话,ListVIew是做不到的。
为此,Android提供了一个更强大的滚动控件 RecyclerView,不仅可以实现ListView同样的效果,还优化了ListView中存在的各种不足之处。
RecyclerView属于新增的控件,为了让其在所有Android版本上都能使用,Android将RecyclerView定义在suppor库当中。

  • 打开app/build.gradle文件中,在dependencies闭包中添加
implementation 'com.android.support:recyclerview-v7:24.2.1'
  • 为RecyclerView准备适配器,继承自RecyclerView.Adapter。
public class FruitRecycleAdapter extends RecyclerView.Adapter {
    private List mFruitList;
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder mHolder = (ViewHolder)holder;
        FruitModel model = mFruitList.get(position);
        mHolder.fIndex.setText(position + "、");
        mHolder.fName.setText(model.getName());
    }

    @Override
    public int getItemCount() {
        return mFruitList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView fIndex;
        TextView fName;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            fIndex = itemView.findViewById(R.id.fruit_index);
            fName = itemView.findViewById(R.id.fruit_name);
        }
    }

    public FruitRecycleAdapter(List mFruitList) {
        this.mFruitList = mFruitList;
    }
}
  • Activity中
recycleView = findViewById(R.id.recycle_view);
 LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);   //横向排列
 recycleView.setLayoutManager(layoutManager);
 FruitRecycleAdapter adapter = new FruitRecycleAdapter(fruitList);
 recycleView.setAdapter(adapter);

除了LinearLayoutManager之外,RecyclerView还提供了GridLayoutManager(网格布局)和StaggeredGridLayoutManager(可实现瀑布流效果)

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recycleView.setLayoutManager(layoutManager);

你可能感兴趣的:(ListView/RecyclerView-Android)