mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Use EasyAdapter
This commit is contained in:
		| @@ -1,7 +1,6 @@ | ||||
| package eu.kanade.mangafeed.presenter; | ||||
|  | ||||
| import android.content.Intent; | ||||
| import android.widget.GridView; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
|  | ||||
| @@ -11,8 +10,8 @@ import de.greenrobot.event.EventBus; | ||||
| import eu.kanade.mangafeed.App; | ||||
| import eu.kanade.mangafeed.data.helpers.DatabaseHelper; | ||||
| import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; | ||||
| import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; | ||||
| import eu.kanade.mangafeed.view.LibraryView; | ||||
| import uk.co.ribot.easyadapter.EasyAdapter; | ||||
|  | ||||
| import static rx.android.schedulers.AndroidSchedulers.mainThread; | ||||
|  | ||||
| @@ -28,7 +27,7 @@ public class LibraryPresenter { | ||||
|         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); | ||||
|         EventBus.getDefault().postSticky(adapter.getItem(position)); | ||||
|         mLibraryView.getActivity().startActivity(intent); | ||||
|   | ||||
| @@ -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); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
| @@ -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); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -15,15 +15,16 @@ import eu.kanade.mangafeed.R; | ||||
| import eu.kanade.mangafeed.data.models.Manga; | ||||
| import eu.kanade.mangafeed.presenter.LibraryPresenter; | ||||
| 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 uk.co.ribot.easyadapter.EasyAdapter; | ||||
|  | ||||
|  | ||||
| public class LibraryFragment extends Fragment implements LibraryView { | ||||
|  | ||||
|     @Bind(R.id.gridView) GridView grid; | ||||
|     LibraryPresenter mLibraryPresenter; | ||||
|     LibraryAdapter mAdapter; | ||||
|     EasyAdapter<Manga> mEasyAdapter; | ||||
|  | ||||
|     public static LibraryFragment newInstance() { | ||||
|         LibraryFragment fragment = new LibraryFragment(); | ||||
| @@ -54,14 +55,15 @@ public class LibraryFragment extends Fragment implements LibraryView { | ||||
|     } | ||||
|  | ||||
|     public void setMangas(ArrayList<Manga> mangas) { | ||||
|         if (mAdapter == null) { | ||||
|             mAdapter = new LibraryAdapter( | ||||
|         if (mEasyAdapter == null) { | ||||
|             mEasyAdapter = new EasyAdapter<Manga>( | ||||
|                     getActivity(), | ||||
|                     R.layout.item_library, | ||||
|                     mangas); | ||||
|             grid.setAdapter(mAdapter); | ||||
|                     MangaLibraryHolder.class, | ||||
|                     mangas | ||||
|             ); | ||||
|             grid.setAdapter(mEasyAdapter); | ||||
|         } else { | ||||
|             mAdapter.setData(mangas); | ||||
|             mEasyAdapter.setItems(mangas); | ||||
|         } | ||||
|  | ||||
|     } | ||||
| @@ -69,7 +71,7 @@ public class LibraryFragment extends Fragment implements LibraryView { | ||||
|     private void setMangaClickListener() { | ||||
|         grid.setOnItemClickListener( | ||||
|                 (parent, view, position, id) -> | ||||
|                     mLibraryPresenter.onMangaClick(mAdapter, position) | ||||
|                     mLibraryPresenter.onMangaClick(mEasyAdapter, position) | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user