This repository has been archived by the owner on Sep 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds GridExampleActivity for grid tutorial and NYTimesData integration
- Loading branch information
1 parent
49a7a34
commit 93c8b9d
Showing
8 changed files
with
201 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...le/app/src/main/java/co/moonmonkeylabs/realmrecyclerview/example/GridExampleActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package co.moonmonkeylabs.realmrecyclerview.example; | ||
|
||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ImageView; | ||
import android.widget.LinearLayout; | ||
import android.widget.TextView; | ||
|
||
import com.bumptech.glide.Glide; | ||
|
||
import co.moonmonkeylabs.realmnytimesdata.NYTimesDataLoader; | ||
import co.moonmonkeylabs.realmnytimesdata.NYTimesModule; | ||
import co.moonmonkeylabs.realmnytimesdata.model.NYTimesMultimedium; | ||
import co.moonmonkeylabs.realmnytimesdata.model.NYTimesStory; | ||
import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView; | ||
import io.realm.Realm; | ||
import io.realm.RealmBasedRecyclerViewAdapter; | ||
import io.realm.RealmConfiguration; | ||
import io.realm.RealmList; | ||
import io.realm.RealmResults; | ||
import io.realm.RealmViewHolder; | ||
|
||
public class GridExampleActivity extends AppCompatActivity { | ||
|
||
private RealmRecyclerView realmRecyclerView; | ||
private NYTimesStoryRecyclerViewAdapter nyTimesStoryAdapter; | ||
private Realm realm; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
setContentView(R.layout.activity_main_grid_layout); | ||
realmRecyclerView = (RealmRecyclerView) findViewById(R.id.realm_recycler_view); | ||
|
||
setTitle(getResources().getString( | ||
R.string.activity_layout_name, | ||
getIntent().getStringExtra("Type"))); | ||
|
||
resetRealm(); | ||
|
||
Realm.setDefaultConfiguration(getRealmConfig()); | ||
realm = Realm.getDefaultInstance(); | ||
RealmResults<NYTimesStory> nyTimesStories = | ||
realm.where(NYTimesStory.class).findAllSorted("sortTimeStamp", false); | ||
nyTimesStoryAdapter = new NYTimesStoryRecyclerViewAdapter(this, nyTimesStories, true, true); | ||
realmRecyclerView.setAdapter(nyTimesStoryAdapter); | ||
|
||
final NYTimesDataLoader nyTimesDataLoader = new NYTimesDataLoader(); | ||
nyTimesDataLoader.loadData("home", realm, "b9989f55a0c330b0bdfea069af08b163:15:73381676"); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
realm.close(); | ||
realm = null; | ||
} | ||
|
||
public class NYTimesStoryRecyclerViewAdapter extends RealmBasedRecyclerViewAdapter<NYTimesStory, | ||
NYTimesStoryRecyclerViewAdapter.ViewHolder> { | ||
|
||
public NYTimesStoryRecyclerViewAdapter( | ||
Context context, | ||
RealmResults<NYTimesStory> realmResults, | ||
boolean automaticUpdate, | ||
boolean animateIdType) { | ||
super(context, realmResults, automaticUpdate, animateIdType); | ||
} | ||
|
||
public class ViewHolder extends RealmViewHolder { | ||
|
||
public TextView title; | ||
public TextView publishedDate; | ||
public ImageView image; | ||
public TextView storyAbstract; | ||
|
||
public ViewHolder(LinearLayout container) { | ||
super(container); | ||
this.title = (TextView) container.findViewById(R.id.title); | ||
this.publishedDate = (TextView) container.findViewById(R.id.date); | ||
this.image = (ImageView) container.findViewById(R.id.image); | ||
this.storyAbstract = (TextView) container.findViewById(R.id.story_abstract); | ||
} | ||
} | ||
|
||
@Override | ||
public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) { | ||
View v = inflater.inflate(R.layout.grid_item_view, viewGroup, false); | ||
ViewHolder vh = new ViewHolder((LinearLayout) v); | ||
return vh; | ||
} | ||
|
||
@Override | ||
public void onBindRealmViewHolder(ViewHolder viewHolder, int position) { | ||
final NYTimesStory nyTimesStory = realmResults.get(position); | ||
viewHolder.title.setText(nyTimesStory.getTitle()); | ||
viewHolder.publishedDate.setText(nyTimesStory.getPublishedDate()); | ||
final RealmList<NYTimesMultimedium> multimedia = nyTimesStory.getMultimedia(); | ||
if (multimedia != null && !multimedia.isEmpty()) { | ||
Glide.with(GridExampleActivity.this).load( | ||
multimedia.get(0).getUrl()).into(viewHolder.image); | ||
} | ||
viewHolder.storyAbstract.setText(nyTimesStory.getStoryAbstract()); | ||
} | ||
} | ||
|
||
private RealmConfiguration getRealmConfig() { | ||
return new RealmConfiguration | ||
.Builder(this) | ||
.setModules(Realm.getDefaultModule(), new NYTimesModule()) | ||
.build(); | ||
} | ||
|
||
private void resetRealm() { | ||
Realm.deleteRealm(getRealmConfig()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:layout_width="wrap_content" | ||
android:layout_height="match_parent"> | ||
|
||
<ImageView | ||
android:id="@+id/image" | ||
android:layout_width="100dp" | ||
android:layout_height="100dp" | ||
android:layout_marginTop="5dp" | ||
android:layout_gravity="center_horizontal"/> | ||
|
||
<TextView | ||
android:id="@+id/title" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="5dp" | ||
android:layout_marginRight="5dp" | ||
android:singleLine="true" | ||
android:ellipsize="end" | ||
android:textStyle="bold"/> | ||
|
||
<TextView | ||
android:id="@+id/date" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="5dp" | ||
android:layout_marginRight="5dp" | ||
android:layout_gravity="center_horizontal" | ||
android:singleLine="true" | ||
android:ellipsize="end"/> | ||
|
||
<TextView | ||
android:id="@+id/story_abstract" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="5dp" | ||
android:layout_marginRight="5dp" | ||
android:layout_marginBottom="5dp" | ||
android:gravity="center_horizontal" | ||
android:maxLines="5" | ||
android:ellipsize="end"/> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
include ':app' | ||
|
||
include 'library' | ||
project(':library').projectDir = new File(settingsDir, '../library') | ||
project(':library').projectDir = new File(settingsDir, '../library') | ||
|
||
include 'data' | ||
project(':data').projectDir = new File(settingsDir, '../../realm-nytimes-data/library') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters