ListView和RecyclerView

ListView

ListView 性能如何优化 (主要在getView方法上优化:复用view,ViewHolder)
SampleCode

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = getItem(position);
        View v;
        ViewHolder holder;
        if(convertView == null) {
            v = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.textView = v.findViewById(R.id.list_text);
            holder.imageView = v.findViewById(R.id.list_image);
            v.setTag(holder);
        } else {
            v = convertView;
            holder = (ViewHolder) v.getTag();
        }
        holder.imageView.setImageResource(fruit.id);
        holder.textView.setText(fruit.name);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "okok", Toast.LENGTH_SHORT).show();
            }
        });
        return v;
    }
    
    private class ViewHolder {
        TextView textView;
        ImageView imageView;
    }

RecyclerView

RecyclerView的基类已经封装好了,我们必须定义ViewHolder以提升效率,因此无需特别注意。
SampleCode of RecyclerAdapter

public class MyRecyclerAdapter extends RecyclerView.Adapter {
    List mFruitList;

     static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.list_image);
            textView = itemView.findViewById(R.id.list_text);
        }
    }

    public MyRecyclerAdapter(List mFruitList) {
        this.mFruitList = mFruitList;
    }

    @NonNull
    @Override
    public MyRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.list_item, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder viewHolder, int i) {
            Fruit fruit = mFruitList.get(i);
            viewHolder.textView.setText(fruit.name);
            viewHolder.imageView.setImageResource(fruit.id);
    }

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

SampleCode of RecyclerView in a Activity(包含LayoutManager用法)

public class ListViewTestActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_test);
        initView();
    }

    private void initView() {
        // recyclerView and adapter can be set as global variables, this is just a demo
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        
        //List should be filled with data, this is just a demo
        MyRecyclerAdapter adapter = new MyRecyclerAdapter(new ArrayList());
        layoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
}

作业:

简易聊天应用界面

你可能感兴趣的:(ListView和RecyclerView)