Use EasyAdapter

This commit is contained in:
inorichi 2015-09-28 02:35:54 +02:00
parent a316298793
commit 43439720db
5 changed files with 52 additions and 103 deletions

View File

@ -1,7 +1,6 @@
package eu.kanade.mangafeed.presenter; package eu.kanade.mangafeed.presenter;
import android.content.Intent; import android.content.Intent;
import android.widget.GridView;
import java.util.ArrayList; import java.util.ArrayList;
@ -11,8 +10,8 @@ import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.App; import eu.kanade.mangafeed.App;
import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.LibraryAdapter;
import eu.kanade.mangafeed.view.LibraryView; import eu.kanade.mangafeed.view.LibraryView;
import uk.co.ribot.easyadapter.EasyAdapter;
import static rx.android.schedulers.AndroidSchedulers.mainThread; import static rx.android.schedulers.AndroidSchedulers.mainThread;
@ -28,7 +27,7 @@ public class LibraryPresenter {
App.getComponent(libraryView.getActivity()).inject(this); App.getComponent(libraryView.getActivity()).inject(this);
} }
public void onMangaClick(LibraryAdapter adapter, int position) { public void onMangaClick(EasyAdapter adapter, int position) {
Intent intent = new Intent(mLibraryView.getActivity(), MangaDetailActivity.class); Intent intent = new Intent(mLibraryView.getActivity(), MangaDetailActivity.class);
EventBus.getDefault().postSticky(adapter.getItem(position)); EventBus.getDefault().postSticky(adapter.getItem(position));
mLibraryView.getActivity().startActivity(intent); mLibraryView.getActivity().startActivity(intent);

View File

@ -1,90 +0,0 @@
package eu.kanade.mangafeed.ui.adapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Manga;
import uk.co.ribot.easyadapter.annotations.LayoutId;
/**
* Created by len on 25/09/2015.
*/
@LayoutId(R.layout.item_library)
public class LibraryAdapter extends ArrayAdapter<Manga> {
Context context;
int layoutResourceId;
ArrayList<Manga> data;
public LibraryAdapter(Context context, int layoutResourceId, ArrayList<Manga> data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MangoHolder holder = null;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MangoHolder(row);
row.setTag(holder);
}
else {
holder = (MangoHolder)row.getTag();
}
Manga m = data.get(position);
holder.nameText.setText(m.title);
Glide.with(getContext())
.load(getImageUrl())
.centerCrop()
.into(holder.thumbnail);
return row;
}
public void setData(ArrayList<Manga> mangas) {
// Avoid calling dataSetChanged twice
data.clear();
addAll(mangas);
}
private String getImageUrl() {
return "http://img1.wikia.nocookie.net/__cb20090524204255/starwars/images/thumb/1/1a/R2d2.jpg/400px-R2d2.jpg";
}
static class MangoHolder {
@Bind(R.id.thumbnailImageView)
ImageView thumbnail;
@Bind(R.id.nameTextView)
TextView nameText;
public MangoHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

View File

@ -0,0 +1,38 @@
package eu.kanade.mangafeed.ui.adapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Manga;
import uk.co.ribot.easyadapter.ItemViewHolder;
import uk.co.ribot.easyadapter.PositionInfo;
import uk.co.ribot.easyadapter.annotations.LayoutId;
import uk.co.ribot.easyadapter.annotations.ViewId;
@LayoutId(R.layout.item_library)
public class MangaLibraryHolder extends ItemViewHolder<Manga> {
@ViewId(R.id.thumbnailImageView)
ImageView mImageView;
@ViewId(R.id.nameTextView)
TextView mTextView;
public MangaLibraryHolder(View view) {
super(view);
}
public void onSetValues(Manga manga, PositionInfo positionInfo) {
mTextView.setText(manga.title);
Glide.with(getContext())
.load("http://img1.wikia.nocookie.net/__cb20090524204255/starwars/images/thumb/1/1a/R2d2.jpg/400px-R2d2.jpg")
.centerCrop()
.into(mImageView);
}
}

View File

@ -15,15 +15,16 @@ import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.LibraryPresenter; import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.ui.activity.BaseActivity; import eu.kanade.mangafeed.ui.activity.BaseActivity;
import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder;
import eu.kanade.mangafeed.view.LibraryView; import eu.kanade.mangafeed.view.LibraryView;
import uk.co.ribot.easyadapter.EasyAdapter;
public class LibraryFragment extends Fragment implements LibraryView { public class LibraryFragment extends Fragment implements LibraryView {
@Bind(R.id.gridView) GridView grid; @Bind(R.id.gridView) GridView grid;
LibraryPresenter mLibraryPresenter; LibraryPresenter mLibraryPresenter;
LibraryAdapter mAdapter; EasyAdapter<Manga> mEasyAdapter;
public static LibraryFragment newInstance() { public static LibraryFragment newInstance() {
LibraryFragment fragment = new LibraryFragment(); LibraryFragment fragment = new LibraryFragment();
@ -54,14 +55,15 @@ public class LibraryFragment extends Fragment implements LibraryView {
} }
public void setMangas(ArrayList<Manga> mangas) { public void setMangas(ArrayList<Manga> mangas) {
if (mAdapter == null) { if (mEasyAdapter == null) {
mAdapter = new LibraryAdapter( mEasyAdapter = new EasyAdapter<Manga>(
getActivity(), getActivity(),
R.layout.item_library, MangaLibraryHolder.class,
mangas); mangas
grid.setAdapter(mAdapter); );
grid.setAdapter(mEasyAdapter);
} else { } else {
mAdapter.setData(mangas); mEasyAdapter.setItems(mangas);
} }
} }
@ -69,7 +71,7 @@ public class LibraryFragment extends Fragment implements LibraryView {
private void setMangaClickListener() { private void setMangaClickListener() {
grid.setOnItemClickListener( grid.setOnItemClickListener(
(parent, view, position, id) -> (parent, view, position, id) ->
mLibraryPresenter.onMangaClick(mAdapter, position) mLibraryPresenter.onMangaClick(mEasyAdapter, position)
); );
} }

View File

@ -10,7 +10,7 @@
android:id="@+id/gridView" android:id="@+id/gridView"
android:padding="10dp" android:padding="10dp"
android:clipToPadding="false" android:clipToPadding="false"
android:verticalSpacing="4dp" android:verticalSpacing="8dp"
android:horizontalSpacing="8dp" android:horizontalSpacing="8dp"
android:columnWidth="96dp" android:columnWidth="96dp"
android:numColumns="auto_fit" android:numColumns="auto_fit"