mirror of
https://github.com/stormzhang/9GAG.git
synced 2026-10-17 05:11:08 +08:00
Add ButterKnife and some Utils
This commit is contained in:
@@ -13,6 +13,13 @@
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<provider
|
||||
android:name=".dao.DataProvider"
|
||||
android:authorities="com.storm.9gag.provider"
|
||||
android:exported="false">
|
||||
<grant-uri-permission android:pathPattern=".*"/>
|
||||
</provider>
|
||||
|
||||
<activity
|
||||
android:name=".ui.MainActivity"
|
||||
android:label="@string/app_name" >
|
||||
|
||||
@@ -22,13 +22,14 @@ import android.support.v4.content.CursorLoader;
|
||||
public class FeedsDataHelper extends BaseDataHelper {
|
||||
private Category mCategory;
|
||||
|
||||
public FeedsDataHelper(Context context) {
|
||||
public FeedsDataHelper(Context context, Category category) {
|
||||
super(context);
|
||||
mCategory = category;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Uri getContentUri() {
|
||||
return null;
|
||||
return DataProvider.FEEDS_CONTENT_URI;
|
||||
}
|
||||
|
||||
private ContentValues getContentValues(Feed feed) {
|
||||
|
||||
@@ -3,12 +3,19 @@ package me.storm.ninegag.model;
|
||||
import android.database.Cursor;
|
||||
import android.media.Image;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import me.storm.ninegag.dao.FeedsDataHelper;
|
||||
|
||||
/**
|
||||
* Created by storm on 14-3-25.
|
||||
*/
|
||||
public class Feed extends BaseModel {
|
||||
private static final HashMap<String, Feed> CACHE = new HashMap<String, Feed>();
|
||||
|
||||
public String id;
|
||||
public String caption;
|
||||
public String link;
|
||||
@@ -25,8 +32,29 @@ public class Feed extends BaseModel {
|
||||
public int count;
|
||||
}
|
||||
|
||||
private static void addToCache(Feed feed) {
|
||||
CACHE.put(feed.id, feed);
|
||||
}
|
||||
|
||||
private static Feed getFromCache(String id) {
|
||||
return CACHE.get(id);
|
||||
}
|
||||
|
||||
public static Feed fromJson(String json) {
|
||||
return new Gson().fromJson(json, Feed.class);
|
||||
}
|
||||
|
||||
public static Feed fromCursor(Cursor cursor) {
|
||||
return null;
|
||||
String id = cursor.getString(cursor.getColumnIndex(FeedsDataHelper.FeedsDBInfo.ID));
|
||||
Feed feed = getFromCache(id);
|
||||
if (feed != null) {
|
||||
return feed;
|
||||
}
|
||||
feed = new Gson().fromJson(
|
||||
cursor.getString(cursor.getColumnIndex(FeedsDataHelper.FeedsDBInfo.JSON)),
|
||||
Feed.class);
|
||||
addToCache(feed);
|
||||
return feed;
|
||||
}
|
||||
|
||||
public static class FeedRequestData {
|
||||
|
||||
@@ -6,6 +6,8 @@ import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v4.widget.DrawerLayout;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
import me.storm.ninegag.R;
|
||||
import me.storm.ninegag.model.Category;
|
||||
import me.storm.ninegag.ui.fragment.BaseFragment;
|
||||
@@ -16,7 +18,8 @@ import me.storm.ninegag.ui.fragment.FeedsFragment;
|
||||
* Created by storm on 14-3-24.
|
||||
*/
|
||||
public class MainActivity extends BaseActivity {
|
||||
private DrawerLayout mDrawerLayout;
|
||||
@InjectView(R.id.drawer_layout)
|
||||
DrawerLayout mDrawerLayout;
|
||||
|
||||
private ActionBarDrawerToggle mDrawerToggle;
|
||||
|
||||
@@ -25,16 +28,12 @@ public class MainActivity extends BaseActivity {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.inject(this);
|
||||
|
||||
findViews();
|
||||
// setCategory(Category.hot);
|
||||
setCategory(Category.hot);
|
||||
replaceFragment(R.id.left_drawer, new DrawerFragment());
|
||||
}
|
||||
|
||||
private void findViews() {
|
||||
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
|
||||
}
|
||||
|
||||
protected void replaceFragment(int viewId, BaseFragment fragment) {
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
fragmentManager.beginTransaction().replace(viewId, fragment).commit();
|
||||
|
||||
@@ -6,22 +6,23 @@ import android.database.Cursor;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v4.widget.CursorAdapter;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CursorAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.volley.toolbox.ImageLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
import me.storm.ninegag.R;
|
||||
import me.storm.ninegag.data.RequestManager;
|
||||
import me.storm.ninegag.model.Feed;
|
||||
|
||||
|
||||
/**
|
||||
* Created by storm on 14-3-26.
|
||||
*/
|
||||
@@ -32,23 +33,16 @@ public class FeedsAdapter extends CursorAdapter {
|
||||
|
||||
private Drawable mDefaultImageDrawable = new ColorDrawable(Color.argb(255, 201, 201, 201));
|
||||
|
||||
private ArrayList<Feed> mFeeds;
|
||||
|
||||
public FeedsAdapter(Context context, ListView listView) {
|
||||
super(context, null, false);
|
||||
mLayoutInflater = ((Activity) context).getLayoutInflater();
|
||||
mListView = listView;
|
||||
}
|
||||
|
||||
public FeedsAdapter(Context context, ArrayList<Feed> feeds) {
|
||||
super(context, null, false);
|
||||
mLayoutInflater = ((Activity) context).getLayoutInflater();
|
||||
this.mFeeds = feeds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Feed getItem(int position) {
|
||||
return mFeeds.get(position);
|
||||
mCursor.moveToPosition(position);
|
||||
return Feed.fromCursor(mCursor);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,8 +60,7 @@ public class FeedsAdapter extends CursorAdapter {
|
||||
view.setEnabled(!mListView.isItemChecked(cursor.getPosition()
|
||||
+ mListView.getHeaderViewsCount()));
|
||||
|
||||
// Section section = Section.fromCursor(cursor);
|
||||
Feed feed = getItem(cursor.getPosition());
|
||||
Feed feed = Feed.fromCursor(cursor);
|
||||
holder.imageRequest = RequestManager.loadImage(feed.images.normal, RequestManager
|
||||
.getImageListener(holder.image, mDefaultImageDrawable, mDefaultImageDrawable));
|
||||
holder.caption.setText(feed.caption);
|
||||
@@ -82,16 +75,17 @@ public class FeedsAdapter extends CursorAdapter {
|
||||
return holder;
|
||||
}
|
||||
|
||||
private class Holder {
|
||||
public ImageView image;
|
||||
static class Holder {
|
||||
@InjectView(R.id.iv_normal)
|
||||
ImageView image;
|
||||
|
||||
public TextView caption;
|
||||
@InjectView(R.id.tv_caption)
|
||||
TextView caption;
|
||||
|
||||
public ImageLoader.ImageContainer imageRequest;
|
||||
|
||||
public Holder(View view) {
|
||||
image = (ImageView) view.findViewById(R.id.iv_normal);
|
||||
caption = (TextView) view.findViewById(R.id.tv_caption);
|
||||
ButterKnife.inject(this, view);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
package me.storm.ninegag.ui.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.volley.toolbox.ImageLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import me.storm.ninegag.R;
|
||||
import me.storm.ninegag.data.RequestManager;
|
||||
import me.storm.ninegag.model.Feed;
|
||||
|
||||
/**
|
||||
* Created by storm on 14-3-28.
|
||||
*/
|
||||
public class TestSectionAdapter extends BaseAdapter {
|
||||
private Context mContext;
|
||||
|
||||
private ArrayList<Feed> mSections;
|
||||
|
||||
private Drawable mDefaultImageDrawable = new ColorDrawable(Color.argb(255, 201, 201, 201));
|
||||
|
||||
public TestSectionAdapter(Context context, ArrayList<Feed> sections) {
|
||||
this.mContext = context;
|
||||
this.mSections = sections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
if (mSections != null)
|
||||
return mSections.size();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Feed getItem(int position) {
|
||||
return mSections.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
convertView = View.inflate(mContext, R.layout.listitem_section, null);
|
||||
Holder holder = getHolder(convertView);
|
||||
Feed section = getItem(position);
|
||||
holder.imageRequest = RequestManager.loadImage(section.images.normal, RequestManager
|
||||
.getImageListener(holder.image, mDefaultImageDrawable, mDefaultImageDrawable));
|
||||
holder.caption.setText(section.caption);
|
||||
return convertView;
|
||||
}
|
||||
|
||||
private Holder getHolder(final View view) {
|
||||
Holder holder = (Holder) view.getTag();
|
||||
if (holder == null) {
|
||||
holder = new Holder(view);
|
||||
view.setTag(holder);
|
||||
}
|
||||
return holder;
|
||||
}
|
||||
|
||||
private class Holder {
|
||||
public ImageView image;
|
||||
|
||||
public TextView caption;
|
||||
|
||||
public ImageLoader.ImageContainer imageRequest;
|
||||
|
||||
public Holder(View view) {
|
||||
image = (ImageView) view.findViewById(R.id.iv_normal);
|
||||
caption = (TextView) view.findViewById(R.id.tv_caption);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,47 @@
|
||||
package me.storm.ninegag.ui.fragment;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
import me.storm.ninegag.App;
|
||||
import me.storm.ninegag.R;
|
||||
import me.storm.ninegag.api.GagApi;
|
||||
import me.storm.ninegag.dao.FeedsDataHelper;
|
||||
import me.storm.ninegag.data.GsonRequest;
|
||||
import me.storm.ninegag.model.Category;
|
||||
import me.storm.ninegag.model.Feed;
|
||||
import me.storm.ninegag.ui.adapter.TestSectionAdapter;
|
||||
import me.storm.ninegag.ui.adapter.FeedsAdapter;
|
||||
import me.storm.ninegag.util.TaskUtils;
|
||||
|
||||
/**
|
||||
* Created by storm on 14-3-25.
|
||||
*/
|
||||
public class FeedsFragment extends BaseFragment {
|
||||
public class FeedsFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<Cursor> {
|
||||
public static final String EXTRA_CATEGORY = "EXTRA_CATEGORY";
|
||||
|
||||
private Category mCategory;
|
||||
|
||||
private TestSectionAdapter mAdapter;
|
||||
private FeedsDataHelper mDataHelper;
|
||||
|
||||
private ListView mListView;
|
||||
private FeedsAdapter mAdapter;
|
||||
|
||||
@InjectView(R.id.listView)
|
||||
ListView mListView;
|
||||
|
||||
private int mPage = 0;
|
||||
|
||||
public static FeedsFragment newInstance(Category category) {
|
||||
FeedsFragment fragment = new FeedsFragment();
|
||||
@@ -40,10 +53,14 @@ public class FeedsFragment extends BaseFragment {
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View contentView = inflater.inflate(R.layout.fragment_section, null);
|
||||
mListView = (ListView) contentView.findViewById(R.id.listView);
|
||||
View contentView = inflater.inflate(R.layout.fragment_section, container, false);
|
||||
ButterKnife.inject(this, contentView);
|
||||
|
||||
parseArgument();
|
||||
loadData(0);
|
||||
mDataHelper = new FeedsDataHelper(App.getContext(), mCategory);
|
||||
mAdapter = new FeedsAdapter(getActivity(), mListView);
|
||||
mListView.setAdapter(mAdapter);
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
return contentView;
|
||||
}
|
||||
|
||||
@@ -59,11 +76,37 @@ public class FeedsFragment extends BaseFragment {
|
||||
private Response.Listener<Feed.FeedRequestData> responseListener() {
|
||||
return new Response.Listener<Feed.FeedRequestData>() {
|
||||
@Override
|
||||
public void onResponse(Feed.FeedRequestData response) {
|
||||
Toast.makeText(App.getContext(), new Gson().toJson(response), Toast.LENGTH_LONG).show();
|
||||
mAdapter = new TestSectionAdapter(getActivity(), response.data);
|
||||
mListView.setAdapter(mAdapter);
|
||||
public void onResponse(final Feed.FeedRequestData response) {
|
||||
TaskUtils.executeAsyncTask(new AsyncTask<Object, Object, Object>() {
|
||||
@Override
|
||||
protected Object doInBackground(Object... params) {
|
||||
if (mPage == 1) {
|
||||
mDataHelper.deleteAll();
|
||||
}
|
||||
ArrayList<Feed> feeds = response.data;
|
||||
mDataHelper.bulkInsert(feeds);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||
return mDataHelper.getCursorLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
||||
mAdapter.changeCursor(data);
|
||||
if (data != null && data.getCount() == 0) {
|
||||
loadData(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> loader) {
|
||||
mAdapter.changeCursor(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package me.storm.ninegag.util;
|
||||
|
||||
import android.os.Build;
|
||||
import android.widget.ListView;
|
||||
|
||||
/**
|
||||
* Created by storm on 14-4-11.
|
||||
*/
|
||||
public class ListViewUtils {
|
||||
private ListViewUtils() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 滚动列表到顶端
|
||||
*
|
||||
* @param listView
|
||||
*/
|
||||
public static void smoothScrollListViewToTop(final ListView listView) {
|
||||
if (listView == null) {
|
||||
return;
|
||||
}
|
||||
smoothScrollListView(listView, 0);
|
||||
listView.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listView.setSelection(0);
|
||||
}
|
||||
}, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* 滚动列表到position
|
||||
*
|
||||
* @param listView
|
||||
* @param position
|
||||
*/
|
||||
public static void smoothScrollListView(ListView listView, int position) {
|
||||
if (Build.VERSION.SDK_INT > 7) {
|
||||
listView.smoothScrollToPositionFromTop(0, 0);
|
||||
} else {
|
||||
listView.setSelection(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package me.storm.ninegag.util;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
|
||||
/**
|
||||
* Created by storm on 14-4-11.
|
||||
*/
|
||||
public class TaskUtils {
|
||||
public static <Params, Progress, Result> void executeAsyncTask(
|
||||
AsyncTask<Params, Progress, Result> task, Params... params) {
|
||||
if (Build.VERSION.SDK_INT >= 11) {
|
||||
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
|
||||
} else {
|
||||
task.execute(params);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user