-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, we're getting the context (which I believe is the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so there is quite a long conversation about using |
||
|
||
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 | ||
|
@@ -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(); | ||
|
||
|
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> |
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> |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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