RecyclerView滑动图片加文字

1.打开app/build.gradle文件,在dependencies闭包中添加如下内容:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    **compile 'com.android.support:recyclerview-v7:24.2.1'**
    compile 'com.android.support:appcompat-v7:24.2.1'
}

添加完后,要记得Sync Now来进行同步。

然后修改main_activity.xml代码:




    

    

2,准备fruit_item.xml布局,实现左右滑动效果:

3,为RecyclerView准备一个适配器:

package com.gyq.recyclerviewtest.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gyq.recyclerviewtest.R;
import com.gyq.recyclerviewtest.bean.Fruit;

import java.util.List;

/**
 * ${DESC}
 * author: gyq
 * create at 2016/12/12 18:51
 */
public class FruitAdapter extends RecyclerView.Adapter {
    private List 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.iv_item_icon);
            fruitName = (TextView)itemView.findViewById(R.id.tv_item_name);
        }
    }

    public FruitAdapter(List fruitInfo) {
        mFruitList = fruitInfo;
    }

    @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();
    }


}

解释一下上面代码:首先定义了一个内部类ViewHolder,其继承RecyclerView.ViewHolder。然后ViewHolder的构造函数中需要传入一个view参数;

4,适配器准备好了,接下来我们就来使用RecyclerView:

package com.gyq.recyclerviewtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.gyq.recyclerviewtest.adapter.FruitAdapter;
import com.gyq.recyclerviewtest.bean.Fruit;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private List fruitInfo = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFruit();

        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        //设置水平滚动
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);

        FruitAdapter adapter = new FruitAdapter(fruitInfo);
        recyclerView.setAdapter(adapter);
    }

    private void initFruit() {
        for (int i = 0; i < 2;i++) {
            Fruit apple = new Fruit("apple",R.drawable.apple);
            fruitInfo.add(apple);
            Fruit apricot = new Fruit("apricot"),R.drawable.apricot);
            fruitInfo.add(apricot);
            Fruit banana = new Fruit("banana"),R.drawable.banana);
            fruitInfo.add(banana);
            Fruit cherry = new Fruit("cherry"),R.drawable.cherry);
            fruitInfo.add(cherry);
            Fruit kiwi = new Fruit("kiwi"),R.drawable.kiwi);
            fruitInfo.add(kiwi);
            Fruit lemon = new Fruit("lemon"),R.drawable.lemon);
            fruitInfo.add(lemon);
            Fruit mango = new Fruit("mango"),R.drawable.mango);
            fruitInfo.add(mango);
            Fruit orange = new Fruit("orange"),R.drawable.orange);
            fruitInfo.add(orange);
            Fruit peach = new Fruit("peach"),R.drawable.peach);
            fruitInfo.add(peach);
            Fruit pear = new Fruit("pear"),R.drawable.pear);
            fruitInfo.add(pear);
            Fruit strawberry = new Fruit("strawberry"),R.drawable.strawberry);
            fruitInfo.add(strawberry);
            Fruit tomato = new Fruit("tomato"),R.drawable.tomato);
            fruitInfo.add(tomato);
        }

    }
}

5,还有一个bean类

package com.gyq.recyclerviewtest.bean;

/**
 * ${DESC}
 * author: gyq
 * create at 2016/12/12 17:02
 */
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;
    }

}

 

你可能感兴趣的:(android)