12 January 2017

Android: RecyclerView hiển thị GridLayoutManager, LinearLayoutManager, StaggeredGridLayoutManager sử dụng Adapter và Interface (Android Studio)

Tải thư viện recyclerview-v7:25.1.0 vào Project
Thành công nếu thấy đoạn code này: compile 'com.android.support:recyclerview-v7:25.1.0'
//===Show kiểu LinearLayoutManager===//
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
 //===Show kiểu LinearLayoutManager ===//
HORIZONTAL cuộn ngang
VERTICAL cuộn dọc 
TRUE show từ đầu list
FALSE show từ cuối list
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
//===Show kiểu GridLayoutManager===//
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
//===Show kiểu StaggeredGridLayoutManager===//
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lonely.recyclerviewdemo.com.main.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvProduct"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
item_product.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_product"
        android:src="@drawable/p3"
        android:layout_width="120dp"
        android:layout_height="120dp" />

    <TextView
        android:id="@+id/tv_name"
        android:text="CHOCOLATE CHERRY"
        android:textColor="#2b0102"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_description"
        android:text="BLENDED - LIMITED BATCH"
        android:textSize="12sp"
        android:textColor="#2d1fc9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_price"
        android:text="150.000 VNĐ"
        android:textColor="#ff0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>
Product.java
Java Android 2017
package com.example.lonely.recyclerviewdemo.com.model;

/**
 * Created by Lonely on 1/12/2017.
 */

public class Product {
    private String name;
    private String description;
    private String price;
    private int image;

    public Product(String name, String description, String price, int image) {
        this.name = name;
        this.description = description;
        this.price = price;
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }
}
ProductAdapter.java
Java Android 2017
package com.example.lonely.recyclerviewdemo.com.adapter;

import android.app.Activity;
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 com.example.lonely.recyclerviewdemo.R;
import com.example.lonely.recyclerviewdemo.com.model.Product;

import java.util.List;

/**
 * Created by Lonely on 1/12/2017.
 */

public class ProductAdapter extends RecyclerView.Adapter {
    private Activity activity;
    private List<Product> productList;
    private IOnClickItem iOnClickItem;

    public ProductAdapter(Activity activity, List<Product> productList, IOnClickItem iOnClickItem) {
        this.activity = activity;
        this.productList = productList;
        this.iOnClickItem = iOnClickItem;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = activity.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.item_product,parent, false);
        ProductHolder productHolder = new ProductHolder(view);
        return productHolder;
    }
    public static class ProductHolder extends RecyclerView.ViewHolder{

        private ImageView ivProduct;
        private TextView tvName;
        private TextView tvDescrition;
        private TextView tvPrice;

        public ProductHolder(View itemView) {
            super(itemView);
            ivProduct = (ImageView) itemView.findViewById(R.id.iv_product);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvDescrition = (TextView) itemView.findViewById(R.id.tv_description);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ProductHolder productHolder = (ProductHolder) holder;
        Product product = productList.get(position);
        productHolder.ivProduct.setImageResource(product.getImage());
        productHolder.tvName.setText(product.getName());
        productHolder.tvDescrition.setText(product.getDescription());
        productHolder.tvPrice.setText(product.getPrice());

        productHolder.itemView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                iOnClickItem.onClickItem(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }
}
IOnClickItem.java
Java Android 2017
package com.example.lonely.recyclerviewdemo.com.adapter;

/**
 * Created by Lonely on 1/12/2017.
 */

public interface IOnClickItem {
    void onClickItem(int position);
}
MainActivity.java
Java Android 2017
package com.example.lonely.recyclerviewdemo.com.main;

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

import com.example.lonely.recyclerviewdemo.R;
import com.example.lonely.recyclerviewdemo.com.adapter.IOnClickItem;
import com.example.lonely.recyclerviewdemo.com.adapter.ProductAdapter;
import com.example.lonely.recyclerviewdemo.com.model.Product;

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

public class MainActivity extends AppCompatActivity implements IOnClickItem{

    List<Product> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //B1: Data source
        initData();
        //B2: Adapter
        ProductAdapter adapter = new ProductAdapter(this,list, this);
        //B3: LayoutManager
        //===Show kiểu LinearLayoutManager===//
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //===Show kiểu LinearLayoutManager (HORIZONTAL cuộn ngang - VERTICAL cuộn dọc) (true show từ đầu list- false show từ cuối list)===//
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        //===Show kiểu GridLayoutManager===//
//        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        //===Show kiểu StaggeredGridLayoutManager===//
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //B4: RecycleView
        RecyclerView view = (RecyclerView) findViewById(R.id.rvProduct);
        view.setAdapter(adapter);
        view.setLayoutManager(layoutManager);
    }
    public void initData(){
        list.add(new Product("CHOCOLATE CHERRY","BLENDED - LIMITED BATCH","150.000 VNĐ" , R.drawable.p1));
        list.add(new Product("DULCE DE LECHE","BLENDED - LIMITED BATCH","150.000 VNĐ" , R.drawable.p2));
        list.add(new Product("APPLE CINNAMON","BLENDED","150.000 VNĐ" , R.drawable.p3));
        list.add(new Product("BANANA","FRUIT ON THE BOTTOM","150.000 VNĐ" , R.drawable.p4));
        list.add(new Product("BLACKBERRY","FRUIT ON THE BOTTOM","150.000 VNĐ" , R.drawable.p5));
        list.add(new Product("CHOCOLATE CHERRY","BLENDED - LIMITED BATCH","150.000 VNĐ" , R.drawable.p1));
        list.add(new Product("DULCE DE LECHE","BLENDED - LIMITED BATCH","150.000 VNĐ" , R.drawable.p2));
        list.add(new Product("APPLE CINNAMON","BLENDED","150.000 VNĐ" , R.drawable.p3));
        list.add(new Product("BANANA","FRUIT ON THE BOTTOM","150.000 VNĐ" , R.drawable.p4));
        list.add(new Product("BLACKBERRY","FRUIT ON THE BOTTOM","150.000 VNĐ" , R.drawable.p5));
    }

    @Override
    public void onClickItem(int position) {
        Product product = list.get(position);
        Toast.makeText(this, product.getName(), Toast.LENGTH_SHORT).show();
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang