Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement list activity with RecyclerView #2

Merged
merged 1 commit into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.google.code.gson:gson:2.6.2'
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ListActivity"
android:name=".ArticleListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.novinger.jason.demoreader;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.novinger.jason.demoreader.datamodels.Article;

import java.util.ArrayList;

/**
* Created by jason on 5/1/16.
*/
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
private ArrayList<Article> mArticles;

public static class ViewHolder extends RecyclerView.ViewHolder {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey! half my comments from the last PR are already addressed 8 hours ago!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you get the viewholder pattern?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not completely, but I expect there will be some amount of "it just works" while learning a framework. I'll report back on ViewHolder at some point.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, let me know if you want the long story... it came about because of bad habits and slow xml parsing, but is now baked in the cake, so i think turned out maybe a little differently than it might have without those bumps

public TextView mHeadline;
public TextView mByline;

public ViewHolder(View articleView) {
super(articleView);

mHeadline = (TextView) articleView.findViewById(R.id.headline);
mByline = (TextView) articleView.findViewById(R.id.byline);
}
}

public ArticleAdapter(ArrayList<Article> articles) {
mArticles = articles;
}

@Override
public ArticleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int ViewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this make sense?

Copy link
Owner Author

@jnovinger jnovinger May 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we're getting the context (which I believe is the ArticleListActivity, correct?) and then using it to inflate the XML layout into an instantiated View object.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct... although i actually was talking about the next line (sorry!)

View articleView = inflater.inflate(R.layout.article_list_item_view, parent, false);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a several signatures for this method, and not all of them are obvious... might be worth a read

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, thanks.

return new ViewHolder(articleView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Article article = mArticles.get(position);
holder.mHeadline.setText(article.getHeadline());
holder.mByline.setText(article.getPrimaryAuthor().toString());
}

@Override
public int getItemCount() {
return mArticles.size();
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like this class a lot - it's very clear what it's doing, and is only doing exactly what it should be... "only and exactly" +1

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do too! Thanks.

Original file line number Diff line number Diff line change
@@ -1,72 +1,54 @@
package org.novinger.jason.demoreader;

import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.gson.Gson;

import org.novinger.jason.demoreader.datamodels.Article;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ListActivity extends AppCompatActivity {
public class ArticleListActivity extends AppCompatActivity {

private final static int qty = 50;
private final static int qty = 250;
private final static String feedUrl = "http://www.dailydot.com/api/v1/content/article/";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Article[] articles;
try {
articles = getFeedItems(getFeedUrl());
} catch (IOException ex) {
articles = new Article[0];
Log.d("Exception", ex.toString());
}
enableNetworkingPolicy();

final ListView listview = (ListView) findViewById(R.id.listview);
final ArrayList<String> list = new ArrayList<>();
for (Article article : articles) {
list.add(article.toString());
}
final ArrayList<Article> list = getArticles();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this final?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, good question. I probably got excited there about providing optimization hints to the compiler. Since it's a local variable that doesn't get reassigned, it should not cause any issues, I believe. The list object is still mutable, the label just isn't reassignable.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so there is quite a long conversation about using final everywhere you can versus everywhere you must. due to laziness, i tend to fall into the latter camp. you can do whatever, but i'd suggest consistency - when i see most of the time final is not used, then i see it used, that's a flag to me (insert scooby doo noise here)


RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

RecyclerView.Adapter mAdapter = new ArticleAdapter(list);
mRecyclerView.setAdapter(mAdapter);
}

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
final ViewPropertyAnimator viewPropertyAnimator = view.animate().setDuration(100).alpha(0);
viewPropertyAnimator.withEndAction(new Runnable() {
@Override
public void run() {
list.remove(item);
list.trimToSize();
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
private void enableNetworkingPolicy() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

@Override
Expand All @@ -91,6 +73,21 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@NonNull
private ArrayList<Article> getArticles() {
Article[] articles;
try {
articles = getFeedItems(getFeedUrl());
} catch (IOException ex) {
articles = new Article[0];
Log.d("Exception", ex.toString());
}

final ArrayList<Article> list = new ArrayList<>();
Collections.addAll(list, articles);
return list;
}

private Article[] getFeedItems(String url) throws IOException {
OkHttpClient client = new OkHttpClient();

Expand Down
21 changes: 13 additions & 8 deletions app/src/main/res/layout/activity_list.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
<RelativeLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ListActivity">
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ListActivity">

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>
27 changes: 27 additions & 0 deletions app/src/main/res/layout/article_list_item_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="headline"
android:id="@+id/headline" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="byline"
android:id="@+id/byline"
android:layout_weight="1" />

</LinearLayout>