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 31 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 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:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.0' 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' } |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:background="#dfdfdf" android:paddingBottom="0dp" android:paddingLeft="0dp" android:paddingRight="0dp" android:paddingTop="0dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"></android.support.v7.widget.RecyclerView> </LinearLayout> |
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; 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<RowItem> mItemArray; private CustomAdapter mAdapter; private boolean mIsLoading = false; private int mCreatedItems; private final int ITEMS_PER_PAGE = 5; private SQLiteHelper mSQLiteHelper; private SQLiteDatabase mDb; private int mTotalRows; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); fab.hide(); } @Override public void onStart() { super.onStart(); mSQLiteHelper = SQLiteHelper.getInstance(getBaseContext()); mDb = mSQLiteHelper.getReadableDatabase(); String sql = "SELECT * FROM list LIMIT " + ITEMS_PER_PAGE; Cursor cursor = mDb.rawQuery(sql, null); mItemArray = new ArrayList<>(); RowItem row; while (cursor.moveToNext()) { row = new RowItem(); row.item = cursor.getString(1); mItemArray.add(row); } mRcv = (RecyclerView) findViewById(R.id.recycler_view); mAdapter = new CustomAdapter(this, mItemArray); mRcv.setAdapter(mAdapter); mLayoutMan = new LinearLayoutManager(this, RecyclerView.VERTICAL, false); mRcv.setLayoutManager(mLayoutMan); sql = "SELECT COUNT(*) FROM list"; Cursor cursorCount = mDb.rawQuery(sql, null); cursorCount.moveToFirst(); mTotalRows = cursorCount.getInt(0); cursorCount.close(); 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 >= mTotalRows) { return true; } else { return false; } } @Override public void onLoadMore() { mIsLoading = true; new Handler().postDelayed(new Runnable() { @Override public void run() { mCreatedItems = mLayoutMan.getItemCount(); int startPosition = mCreatedItems - 1; String sql = "SELECT * FROM list LIMIT ?, ?"; String[] args = {startPosition + "", ITEMS_PER_PAGE + ""}; Cursor cursor = mDb.rawQuery(sql, args); RowItem row; while (cursor.moveToNext()) { row = new RowItem(); row.item = cursor.getString(1); mItemArray.add(row); } mAdapter.notifyItemRangeInserted(startPosition, ITEMS_PER_PAGE); mIsLoading = false; } }, 4000); } }; Paginate.with(mRcv, callbacks) .addLoadingListItem(true) .build(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
layout/item_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/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:textSize="24sp" /> </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 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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<RowItem> mItems; private Context mContext; public CustomAdapter(Context context, ArrayList<RowItem> items) { mContext = context; mItems = items; } @Override public int getItemCount() { return mItems.size(); } @Override public CustomHolder onCreateViewHolder(ViewGroup vg, int type) { final LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.item_layout, vg, false); final CustomHolder vHolder = new CustomHolder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int pos = vHolder.getAdapterPosition(); if(pos != RecyclerView.NO_POSITION) { //... } } }); return vHolder; } @Override public void onBindViewHolder(CustomHolder vHolder, int position) { RowItem row = mItems.get(position); vHolder.textView.setText(row.item); } } |
CustomHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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.text_view); } } |
RowItem.java
1 2 3 4 5 6 |
package com.phaisarn.myapplication; public class RowItem { public int _id; public String item; } |
SQLiteHelper.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 |
package com.phaisarn.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SQLiteHelper extends SQLiteOpenHelper { private static SQLiteHelper sqLiteDB; private static final String DB_NAME = "db_pagination"; private static final int DB_VERSION = 1; private SQLiteHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } public static synchronized SQLiteHelper getInstance(Context context) { if (sqLiteDB == null) { sqLiteDB = new SQLiteHelper(context.getApplicationContext()); } return sqLiteDB; } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE IF NOT EXISTS list(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "item TEXT)"; db.execSQL(sql); for (int i = 1; i <= 50; i++) { sql = "INSERT INTO list(item) VALUES(?)"; String[] args = {"Item " + i}; db.execSQL(sql, args); } } @Override public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { } } |