-
-
Save rajtheinnovator/4ae0ab873129eff84db68d5645ac64d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultMovieAdapter extends RecyclerView.Adapter<DefaultMovieAdapter.ViewHolder>{ | |
// Store a member variable for the movies | |
private ArrayList<Movie> mDefaultMovie; | |
// Store the context for easy access | |
private Context mContext; | |
private Movie currentMovie; | |
// Pass in the movies array into the constructor | |
public DefaultMovieAdapter(Context context, ArrayList<Movie> movies) { | |
mDefaultMovie = movies; | |
mContext = context; | |
} | |
// Easy access to the context object in the recyclerview | |
private Context getContext() { | |
return mContext; | |
} | |
/* | |
Provide a direct reference to each of the views within a data item | |
Used to cache the views within the item layout for fast access | |
*/ | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
/* | |
Your holder should contain a member variable | |
for any view that will be set as you render a row | |
*/ | |
public final TextView movieTitleTextView; | |
public final ImageView movieTitleImageView; | |
/* | |
We also create a constructor that accepts the entire item row | |
and does the view lookups to find each subview | |
*/ | |
public ViewHolder(View itemView) { | |
/* | |
Stores the itemView in a public final member variable that can be used | |
to access the context from any ViewHolder instance. | |
*/ | |
super(itemView); | |
movieTitleTextView = (TextView) itemView.findViewById(R.id.grid_item_movie_title); | |
movieTitleImageView = (ImageView) itemView.findViewById(R.id.grid_item_movie_image); | |
} | |
} | |
@Override | |
public DefaultMovieAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()). | |
inflate(R.layout.item_movies, parent, false); | |
return new ViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(DefaultMovieAdapter.ViewHolder viewHolder, int position) { | |
Log.v("############", "onBindViewHolder called"); | |
// Get the data model based on position | |
currentMovie = mDefaultMovie.get(position); | |
Log.v("############", "currentMovie called is "+currentMovie.toString()); | |
Log.v("############", "currentMovie's title is "+currentMovie.getMovieTitle().toString()); | |
/* | |
Set item views based on your views and data model | |
TextView textView = viewHolder.movieTitleTextView; | |
*/ | |
viewHolder.movieTitleTextView.setText(currentMovie.getMovieTitle()); | |
Log.v("############", "title is :>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+currentMovie.getMovieTitle()); | |
//ImageView button = viewHolder.movieTitleImageView; | |
//viewHolder.movieTitleImageView.setImageResource(R.mipmap.ic_launcher); | |
String url = "https://image.tmdb.org/t/p/w500/"+currentMovie.getMoviePosterPath().toString(); | |
Log.v("############", "poster path is :>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+currentMovie.getMoviePosterPath().toString()); | |
Picasso.with(getContext()) | |
.load(url) | |
.placeholder(R.mipmap.ic_launcher) | |
.into(viewHolder.movieTitleImageView); | |
} | |
@Override | |
public int getItemCount() { | |
Log.v("############", "getItemCount called with size "+ mDefaultMovie.size()); | |
return mDefaultMovie.size(); | |
} | |
public void setMovieData(ArrayList<Movie> weatherData) { | |
Log.v("############", "setMovieData Called"); | |
mDefaultMovie = weatherData; | |
Log.v("############", "mDefaultMovie is "+mDefaultMovie); | |
notifyDataSetChanged(); | |
Log.v("############", "notifyDataSetChanged Finished"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultMovieFragment extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<Movie>> { | |
private static final int DEFAULT_MOVIE_LOADER_ID = 1; | |
ArrayList<Movie> movies; | |
DefaultMovieAdapter mAdapter; | |
RecyclerView mRecyclerView; | |
public DefaultMovieFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
Log.v("############", "onCreateView called"); | |
// Inflate the layout for this fragment | |
View rootView = inflater.inflate(R.layout.fragment_default_movie, container, false); | |
Movie movie = new Movie("ram", 2, "path"); | |
if (savedInstanceState==null){ | |
movies = new ArrayList<>(); | |
} | |
//First of all check if network is connected or not then only start the loader | |
ConnectivityManager connMgr = (ConnectivityManager) | |
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); | |
if (networkInfo != null && networkInfo.isConnected()) { | |
/* fetch data. Get a reference to the LoaderManager, in order to interact with loaders. */ | |
startLoaderManager(); | |
Log.v("############", "startLoaderManager called"); | |
} | |
// Lookup the recyclerview in activity layout | |
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerViewMovies); | |
// Create mAdapter passing in the sample user data | |
mAdapter = new DefaultMovieAdapter(getActivity(), movies); | |
// Attach the mAdapter to the recyclerview to populate items | |
mRecyclerView.setAdapter(mAdapter); | |
// First param is number of columns and second param is orientation i.e Vertical or Horizontal | |
final StaggeredGridLayoutManager gridLayoutManager = | |
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); | |
// Attach the layout manager to the recycler view | |
mRecyclerView.setLayoutManager(gridLayoutManager); | |
// That's all! | |
return rootView; | |
} | |
private void startLoaderManager() { | |
LoaderManager loaderManager = getLoaderManager(); | |
loaderManager.initLoader(DEFAULT_MOVIE_LOADER_ID, null, this); | |
Log.v("############", "startLoaderManager finished"); | |
} | |
@Override | |
public Loader<ArrayList<Movie>> onCreateLoader(int id, Bundle args) { | |
Log.v("############", "onCreateLoader called"); | |
Uri baseUri = Uri.parse(UrlsAndConstants.DefaultQuery.DEFAULT_URL); | |
Log.v("############", "baseUri is "+baseUri.toString()); | |
Uri.Builder uriBuilder = baseUri.buildUpon(); | |
Log.v("############", "uriBuilder is "+uriBuilder.toString()); | |
uriBuilder.appendQueryParameter(API_KEY_PARAM, API_KEY_PARAM_VALUE); | |
Log.v("############", "uriBuilder.toString() is "+uriBuilder.toString()); | |
String urls = "https://api.themoviedb.org/3/discover/movie?api_key=4182aa25bab27d06344e404f65c4ae76"; | |
return new DefaultMovieLoader(getActivity().getApplicationContext(), urls); | |
} | |
@Override | |
public void onLoadFinished(Loader<ArrayList<Movie>> loader, ArrayList<Movie> movie) { | |
Log.v("############", "startLoaderManager finished"); | |
if (movie.isEmpty()) { | |
Log.v("******************", "movies isEmpty"); | |
return; | |
} else { | |
Log.v("############", "movies are"+movie); | |
// Attach the mAdapter to the recyclerview to populate items | |
mAdapter.setMovieData(movie); | |
mRecyclerView.setAdapter(mAdapter); | |
} | |
} | |
@Override | |
public void onLoaderReset(Loader<ArrayList<Movie>> loader) { | |
Log.v("############", "onLoaderReset called"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultMovieLoader extends AsyncTaskLoader { | |
/** | |
* Query URL | |
*/ | |
private String mUrl; | |
/** | |
* Constructs a new {@link DefaultMovieLoader}. | |
* | |
* @param context of the activity | |
* @param url to load data from | |
*/ | |
public DefaultMovieLoader(Context context, String url) { | |
super(context); | |
mUrl = url; | |
Log.v("############", "url is "+mUrl); | |
} | |
@Override | |
protected void onStartLoading() { | |
forceLoad(); | |
Log.v("############", "onStartLoading called"); | |
} | |
/** | |
* This is on a background thread. | |
*/ | |
@Override | |
public ArrayList<Movie> loadInBackground() { | |
if (mUrl == null) { | |
return null; | |
} | |
// Perform the network request, parse the response, and extract a list of news. | |
ArrayList<Movie> movies = QueryUtils.fetchMovieData(mUrl); | |
Log.v("############", "loadInBackground called"); | |
return movies; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="me.abhishekraj.showmyshow.MainActivity"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerViewMovies" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:scrollbars="vertical"/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<ImageView | |
android:id="@+id/grid_item_movie_image" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#634500" | |
android:layout_gravity="center_horizontal|center_vertical" | |
android:adjustViewBounds="true" | |
android:scaleType="centerCrop"/> | |
<TextView | |
android:id="@+id/grid_item_movie_title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/colorAccent" | |
android:layout_gravity="center_horizontal|center_vertical" | |
android:hint="@string/movieTitleHint"/> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment