Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Adds functionality for adding sticky headers via integration with Sup…
Browse files Browse the repository at this point in the history
…erSLiM.

Adds LoadMore
  • Loading branch information
thorbenprimke committed Oct 19, 2015
1 parent 62467e0 commit 4ef3c40
Show file tree
Hide file tree
Showing 27 changed files with 1,147 additions and 69 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ buildscript {
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
2 changes: 2 additions & 0 deletions example/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.application'
// This does not break the build when Android Studio is missing the JRebel for Android plugin.
apply plugin: 'com.zeroturnaround.jrebel.android'

android {
compileSdkVersion 22
Expand Down
4 changes: 4 additions & 0 deletions example/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".MainActivity2"
android:label="@string/app_name" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public void onClick(View v) {
}
);

final Button linearWithLoadMoreButton =
(Button) findViewById(R.id.recycler_linear_with_load_more_button);
linearWithLoadMoreButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LayoutSelectorActivity.this, MainActivity.class);
intent.putExtra("Type", "LinearLoadMore");
startActivity(intent);
}
}
);


final Button gridButton = (Button) findViewById(R.id.recycler_grid_button);
gridButton.setOnClickListener(
new View.OnClickListener() {
Expand All @@ -39,5 +53,17 @@ public void onClick(View v) {
}
}
);

final Button sectionButton = (Button) findViewById(R.id.recycler_section_header_button);
sectionButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LayoutSelectorActivity.this, MainActivity2.class);
intent.putExtra("Type", "Header (SLM) ");
startActivity(intent);
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;
Expand All @@ -24,6 +22,7 @@
import io.realm.RealmBasedRecyclerViewAdapter;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
import io.realm.RealmViewHolder;

public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -54,27 +53,33 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

type = getIntent().getStringExtra("Type");
if (type.equals("Grid")) {
setContentView(R.layout.activity_main_grid_layout);
} else {
setContentView(R.layout.activity_main_linear_layout);
}
realmRecyclerView = (RealmRecyclerView) findViewById(R.id.realm_recycler_view);

setTitle(getResources().getString(R.string.activity_layout_name, type));

resetRealm();
realm = Realm.getInstance(this);

realmRecyclerView = (RealmRecyclerView) findViewById(R.id.realm_recycler_view);

if (type.equals("Grid")) {
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
realmRecyclerView.setLayoutManager(gridLayoutManager);
} else {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
realmRecyclerView.setLayoutManager(linearLayoutManager);
boolean isLoadMore = type.equals("LinearLoadMore");
if (isLoadMore) {
realm.beginTransaction();
for (int i = 0; i < 60; i++) {
QuoteModel quoteModel = realm.createObject(QuoteModel.class);
quoteModel.setId(i + 1);
quoteModel.setQuote(quotes.get((int) (quoteModel.getId() % quotes.size())));
}
realm.commitTransaction();
}

RealmResults<QuoteModel> quoteModels =
realm.where(QuoteModel.class).findAllSorted("id", false);
realm.where(QuoteModel.class).findAllSorted("id", isLoadMore ? true : false);
quoteAdapter = new QuoteRecyclerViewAdapter(getBaseContext(), quoteModels, true, true);
realmRecyclerView.setAdapter(quoteAdapter);

Expand All @@ -86,6 +91,24 @@ public void onRefresh() {
}
}
);

if (isLoadMore) {
realmRecyclerView.setOnLoadMoreListener(
new RealmRecyclerView.OnLoadMoreListener() {
@Override
public void onLoadMore(Object lastItem) {
if (lastItem instanceof QuoteModel) {
Toast.makeText(
MainActivity.this,
((QuoteModel) lastItem).getId() + " ",
Toast.LENGTH_SHORT).show();
}
asyncLoadMoreQuotes();
}
}
);
realmRecyclerView.enableShowLoadMore();
}
}

@Override
Expand Down Expand Up @@ -122,7 +145,7 @@ public QuoteRecyclerViewAdapter(
super(context, realmResults, automaticUpdate, animateIdType);
}

public class ViewHolder extends RecyclerView.ViewHolder {
public class ViewHolder extends RealmViewHolder {
public FrameLayout container;
public TextView quoteTextView;
public ViewHolder(FrameLayout container) {
Expand All @@ -133,15 +156,15 @@ public ViewHolder(FrameLayout container) {
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) {
View v = inflater.inflate(R.layout.item_view, viewGroup, false);
ViewHolder vh = new ViewHolder((FrameLayout) v);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final QuoteModel quoteModel = realmResults.get(i);
public void onBindRealmViewHolder(ViewHolder viewHolder, int position) {
final QuoteModel quoteModel = realmResults.get(position);
viewHolder.quoteTextView.setOnClickListener(
new View.OnClickListener() {
@Override
Expand All @@ -156,6 +179,11 @@ public void onClick(View v) {
}
viewHolder.quoteTextView.setText(quoteModel.getQuote());
}

@Override
public ViewHolder convertViewHolder(RealmViewHolder viewHolder) {
return ViewHolder.class.cast(viewHolder);
}
}

private void asyncAddQuote() {
Expand Down Expand Up @@ -223,6 +251,36 @@ protected void onPostExecute(Void aVoid) {
remoteItem.execute();
}

private void asyncLoadMoreQuotes() {
AsyncTask<Void, Void, Void> remoteItem = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Add some delay to the refresh/remove action.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
Realm instance = Realm.getInstance(MainActivity.this);
instance.beginTransaction();
for (int i = 0; i < 60; i++) {
QuoteModel quoteModel = instance.createObject(QuoteModel.class);
quoteModel.setId(i + 100); // That is to offset for primary key
quoteModel.setQuote(quotes.get((int) (quoteModel.getId() % quotes.size())));
}
instance.commitTransaction();
instance.close();
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
realmRecyclerView.disableShowLoadMore();
}
};
remoteItem.execute();
}

private void resetRealm() {
RealmConfiguration realmConfig = new RealmConfiguration
.Builder(this)
Expand Down
Loading

0 comments on commit 4ef3c40

Please sign in to comment.