Skip to content

Commit

Permalink
Add MovieListFragment, its layout files and adapter. (It is not fully…
Browse files Browse the repository at this point in the history
… completed)
  • Loading branch information
Sheng-Xuan committed Feb 9, 2017
1 parent 0ff319f commit ad399ce
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Movie> 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<Movie>)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<Movie> getMoreMovies() {
//For testing
ArrayList<Movie> 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<Movie>)message.obj);
list_movies.removeFooterView(footerView);
isLoading = false;
break;
default:
break;
}
}
}

public class ThreadGetMoreMovies extends Thread {
@Override
public void run() {
mHandler.sendEmptyMessage(0);
ArrayList<Movie> moreMoviesList = getMoreMovies();
try {
//For testing
Thread.sleep(5000);
}catch (InterruptedException e) {
e.printStackTrace();
}
Message message = mHandler.obtainMessage(1,moreMoviesList);
mHandler.sendMessage(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Movie> movieList;
private Context mContext;
private Movie movie;
private ImageLoader imageLoader = ImageLoader.getInstance();

public MovieListAdapter(ArrayList<Movie>movieList,Context mContext) {
this.mContext = mContext;
this.movieList = movieList;
}

public void addListItemToAdapter(ArrayList<Movie> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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="match_parent"
android:orientation="horizontal">

<ListView
android:id="@+id/list_movies"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
27 changes: 27 additions & 0 deletions frontend/Movie_Pigeon/app/src/main/res/layout/movie_list_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/movie_list_item"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="3dp">


<ImageView
android:layout_width="68dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:id="@+id/image_movie_list_poster" />

<TextView
android:text="TextView"
android:paddingLeft="12dp"
android:textSize="18sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_movie_list_title" />

</LinearLayout>

0 comments on commit ad399ce

Please sign in to comment.