dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support:recyclerview-v7:27.1.1' implementation 'com.android.support:cardview-v7:27.1.1' } |
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rvFood" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/control">android.support.v7.widget.RecyclerView> RelativeLayout> |
import android.content.Context; import android.support.annotation.NonNull; 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 java.util.ArrayList; /** * Kế thừa lớp Adapter với Generic là MyViewHolder */ public class MyAdapter extends RecyclerView.Adapter ArrayList Context mContext; /** * Hàm khởi tạo * @param mList * @param mContext */ public MyAdapter(ArrayList this.mList = mList; this.mContext = mContext; } /** * Hàm tạo các đối tượng ViewHolder * @param parent * @param viewType * @return */ @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(mContext).inflate(R.layout.item_food, parent, false); MyViewHolder mvh = new MyViewHolder(v); return mvh; } /** * Đẩy dữ liệu vào ViewHolder * @param holder * @param position */ @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.title.setText(mList.get(position).title); holder.image.setImageResource(mList.get(position).imageRes); } /** * Đếm số lượng các phần tử * @return */ @Override public int getItemCount() { return mList.size(); } /** * Lớp static ViewHolder có tác dụng giữ các phần tử View */ public static class MyViewHolder extends RecyclerView.ViewHolder { TextView title; ImageView image; public MyViewHolder(View v) { super(v); this.title = v.findViewById(R.id.itemLblTitle); this.image = v.findViewById(R.id.itemImgDesc); } } } |
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { RecyclerView mRv; ArrayList private void fakeData() { mLstFood.add(new Food("Bánh mỳ thịt", R.drawable.img_banhmythit)); mLstFood.add(new Food("Hoa quả", R.drawable.img_hoaqua)); mLstFood.add(new Food("Ngô Nướng", R.drawable.img_ngonuong)); mLstFood.add(new Food("Cá hồi Chile", R.drawable.img_cahoi_chile)); mLstFood.add(new Food("Sandwitch", R.drawable.img_sandwitch)); mLstFood.add(new Food("Hoa quả", R.drawable.img_hoaqua)); mLstFood.add(new Food("Ngô Nướng", R.drawable.img_ngonuong)); mLstFood.add(new Food("Bánh mỳ thịt", R.drawable.img_banhmythit)); mLstFood.add(new Food("Cá hồi Chile", R.drawable.img_cahoi_chile)); mLstFood.add(new Food("Hoa quả", R.drawable.img_hoaqua)); mLstFood.add(new Food("Ngô Nướng", R.drawable.img_ngonuong)); mLstFood.add(new Food("Sandwitch", R.drawable.img_sandwitch)); mLstFood.add(new Food("Hoa quả", R.drawable.img_hoaqua)); mLstFood.add(new Food("Ngô Nướng", R.drawable.img_ngonuong)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Dữ liệu ảo fakeData(); // Gọi RecycleView và đổ dữ liệu mRv = (RecyclerView) findViewById(R.id.rvFood); // tăng hiệu năng nếu thay đổi nội dung KHÔNG thay đổi kích thước layout // mRv.setHasFixedSize(true); // Cài đặt hiển thị kiểu Linear mRv.setLayoutManager(new LinearLayoutManager(this)); MyAdapter adapter = new MyAdapter(mLstFood, this); mRv.setAdapter(adapter); } public void checkLinear(View view) { // Cài đặt hiển thị kiểu Linear mRv.setLayoutManager(new LinearLayoutManager(this)); } public void checkGrid(View view) { // Grid 2 cột mRv.setLayoutManager(new GridLayoutManager(this, 2)); } public void checkStagered(View view) { // Stagered 2 cột và điều chỉnh chiều dọc StaggeredGridLayoutManager sglm = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mRv.setLayoutManager(sglm); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id){ case R.id.menu_cardview: Intent intent = new Intent(this, DemoCardViewActivity.class); startActivity(intent); break; } return super.onOptionsItemSelected(item); } |
Các quản lý bố cục của RecycleView có thể hiển thị như sau:
LinearLayoutManager
GridLayoutManager
StaggeredGridLayoutManager
MinhVT