1. 程式人生 > >RecyclerView滑動圖片加文字

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程式碼:

<?xml version="1.0" encoding="utf-8"?>
<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.gyq.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>

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<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.iv_item_icon);
            fruitName = (TextView)itemView.findViewById(R.id.tv_item_name);
        }
    }

    public FruitAdapter(List<Fruit> 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<Fruit> 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;
    }

}