From ad399ce706929a55da4872267604667d22be7b1d Mon Sep 17 00:00:00 2001 From: Sheng Xuan Date: Thu, 9 Feb 2017 15:04:29 +0800 Subject: [PATCH] Add MovieListFragment, its layout files and adapter. (It is not fully completed) --- .../movie_pigeon/MovieListFragment.java | 139 ++++++++++++++++++ .../adapters/MovieListAdapter.java | 79 ++++++++++ .../main/res/layout/fragment_movie_list.xml | 12 ++ .../src/main/res/layout/movie_list_item.xml | 27 ++++ 4 files changed, 257 insertions(+) create mode 100644 frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/MovieListFragment.java create mode 100644 frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/adapters/MovieListAdapter.java create mode 100644 frontend/Movie_Pigeon/app/src/main/res/layout/fragment_movie_list.xml create mode 100644 frontend/Movie_Pigeon/app/src/main/res/layout/movie_list_item.xml diff --git a/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/MovieListFragment.java b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/MovieListFragment.java new file mode 100644 index 0000000..7481783 --- /dev/null +++ b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/MovieListFragment.java @@ -0,0 +1,139 @@ +package org.example.team_pigeon.movie_pigeon; + +import android.app.Fragment; +import android.media.Image; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AbsListView; +import android.widget.AdapterView; +import android.widget.ImageView; +import android.widget.ListView; +import org.example.team_pigeon.movie_pigeon.adapters.MovieListAdapter; +import org.example.team_pigeon.movie_pigeon.models.Movie; +import java.util.ArrayList; + +/** + * Created by SHENGX on 2017/2/3. + */ + +public class MovieListFragment extends Fragment implements AdapterView.OnItemClickListener{ + private android.app.FragmentManager fragmentManager; + private ArrayList movies; + private ListView list_movies; + private MovieListAdapter movieListAdapter; + public android.os.Handler mHandler; + public View footerView; + public boolean isLoading = false; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){ + fragmentManager = getFragmentManager(); + movies = (ArrayList)getArguments().getSerializable("movies"); + View view = inflater.inflate(R.layout.fragment_movie_list,container,false); + list_movies = (ListView)view.findViewById(R.id.list_movies); + footerView = inflater.inflate(R.layout.footer_load_more,null); + mHandler = new footerHandler(); + movieListAdapter = new MovieListAdapter(movies,getActivity()); + list_movies.setAdapter(movieListAdapter); + list_movies.setOnScrollListener(new AbsListView.OnScrollListener() { + @Override + public void onScrollStateChanged(AbsListView view, int scrollState) { + + } + + @Override + public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { + if(view.getLastVisiblePosition() == totalItemCount-1 && list_movies.getCount() >= 10 && isLoading == false) { + isLoading = true; + Thread thread = new ThreadGetMoreMovies(); + thread.start(); + } + } + }); + list_movies.setOnItemClickListener(this); + return view; + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + MoviePageFragment moviePageFragment = new MoviePageFragment(); + Bundle bundle = new Bundle(); + bundle.putString("title",movieListAdapter.getItem(position).getTitle()); + bundle.putString("year",movieListAdapter.getItem(position).getProductionYear()); + bundle.putString("country",movieListAdapter.getItem(position).getCountry()); + bundle.putString("length",movieListAdapter.getItem(position).getLength()); + bundle.putString("director",movieListAdapter.getItem(position).getDirector()); + bundle.putString("poster",movieListAdapter.getItem(position).getPosterURL()); + bundle.putString("plot",movieListAdapter.getItem(position).getPlot()); + if(!movieListAdapter.getItem(position).getGenres().equals("NULL")) { + bundle.putString("genres", movieListAdapter.getItem(position).getGenres().replaceAll(", ", " / ")); + } + else { + bundle.putString("genres", "NULL"); + } + if(!movieListAdapter.getItem(position).getActors().equals("NULL")) { + bundle.putString("actors", movieListAdapter.getItem(position).getActors().replaceAll(", ", " / ")); + } + else { + bundle.putString("actors", "NULL"); + } + moviePageFragment.setArguments(bundle); + fragmentTransaction.replace(R.id.fl_content,moviePageFragment); + fragmentTransaction.addToBackStack(null); + fragmentTransaction.commit(); + } + + private ArrayList getMoreMovies() { + //For testing + ArrayList list = new ArrayList<>(); + list.add(new Movie("new","111")); + Movie movie = list.get(0); + movie.setPosterURL("https://s-media-cache-ak0.pinimg.com/736x/17/ba/70/17ba70b5d152cb9042e4a0785dccf834.jpg"); + list.add(new Movie("new2","1112")); + list.add(new Movie("new3","1113")); + list.add(new Movie("new4","1114")); + list.add(new Movie("new5","1115")); + list.add(new Movie("new6","1112")); + list.add(new Movie("new7","1111")); + return list; + } + + public class footerHandler extends Handler { + @Override + public void handleMessage(Message message) { + switch (message.what) { + case 0: + list_movies.addFooterView(footerView); + break; + case 1: + movieListAdapter.addListItemToAdapter((ArrayList)message.obj); + list_movies.removeFooterView(footerView); + isLoading = false; + break; + default: + break; + } + } + } + + public class ThreadGetMoreMovies extends Thread { + @Override + public void run() { + mHandler.sendEmptyMessage(0); + ArrayList moreMoviesList = getMoreMovies(); + try { + //For testing + Thread.sleep(5000); + }catch (InterruptedException e) { + e.printStackTrace(); + } + Message message = mHandler.obtainMessage(1,moreMoviesList); + mHandler.sendMessage(message); + } + } +} diff --git a/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/adapters/MovieListAdapter.java b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/adapters/MovieListAdapter.java new file mode 100644 index 0000000..d3e2ca0 --- /dev/null +++ b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/adapters/MovieListAdapter.java @@ -0,0 +1,79 @@ +package org.example.team_pigeon.movie_pigeon.adapters; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import com.nostra13.universalimageloader.core.ImageLoader; + +import org.example.team_pigeon.movie_pigeon.R; +import org.example.team_pigeon.movie_pigeon.models.Movie; + +import java.util.ArrayList; + +public class MovieListAdapter extends BaseAdapter { + private ArrayList movieList; + private Context mContext; + private Movie movie; + private ImageLoader imageLoader = ImageLoader.getInstance(); + + public MovieListAdapter(ArrayListmovieList,Context mContext) { + this.mContext = mContext; + this.movieList = movieList; + } + + public void addListItemToAdapter(ArrayList list) { + movieList.addAll(list); + this.notifyDataSetChanged(); + } + + @Override + public int getCount() { + return movieList.size(); + } + + @Override + public Movie getItem(int position) { + return movieList.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(mContext).inflate(R.layout.movie_list_item, parent, false); + viewHolder = new ViewHolder(); + viewHolder.txt_title = (TextView) convertView.findViewById(R.id.text_movie_list_title); + viewHolder.image_poster = (ImageView) convertView.findViewById(R.id.image_movie_list_poster); + convertView.setTag(viewHolder); + } + else { + viewHolder = (ViewHolder) convertView.getTag(); + } + movie = movieList.get(position); + viewHolder.txt_title.setText(movie.getTitle()); + + if(!movie.getPosterURL().equals("NULL")){ + imageLoader.displayImage(movieList.get(position).getPosterURL(),viewHolder.image_poster); + } + else{ + viewHolder.image_poster.setImageResource(R.mipmap.image_no_poster_found); + } + + return convertView; + } + + private static class ViewHolder { + TextView txt_title; + ImageView image_poster; + } + } diff --git a/frontend/Movie_Pigeon/app/src/main/res/layout/fragment_movie_list.xml b/frontend/Movie_Pigeon/app/src/main/res/layout/fragment_movie_list.xml new file mode 100644 index 0000000..f6401c9 --- /dev/null +++ b/frontend/Movie_Pigeon/app/src/main/res/layout/fragment_movie_list.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/frontend/Movie_Pigeon/app/src/main/res/layout/movie_list_item.xml b/frontend/Movie_Pigeon/app/src/main/res/layout/movie_list_item.xml new file mode 100644 index 0000000..7b04193 --- /dev/null +++ b/frontend/Movie_Pigeon/app/src/main/res/layout/movie_list_item.xml @@ -0,0 +1,27 @@ + + + + + + + + +