这是我的适配器类
//adapter class
public class AdapterBoxOffice extends
RecyclerView.Adapter<AdapterBoxOffice.ViewHolderBoxOffice> {
private ArrayList<Movies> listMovies=new ArrayList<>();
private LayoutInflater layoutInflater;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
public AdapterBoxOffice(Context context){
layoutInflater=LayoutInflater.from(context);
volleySingleton=VolleySingleton.getInstance();
imageLoader=volleySingleton.getImageLoader();
}
public void setMovieList(ArrayList<Movies> listMovies){
this.listMovies=listMovies;
notifyItemRangeChanged(0,listMovies.size());
}
@Override
public ViewHolderBoxOffice onCreateViewHolder(ViewGroup parent, int
viewType) {
View view= layoutInflater.inflate(R.layout.custom_movie_box, parent,
false);
ViewHolderBoxOffice viewHolder=new ViewHolderBoxOffice(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolderBoxOffice holder, int
position) {
Movies currentMovies=listMovies.get(position);
holder.movieTitle.setText(currentMovies.getTitle());
holder.movieReleaseDate.setText(currentMovies.getReleaseDate().toString());
holder.movieAudienceScore.setRating(currentMovies.getAudienceScore()
/ 2.0f);
String urlPoster=currentMovies.getUrlPOster();
if (urlPoster!=null){
imageLoader.get(urlPoster, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response,
boolean isImmediate) {
holder.movieThumbnail.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
@Override
public int getItemCount() {
return listMovies.size();
}
static class ViewHolderBoxOffice extends RecyclerView.ViewHolder{
private ImageView movieThumbnail;
private TextView movieTitle;
private TextView movieReleaseDate;
private RatingBar movieAudienceScore;
public ViewHolderBoxOffice(View itemView){
super(itemView);
movieThumbnail= (ImageView)
itemView.findViewById(R.id.movieThumbnail);
movieTitle= (TextView) itemView.findViewById(R.id.movieTitle);
movieReleaseDate= (TextView)
itemView.findViewById(R.id.movieReleaseDate);
movieAudienceScore= (RatingBar)
itemView.findViewById(R.id.movieAudienceScore);
}
}
}
这是我的片段类
//fragment class
public class FragmentBoxOffice extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private RequestQueue requestQueue;
private ArrayList<Movies> listMovies=new ArrayList<>();
private AdapterBoxOffice adapterBoxOffice;
private DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
private RecyclerView listMovieHits;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentBoxOffice.
*/
// TODO: Rename and change types and number of parameters
public static FragmentBoxOffice newInstance(String param1, String param2)
{
FragmentBoxOffice fragment = new FragmentBoxOffice();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public static String getRequestUrl() {
return URL_BOX_OFFICE
+ URL_CHAR_QUESTION
+ URL_PARA_API_KEY + MyApplication.API_KEY;
}
public FragmentBoxOffice() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
sendJsonRequest();
}
public void sendJsonRequest() {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
getRequestUrl(),
null + " ",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listMovies= parseJSONResponse(response);
adapterBoxOffice.setMovieList(listMovies);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
private ArrayList<Movies> parseJSONResponse(JSONObject response) {
ArrayList<Movies> listMovies=new ArrayList<>();
if (response !=null && response.length() >0) {
try {
JSONArray arrayMovies = response.getJSONArray(KEY_RESULTS);
for (int i = 0; i < arrayMovies.length(); i++) {
JSONObject currentMovies = arrayMovies.getJSONObject(i);
//get the id of the movies
long id = currentMovies.getLong(KEY_ID);
//get the title of the movies
String title = currentMovies.getString(KEY_TITLE);
//get the release date of the movies
String releaseDate = null;
if (currentMovies.has(KEY_RELEASE_DATE)) {
releaseDate =
currentMovies.getString(KEY_RELEASE_DATE);
} else {
releaseDate = "NA";
}
//get the ratings of the movies
int audienceScore = -1;
if (currentMovies.has(KEY_RATINGS)) {
audienceScore = currentMovies.getInt(KEY_RATINGS);
}
//get the overview of movies
String overview = currentMovies.getString(KEY_OVERVIEW);
//get the posters of movies
String urlPOster = null;
if (currentMovies.has(KEY_POSTER)) {
urlPOster = currentMovies.getString(URL_POSTER +
KEY_POSTER);
}
Movies movie = new Movies();
movie.setId(id);
movie.setTitle(title);
Date date = dateFormat.parse(releaseDate);
movie.setReleaseDate(date);
movie.setAudienceScore(audienceScore);
movie.setUrlPOster(urlPOster);
movie.setOverview(overview);
listMovies.add(movie);
}
} catch (JSONException e) {
} catch (ParseException e) {
}
}
return listMovies;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_box_office, container,
false);
listMovieHits= (RecyclerView) view.findViewById(R.id.movieHits);
listMovieHits.setLayoutManager(new
LinearLayoutManager(getActivity()));
adapterBoxOffice=new AdapterBoxOffice(getActivity());
listMovieHits.setAdapter(adapterBoxOffice);
sendJsonRequest();
return view;
}
}
Getter和setter方法
//getter and setter methods
public class Movies {
private long id;
private String title;
private Date releaseDate;
private int audienceScore;
private String urlPOster;
private String overview;
public Movies(){
this.id=id;
this.title=title;
this.releaseDate=releaseDate;
this.audienceScore=audienceScore;
this.urlPOster=urlPOster;
this.overview=overview;
}
public long getId(){return id;}
public void setId(long id){this.id=id;}
public String getTitle(){return title;}
public void setTitle(String title){this.title=title;}
public Date getReleaseDate(){return releaseDate;}
public void setReleaseDate(Date releaseDate)
{this.releaseDate=releaseDate;}
public int getAudienceScore(){return audienceScore;}
public void setAudienceScore(int audienceScore)
{this.audienceScore=audienceScore;}
public String getUrlPOster(){return urlPOster;}
public void setUrlPOster(String urlPOster){this.urlPOster=urlPOster;}
public String getOverview(){return overview;}
public void setOverview(String overview){this.overview=overview;}
@Override
public String toString() {
return "ID:"+id+
"Title"+title+
"Date "+releaseDate+
"Vote "+audienceScore+
"Poster "+urlPOster+
"Overview "+overview;
}
}