电影fragment模块代码
首页
MainFragment.java
package com.example.smartcity.fragment.movie;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.smartcity.R;
import com.example.smartcity.activity.movie.PreviewActivity;
import com.example.smartcity.activity.movie.CinemaActivity;
import com.example.smartcity.activity.movie.FilmInfoActivity;
import com.example.smartcity.activity.movie.ServicesActivity;
import com.example.smartcity.activity.movie.StarNewsActivity;
import com.example.smartcity.adpter.SuperBase;
import com.example.smartcity.adpter.SuperPages;
import com.example.smartcity.bean.ServicesIcon;
import com.example.smartcity.bean.SkipActivity;
import com.example.smartcity.http.initMovieHttp;
import com.example.smartcity.info.movie.HotFilmInfo;
import com.example.smartcity.utils.GetNetImage;
import java.util.ArrayList;
import static com.example.smartcity.utils.CacheUtils.cinemaInfo;
import static com.example.smartcity.utils.CacheUtils.homeRotation;
import static com.example.smartcity.utils.CacheUtils.hotFilmInfo;
import static com.example.smartcity.utils.CacheUtils.recommendInfo;
import static com.example.smartcity.utils.CacheUtils.weather;
import static com.example.smartcity.utils.ConstantsUtils.CINEMA_WHAT;
import static com.example.smartcity.utils.ConstantsUtils.HOME_IMG_WHAT;
import static com.example.smartcity.utils.ConstantsUtils.HOT_FILM_WHAT;
import static com.example.smartcity.utils.ConstantsUtils.RECOMMEND_WHAT;
import static com.example.smartcity.utils.ConstantsUtils.WEATHER_WHAT;
import static com.example.smartcity.utils.KeyUtils.POSITION;
import static com.example.smartcity.utils.KeyUtils.TYPE;
public class MainFragment extends Fragment {
private TextView mMainAddress;
private SearchView mMainFilmName;
private ViewPager mMainViewpager;
private TextView mWeather;
private GridView mMainServices;
private GridView mMainHotFilm;
private GridView mMainRecommendFilm;
private ListView mSideCinema;
private RadioGroup main_rg;
private ProgressBar load_img;
private ProgressBar load_cinema;
//实例化
ServicesIcon servicesIcon1;
ServicesIcon servicesIcon2;
ServicesIcon servicesIcon3;
ServicesIcon servicesIcon4;
Handler handler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case HOME_IMG_WHAT:
loadRotation();
load_img.setVisibility(View.GONE);
break;
case WEATHER_WHAT:
if (weather.getCode() == 200) {
mWeather.setText("天气:"+weather.getData().getWeather()+"\t\t温度最高:" + weather.getData().getMinTemperature() + "\t\t温度最低:" + weather.getData().getMaxTemperature() +
"\t\t湿度:" + weather.getData().getHumidity() + "\t\t空气质量:"+weather.getData().getAir());
}
break;
case HOT_FILM_WHAT:
loadHotFilm();
break;
case RECOMMEND_WHAT:
loadRecommendFilm();
break;
case CINEMA_WHAT:
loadCinema();
load_cinema.setVisibility(View.GONE);
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
mMainAddress = (TextView) view.findViewById(R.id.main_address);
mMainFilmName = view.findViewById(R.id.main_film_name);
mMainViewpager = (ViewPager) view.findViewById(R.id.main_viewpager);
mWeather = (TextView) view.findViewById(R.id.weather);
mMainServices = (GridView) view.findViewById(R.id.main_services);
mMainHotFilm = (GridView) view.findViewById(R.id.main_hot_film);
mMainRecommendFilm = (GridView) view.findViewById(R.id.main_recommend_film);
mSideCinema = (ListView) view.findViewById(R.id.side_cinema);
main_rg = view.findViewById(R.id.main_rg);
load_img = view.findViewById(R.id.load_img);
load_cinema = view.findViewById(R.id.load_cinema);
mSideCinema.setOnItemClickListener(new sideCinemaItemClick());
mMainServices.setOnItemClickListener(new servicesItemClick());
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
initData();
loadServices();//设置应用功能
}
//初始化
public void initData() {
new initMovieHttp(handler).rotationData();
new initMovieHttp(handler).weatherData();
new initMovieHttp(handler).hotFilmData();
new initMovieHttp(handler).recommendFilmData();
new initMovieHttp(handler).cinemaData();
}
//应用功能点击
class servicesItemClick implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Intent intentRecommend = new Intent(getContext(), ServicesActivity.class);
startActivity(intentRecommend);
break;
case 1:
Intent intentPreview = new Intent(getContext(), PreviewActivity.class);
startActivity(intentPreview);
break;
case 2:
break;
case 3:
Intent intentStarNews = new Intent(getContext(), StarNewsActivity.class);
startActivity(intentStarNews);
break;
}
}
}
//周边影院条目点击事件
class sideCinemaItemClick implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = cinemaInfo.getRows().get(position).getName();
SkipActivity.skipEmptyActivity(name,getContext());
}
}
//跳转影片详情页
/**
* 1、点击条目的位置
* 2、类型
* */
public void skipFilmInfo(int position,String types){
Intent intent = new Intent(getContext(), FilmInfoActivity.class);
intent.putExtra(POSITION,position);
intent.putExtra(TYPE,types);
startActivity(intent);
}
//设置周边影院信息
public void loadCinema(){
SuperBase superBase = new SuperBase(cinemaInfo.getRows().size()) {
private ImageView mCinemaImage;
private TextView mCinemaTxt;
private TextView mCinemaAddress;
private RatingBar mCinemaLevel;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_cinema,parent,false);
mCinemaImage = (ImageView) convertView.findViewById(R.id.cinema_image);
mCinemaTxt = (TextView) convertView.findViewById(R.id.cinema_txt);
mCinemaAddress = (TextView) convertView.findViewById(R.id.cinema_address);
mCinemaLevel = (RatingBar) convertView.findViewById(R.id.cinema_level);
new GetNetImage(getContext(),cinemaInfo.getRows().get(position).getCover(),mCinemaImage);
mCinemaTxt.setText(cinemaInfo.getRows().get(position).getName());
mCinemaAddress.setText(cinemaInfo.getRows().get(position).getAddress());
// float num = 0;
// ArrayList<Float> floats = new ArrayList<Float>();
//
// float result = 0;
// Log.e("TAG", "getView: "+position);
// for (int i = 0; i < position; i++) {
// num = (cinemaInfo.getRows().get(position).getScore()) / 10 / 2;
// result += num;
// floats.add(result);
// result = 0;
// num = 0;
// }
// for (int j = 0; j < position; j++) {
// mCinemaLevel.setRating(floats.get(j));
// }
return convertView;
}
};
mSideCinema.setAdapter(superBase);
}
//设置即将上映信息
public void loadRecommendFilm(){
int mSize = recommendInfo.getRows().size();
mMainRecommendFilm.setNumColumns(mSize);
SuperBase superBase = new SuperBase(mSize) {
private ImageView mFilmImage;
private TextView mFilmTitle;
private Button mFilmBuy;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_recommend_film,parent,false);
mFilmImage = (ImageView) convertView.findViewById(R.id.film_image);
mFilmTitle = (TextView) convertView.findViewById(R.id.film_title);
mFilmBuy = (Button) convertView.findViewById(R.id.film_buy);
new GetNetImage(getContext(),recommendInfo.getRows().get(position).getCover(),mFilmImage);
mFilmTitle.setText(recommendInfo.getRows().get(position).getName());
//点击图片跳转到相应的电影详情界面
mFilmImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
skipFilmInfo(position,"即将上映");
}
});
return convertView;
}
};
mMainRecommendFilm.setAdapter(superBase);
}
//设置正在热映信息
public void loadHotFilm(){
//判断是否为热映
ArrayList<HotFilmInfo.RowsDTO> hotFilmInfoArr = new ArrayList<>();
for (int i = 0; i < hotFilmInfo.getRows().size(); i++) {
if (hotFilmInfo.getRows().get(i).getRecommend().equals("Y")){
hotFilmInfoArr.add(hotFilmInfo.getRows().get(i));
}
}
mMainHotFilm.setNumColumns(hotFilmInfoArr.size());
SuperBase superBase = new SuperBase(hotFilmInfoArr.size()) {
private ImageView mFilmImage;
private TextView mFilmTitle;
private Button mFilmBuy;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_hot_film,parent,false);
mFilmImage = (ImageView) convertView.findViewById(R.id.film_image);
mFilmTitle = (TextView) convertView.findViewById(R.id.film_title);
mFilmBuy = (Button) convertView.findViewById(R.id.film_buy);
new GetNetImage(getContext(),hotFilmInfoArr.get(position).getCover(),mFilmImage);
mFilmTitle.setText(hotFilmInfoArr.get(position).getName());
//点击图片跳转到相应的电影详情界面
mFilmImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
skipFilmInfo(position,"正在热映");
}
});
//点击购票,跳转
mFilmBuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), CinemaActivity.class);
intent.putExtra(POSITION,position);
startActivity(intent);
}
});
return convertView;
}
};
mMainHotFilm.setAdapter(superBase);
}
//设置应用功能
public void loadServices(){
//设置假数据
servicesIcon1 = new ServicesIcon();
servicesIcon2 = new ServicesIcon();
servicesIcon3 = new ServicesIcon();
servicesIcon4 = new ServicesIcon();
servicesIcon1.setImage(R.drawable.img_home3);
servicesIcon1.setTitle("推荐");
servicesIcon2.setImage(R.drawable.img_home4);
servicesIcon2.setTitle("预告");
servicesIcon3.setImage(R.drawable.img_home5);
servicesIcon3.setTitle("影评");
servicesIcon4.setImage(R.drawable.img_home6);
servicesIcon4.setTitle("新闻");
ArrayList<ServicesIcon> servicesIcons = new ArrayList<>();
servicesIcons.add(servicesIcon1);
servicesIcons.add(servicesIcon2);
servicesIcons.add(servicesIcon3);
servicesIcons.add(servicesIcon4);
SuperBase superBase = new SuperBase(servicesIcons.size()) {
private ImageView mServicesIcon;
private TextView mServicesTxt;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_services,parent,false);
mServicesIcon = (ImageView) convertView.findViewById(R.id.services_icon);
mServicesTxt = (TextView) convertView.findViewById(R.id.services_txt);
mServicesIcon.setBackgroundResource(servicesIcons.get(position).getImage());
mServicesTxt.setText(servicesIcons.get(position).getTitle());
return convertView;
}
};
mMainServices.setAdapter(superBase);
}
//设置轮播图
public void loadRotation() {
ArrayList<ImageView> imageViews = new ArrayList<>();//定义一个集合存放图片
for (int i = 0; i < homeRotation.getRows().size(); i++) {
ImageView imageView = new ImageView(getContext());
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
new GetNetImage(getContext(), homeRotation.getRows().get(i).getAdvImg(), imageView);
imageViews.add(imageView);
}
SuperPages superPages = new SuperPages(homeRotation.getRows().size(), main_rg, getContext()) {
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = imageViews.get(position);
container.addView(imageView);
return imageView;
}
};
mMainViewpager.setAdapter(superPages);
//圆圈标识
main_rg.check(0);
mMainViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
main_rg.check(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
fragment_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragment.movie.MainFragment"
android:background="#F6F6F6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#CA3939"
android:orientation="horizontal">
<TextView
android:id="@+id/main_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="大连市"
android:textColor="@color/white"
android:textSize="20dp" />
<androidx.appcompat.widget.SearchView
android:id="@+id/main_film_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:background="#DFFFFFFF"
android:imeOptions="actionSearch"
app:queryHint="请输入电影名" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp">
<ProgressBar
android:id="@+id/load_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RadioGroup
android:id="@+id/main_rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:layout_marginTop="150dp"
android:orientation="horizontal" />
</FrameLayout>
<TextView
android:id="@+id/weather"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#99D3D3D3"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="#FF9800"
android:text="111dsjaiufaf bhaduishfiahdufuad dhfiuiahsfi ihadsifha jfdiajfiasdfuai"
android:gravity="center">
<requestFocus/>
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="应用功能" />
<GridView
android:id="@+id/main_services"
android:layout_width="match_parent"
android:layout_height="100dp"
android:numColumns="4"
android:layout_margin="5dp"
android:background="@drawable/bg_normal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="正在热映" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="195dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_normal">
<GridView
android:id="@+id/main_hot_film"
android:layout_width="1500dp"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="7"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</HorizontalScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="即将上映" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_normal">
<GridView
android:id="@+id/main_recommend_film"
android:layout_width="1500dp"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="7"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</HorizontalScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="周边影院" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/load_cinema"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"/>
<ListView
android:id="@+id/side_cinema"
android:layout_width="match_parent"
android:layout_height="1000dp" />
</FrameLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
影院
CinemaFragment.java
package com.example.smartcity.fragment.movie;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import com.example.smartcity.R;
import com.example.smartcity.adpter.SuperBase;
import com.example.smartcity.bean.SkipActivity;
import com.example.smartcity.http.initMovieHttp;
import com.example.smartcity.utils.GetNetImage;
import static com.example.smartcity.utils.CacheUtils.*;
import static com.example.smartcity.utils.ConstantsUtils.CINEMA_WHAT;
public class CinemaFragment extends Fragment {
private TextView mMainAddress;
private SearchView mMainCinemaName;
private ListView mCinemaList;
private ProgressBar load_cinema_list;
Handler handler = new Handler(Looper.myLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case CINEMA_WHAT:
loadCinemaList();
load_cinema_list.setVisibility(View.GONE);
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cinema, container, false);
mMainAddress = (TextView) view.findViewById(R.id.cinema_fragment_address);
mMainCinemaName = (SearchView) view.findViewById(R.id.main_cinema_name);
mCinemaList = (ListView) view.findViewById(R.id.cinema_list);
load_cinema_list = view.findViewById(R.id.load_cinema_list);
new initMovieHttp(handler).cinemaData();
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
mCinemaList.setOnItemClickListener(new CinemaListItemClick());
}
//电影院条目点击
class CinemaListItemClick implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = cinemaInfo.getRows().get(position).getName();
SkipActivity.skipEmptyActivity(name,getContext());
}
}
//设置影院列表
public void loadCinemaList(){
SuperBase superBase = new SuperBase(cinemaInfo.getRows().size()) {
private ImageView mCinemaImage;
private TextView mCinemaTxt;
private TextView mCinemaAddress;
private RatingBar mCinemaLevel;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_cinema,parent,false);
mCinemaImage = (ImageView) convertView.findViewById(R.id.cinema_image);
mCinemaTxt = (TextView) convertView.findViewById(R.id.cinema_txt);
mCinemaAddress = (TextView) convertView.findViewById(R.id.cinema_address);
mCinemaLevel = (RatingBar) convertView.findViewById(R.id.cinema_level);
new GetNetImage(getContext(), cinemaInfo.getRows().get(position).getCover(),mCinemaImage);
mCinemaTxt.setText(cinemaInfo.getRows().get(position).getName());
mCinemaAddress.setText(cinemaInfo.getRows().get(position).getAddress());
return convertView;
}
};
mCinemaList.setAdapter(superBase);
}
}
fragment_cinema.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragment.movie.CinemaFragment"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#CA3939"
android:orientation="horizontal">
<TextView
android:id="@+id/cinema_fragment_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="大连市"
android:textColor="@color/white"
android:textSize="20dp" />
<androidx.appcompat.widget.SearchView
android:id="@+id/main_cinema_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:background="#DFFFFFFF"
android:imeOptions="actionSearch"
app:queryHint="请输入影院名" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<ProgressBar
android:id="@+id/load_cinema_list"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"/>
<ListView
android:id="@+id/cinema_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
影片
FilmFragment.java
package com.example.smartcity.fragment.movie;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.smartcity.R;
import com.example.smartcity.activity.movie.FilmInfoActivity;
import com.example.smartcity.adpter.SuperBase;
import com.example.smartcity.http.initMovieHttp;
import com.example.smartcity.info.movie.HotFilmInfo;
import com.example.smartcity.utils.GetNetImage;
import java.util.ArrayList;
import static com.example.smartcity.utils.CacheUtils.hotFilmInfo;
import static com.example.smartcity.utils.ConstantsUtils.HOT_FILM_WHAT;
import static com.example.smartcity.utils.KeyUtils.POSITION;
import static com.example.smartcity.utils.KeyUtils.TYPE;
public class FilmFragment extends Fragment {
private ProgressBar mLoadFilmList;
private ListView mFilmList;
Handler handler = new Handler(Looper.myLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case HOT_FILM_WHAT:
loadFilmList();
mLoadFilmList.setVisibility(View.GONE);
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_film, container, false);
mLoadFilmList = (ProgressBar) view.findViewById(R.id.load_film_list);
mFilmList = (ListView) view.findViewById(R.id.film_list);
new initMovieHttp(handler).hotFilmData();
return view;
}
public void loadFilmList(){
SuperBase superBase = new SuperBase(hotFilmInfo.getRows().size()) {
private ImageView mFilmImg;
private TextView mFilmName;
private TextView mFilmType;
private TextView mFilmPlayType;
private TextView mFilmScore;
private Button mBtnFilmBuy;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_film_list,parent,false);
mFilmImg = (ImageView) convertView.findViewById(R.id.film_img);
mFilmName = (TextView) convertView.findViewById(R.id.film_name);
mFilmType = (TextView) convertView.findViewById(R.id.film_type);
mFilmPlayType = (TextView) convertView.findViewById(R.id.film_play_type);
mFilmScore = (TextView) convertView.findViewById(R.id.film_score);
mBtnFilmBuy = (Button) convertView.findViewById(R.id.btn_film_buy);
//设置数据
HotFilmInfo.RowsDTO rowsDTOS = hotFilmInfo.getRows().get(position);
new GetNetImage(getContext(), rowsDTOS.getCover(),mFilmImg);
mFilmName.setText(rowsDTOS.getName());
mFilmType.setText(rowsDTOS.getCategory());
//类型
switch (rowsDTOS.getCategory()) {
case "1":
mFilmType.setText("影片类型:故事");
break;
case "2":
mFilmType.setText("影片类型:爱情");
break;
case "3":
mFilmType.setText("影片类型:动作");
break;
case "4":
mFilmType.setText("影片类型:喜剧");
break;
case "5":
mFilmType.setText("影片类型:恐怖");
break;
case "6":
mFilmType.setText("影片类型:惊悚");
break;
case "7":
mFilmType.setText("影片类型:战争");
break;
case "8":
mFilmType.setText("影片类型:科幻");
break;
}
mFilmPlayType.setText("播放类型:"+rowsDTOS.getPlayType());
String sum = "";
String star = "☆";
ArrayList<String> starList = new ArrayList<>();
for (int i = 0; i < rowsDTOS.getScore(); i++) {
sum += star;
starList.add(sum);
}
sum = "";
for (int j = 0; j < starList.size(); j++) {
mFilmScore.setText(starList.get(j));
}
//购票按钮点击事件
mBtnFilmBuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), FilmInfoActivity.class);
intent.putExtra(POSITION , position);
intent.putExtra(TYPE , "正在热映");
startActivity(intent);
}
});//点击跳转
return convertView;
}
};
mFilmList.setAdapter(superBase);
}
}
fragment_film.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.movie.FilmFragment"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#CA3939"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="影 片"
android:textSize="30dp"
android:textColor="@color/white"
android:gravity="center"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<ProgressBar
android:id="@+id/load_film_list"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"/>
<ListView
android:id="@+id/film_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>