build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
apply plugin: 'com.android.application' android { compileSdkVersion 25 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:25.3.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' 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.github.markomilos:paginate:0.5.1' } |
บรรทัดที่ 23 : ไลบรารี paginate ใช้ com.android.support:recyclerview-v7:23.0.1
เราเลยต้องใช้ com.android.support:appcompat-v7:25.3.1
เพราะลองใช้ com.android.support:appcompat-v7:26.1.0
แล้วตอนรันมัน Error
บรรทัดที่ 29 : เรียกใช้ไลบรารี paginate (ไลบรารีนี้ไม่ได้ update มา 3 ปีละ ถ้าจะใช้ต้องเอามาปรับปรุงก่อน)
layout/custom_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginBottom="8dp" android:background="#fff" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:textSize="24sp" /> </LinearLayout> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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" android:background="#dfdfdf" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"></android.support.v7.widget.RecyclerView> </LinearLayout> |
CustomAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.phaisarn.myapplication; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; public class CustomAdapter extends RecyclerView.Adapter<CustomHolder> { private ArrayList<CustomItem> mItems; private Context mContext; public CustomAdapter(Context context, ArrayList<CustomItem> items) { mContext = context; mItems = items; } @Override public int getItemCount() { return mItems.size(); } @Override public CustomHolder onCreateViewHolder(ViewGroup vg, int type) { LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.custom_layout, vg, false); CustomHolder holder = new CustomHolder(view); return holder; } @Override public void onBindViewHolder(CustomHolder vHolder, int position) { CustomItem item = mItems.get(position); vHolder.textView.setText(item.text); } } |
CustomHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.phaisarn.myapplication; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; public class CustomHolder extends RecyclerView.ViewHolder { public TextView textView; public CustomHolder(View view) { super(view); textView = (TextView) view.findViewById(R.id.textView); } } |
CustomItem.java
1 2 3 4 5 6 7 8 9 10 |
package com.phaisarn.myapplication; public class CustomItem { public String text; public CustomItem(String text) { this.text = text; } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package com.phaisarn.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import android.os.Handler; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.paginate.Paginate; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView mRcv; private LinearLayoutManager mLayoutMan; private ArrayList<CustomItem> mItemArray; private CustomAdapter mAdapter; private boolean mIsLoading = false; private int mCreatedItems; private final int ITEMS_PER_PAGE = 4; private int mTotal_Items = 20; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mItemArray = new ArrayList<>(); mRcv = (RecyclerView) findViewById(R.id.recyclerView); mAdapter = new CustomAdapter(this, mItemArray); mRcv.setAdapter(mAdapter); mRcv.setHasFixedSize(true); mLayoutMan = new LinearLayoutManager(this, RecyclerView.VERTICAL, false); mRcv.setLayoutManager(mLayoutMan); if (mTotal_Items == 1) { mItemArray.add(new CustomItem("Item #" + 1)); } else if (mTotal_Items >= 2) { onScrollRecyclerView(); } } private void onScrollRecyclerView() { Paginate.Callbacks callbacks = new Paginate.Callbacks() { @Override public boolean isLoading() { //เมธอดสำหรับตรวจสอบขณะนั้นกำลังโหลดอยู่หรือไม่ return mIsLoading; } @Override //เมธอดสำหรับตรวจสอบว่ายังมีรายการที่ต้องโหลด (สร้าง) อีกหรือไม่ public boolean hasLoadedAllItems() { mCreatedItems = mLayoutMan.getItemCount(); if (mCreatedItems >= mTotal_Items) { return true; } else { return false; } } @Override //เมธอดสำหรับกำหนดวิธีการสร้างรายการใหม่ public void onLoadMore() { mIsLoading = true; new Handler().postDelayed(new Runnable() { @Override public void run() { int nextItem = mLayoutMan.getItemCount(); for (int i = 1; i <= ITEMS_PER_PAGE; i++) { mItemArray.add(new CustomItem("Item #" + nextItem)); if (nextItem == mTotal_Items) { break; } nextItem++; } mAdapter.notifyItemRangeInserted( nextItem, ITEMS_PER_PAGE); Toast.makeText(getBaseContext(), "Items Updated", Toast.LENGTH_SHORT).show(); mIsLoading = false; } }, 3000); } }; Paginate.with(mRcv, callbacks) .addLoadingListItem(true) //ค่าดีฟอลต์เป็น true อยู่แล้ว ดังนั้นอาจไม่กำหนดเมธอดนี้ก็ได้ .build(); } } |