相关文章推荐
冲动的显示器  ·  Uses of Interface ...·  1 年前    · 
帅气的青蛙  ·  一步步学OpenGL(39) ...·  1 年前    · 
玩足球的莴苣  ·  在OSS原图上自定义裁剪_对象存储 ...·  2 年前    · 
欢乐的打火机  ·  一个基于Python数据大屏可视化开源项目 ...·  2 年前    · 
爽快的咖啡豆  ·  值得收藏!16段代码入门Python循环语句 ...·  2 年前    · 
Code  ›  android-RecyclerView开发者社区
android recyclerview viewholder
https://cloud.tencent.com/developer/article/2050060
打篮球的香烟
1 年前
tea9

android-RecyclerView

腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
tea9
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > android-RecyclerView

android-RecyclerView

作者头像
tea9
发布 于 2022-07-16 17:00:23
243 0
发布 于 2022-07-16 17:00:23
举报
文章被收录于专栏: tea9的博客 tea9的博客

引入RecyclerView包 compile ‘com.android.support:recyclerview-v7:25.1.0’

main_layout布局

垂直
垂直
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.recyclerviewtest.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

fruit_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"/>
</LinearLayout>

Fruit .class

public class Fruit {
    private String name;
    private int imageId;
    public Fruit(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    public String getName() {
        return name;
    public int getImageId() {
        return imageId;
}

适配器 首先继承RecyclerView.Adapter

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    private List<Fruit> mFruitList;
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(View itemView) {
            super(itemView);
            fruitImage = (ImageView) itemView.findViewById(R.id.fruit_image);
            fruitName = (TextView) itemView.findViewById(R.id.fruit_name);
    public FruitAdapter(List<Fruit> fruitList) {
        mFruitList = fruitList;
    @Override
    public ViewHolder onCreateViewHolder(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(ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
    @Override
    public int getItemCount() {
        return mFruitList.size();
}

MainActivity

private List<Fruit> fruitList = new ArrayList<>();

oncreate

    initFruits();
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    FruitAdapter adapter = new FruitAdapter(fruitList);
    recyclerView.setAdapter(adapter);
private void initFruits() {
    for (int i = 0; i < 2; i++) {
        Fruit apple = new Fruit("appale", R.mipmap.ic_launcher);
        fruitList.add(apple);
        Fruit banana = new Fruit("banana", R.mipmap.ic_launcher);
        fruitList.add(banana);
        Fruit orange = new Fruit("orange", R.mipmap.ic_launcher);
        fruitList.add(orange);
        Fruit watermelon = new Fruit("watermelon", R.mipmap.ic_launcher);
        fruitList.add(watermelon);
        Fruit pear = new Fruit("pear", R.mipmap.ic_launcher);
        fruitList.add(pear);
        Fruit grape = new Fruit("grape", R.mipmap.ic_launcher);
        fruitList.add(grape);
        Fruit pineapple = new Fruit("pineapple", R.mipmap.ic_launcher);
        fruitList.add(pineapple);
        Fruit strawberry = new Fruit("strawberry", R.mipmap.ic_launcher);
        fruitList.add(strawberry);
        Fruit cherry = new Fruit("cherry", R.mipmap.ic_launcher);
        fruitList.add(cherry);
        Fruit Mango = new Fruit("Mango", R.mipmap.ic_launcher);
        fruitList.add(Mango);
}

把垂直布局修改成水平

recyclerview
recyclerview
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

修改布局item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="100dp"
              android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"/>
</LinearLayout>

瀑布流

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

item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="5dp">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginTop="10dp"/>
</LinearLayout>

点击事件

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    private List<Fruit> mFruitList;
    static class ViewHolder extends RecyclerView.ViewHolder {
        View fruitView;
        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(View itemView) {
            super(itemView);
            fruitView = itemView;
            fruitImage = (ImageView) itemView.findViewById(R.id.fruit_image);
            fruitName = (TextView) itemView.findViewById(R.id.fruit_name);
    public FruitAdapter(List<Fruit> fruitList) {
        mFruitList = fruitList;
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fruit_item2, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        // 点击view
        holder.fruitView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(), "you clicked view" + fruit.getName(), Toast.LENGTH_SHORT).show();
        // 点击图片
        holder.fruitImage.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(), "you clicked image" + fruit.getName(), Toast.LENGTH_SHORT).show();
        return holder;
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
    @Override
 
推荐文章
冲动的显示器  ·  Uses of Interface javax.print.PrintService
1 年前
帅气的青蛙  ·  一步步学OpenGL(39) -《模型轮廓识别检测[阴影锥1]》 - 知乎
1 年前
玩足球的莴苣  ·  在OSS原图上自定义裁剪_对象存储 OSS-阿里云帮助中心
2 年前
欢乐的打火机  ·  一个基于Python数据大屏可视化开源项目 - 知乎
2 年前
爽快的咖啡豆  ·  值得收藏!16段代码入门Python循环语句! - 知乎
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号