Android :tablayout的各種巢狀
阿新 • • 發佈:2018-12-04
需要用到的許可權
<uses-permission android:name="android.permission.INTERNET"/>
需要用到的jar包
com.android.support:design:28.0.0 com.google.code.gson:gson:2.8.5 com.nostra13.universalimageloader:universal-image-loader:1.9.5 com.github.bumptech.glide:glide:4.8.0 implementation project(':pulltolibrary')
MainActivity
package com.bwie.administrator.yuekaomoni; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.FrameLayout; import android.widget.TextView; import com.bwie.administrator.yuekaomoni.fragment.ShowFragment; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView ShowMovie; private DrawerLayout MyDra; private FragmentManager manager; private ShowFragment showFragment; private FrameLayout FragmentLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { showFragment = new ShowFragment(); } private void initView() { ShowMovie = (TextView) findViewById(R.id.ShowMovie); ShowMovie.setOnClickListener(this); MyDra = (DrawerLayout) findViewById(R.id.MyDra); manager = getSupportFragmentManager(); } @Override public void onClick(View v) { manager.beginTransaction().replace(R.id.FragmentLayout, showFragment).commit(); MyDra.closeDrawers(); } }
Activity的xml 側滑選單
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/MyDra" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/FragmentLayout" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </RelativeLayout> <LinearLayout android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/colorPrimary" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/ic_launcher_round" /> <TextView android:id="@+id/ShowMovie" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="所有的電影" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
解析工具
package com.bwie.administrator.yuekaomoni.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtil {
public static String getStr(String mUrl){
try {
URL url = new URL(mUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
String jsons = getStream(stream);
return jsons;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String getStream(InputStream stream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();
String con;
while((con = reader.readLine())!=null){
sb.append(con);
}
reader.close();
return sb.toString();
}
}
bean,因為是兩個介面所以寫了兩個bean包
package com.bwie.administrator.yuekaomoni.bean;
import java.util.List;
public class MyData {
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
private boolean followMovie;
private int id;
private String imageUrl;
private String name;
private int rank;
private String summary;
public boolean isFollowMovie() {
return followMovie;
}
public void setFollowMovie(boolean followMovie) {
this.followMovie = followMovie;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
}
第二個bean
package com.bwie.administrator.yuekaomoni.bean;
import java.util.List;
public class WeiShangYing {
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
private boolean followMovie;
private int id;
private String imageUrl;
private String name;
private int rank;
private long releaseTime;
private String releaseTimeShow;
private String summary;
public boolean isFollowMovie() {
return followMovie;
}
public void setFollowMovie(boolean followMovie) {
this.followMovie = followMovie;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public long getReleaseTime() {
return releaseTime;
}
public void setReleaseTime(long releaseTime) {
this.releaseTime = releaseTime;
}
public String getReleaseTimeShow() {
return releaseTimeShow;
}
public void setReleaseTimeShow(String releaseTimeShow) {
this.releaseTimeShow = releaseTimeShow;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
}
介面卡
package com.bwie.administrator.yuekaomoni.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bwie.administrator.yuekaomoni.R;
import com.bwie.administrator.yuekaomoni.bean.MyData;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<MyData.ResultBean> datas;
public MyAdapter(Context context, ArrayList<MyData.ResultBean> datas) {
this.context = context;
this.datas = datas;
}
public ArrayList<MyData.ResultBean> getDatas() {
return datas;
}
public void setDatas(ArrayList<MyData.ResultBean> datas) {
this.datas = datas;
notifyDataSetChanged();
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.mylayout, null);
holder = new ViewHolder();
holder.iv_img = convertView.findViewById(R.id.Img_Show);
holder.tv_title = convertView.findViewById(R.id.My_Title);
holder.tv_content = convertView.findViewById(R.id.My_content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Glide.with(context).load(datas.get(position).getImageUrl()).into(holder.iv_img);
holder.tv_title.setText(datas.get(position).getName());
holder.tv_content.setText(datas.get(position).getSummary());
return convertView;
}
class ViewHolder {
ImageView iv_img;
TextView tv_title;
TextView tv_content;
}
}
xml的包
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/Img_Show"
android:layout_width="100dp"
android:layout_height="150dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/My_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp" />
<TextView
android:id="@+id/My_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
fragment的介面卡
package com.bwie.administrator.yuekaomoni.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
public class MyFragmentAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> datas;
public MyFragmentAdapter(FragmentManager fm, ArrayList<Fragment> datas) {
super(fm);
this.datas = datas;
}
@Override
public Fragment getItem(int i) {
return datas.get(i);
}
@Override
public int getCount() {
return datas.size();
}
}
未上映裡面的介面卡
package com.bwie.administrator.yuekaomoni.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bwie.administrator.yuekaomoni.R;
import com.bwie.administrator.yuekaomoni.bean.WeiShangYing;
import java.util.ArrayList;
public class MyWeiShang extends BaseAdapter {
private Context context;
private ArrayList<WeiShangYing.ResultBean> datas;
public MyWeiShang(Context context, ArrayList<WeiShangYing.ResultBean> datas) {
this.context = context;
this.datas = datas;
}
public ArrayList<WeiShangYing.ResultBean> getDatas() {
return datas;
}
public void setDatas(ArrayList<WeiShangYing.ResultBean> datas) {
this.datas = datas;
notifyDataSetChanged();
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.mylayout, null);
holder = new ViewHolder();
holder.iv_img = convertView.findViewById(R.id.Img_Show);
holder.tv_title = convertView.findViewById(R.id.My_Title);
holder.tv_content = convertView.findViewById(R.id.My_content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Glide.with(context).load(datas.get(position).getImageUrl()).into(holder.iv_img);
holder.tv_title.setText(datas.get(position).getName());
holder.tv_content.setText(datas.get(position).getSummary());
return convertView;
}
class ViewHolder {
ImageView iv_img;
TextView tv_title;
TextView tv_content;
}
}
//未上映的Fragment的介面卡
package com.bwie.administrator.yuekaomoni.adapter;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
public class MyTabFragmentAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> datas;
private ArrayList<String> titles;
public MyTabFragmentAdapter(FragmentManager fm, ArrayList<Fragment> datas, ArrayList<String> titles) {
super(fm);
this.datas = datas;
this.titles = titles;
}
@Override
public Fragment getItem(int i) {
return datas.get(i);
}
@Override
public int getCount() {
return datas.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
展示頁面的fragment
package com.bwie.administrator.yuekaomoni.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import com.bwie.administrator.yuekaomoni.R;
import com.bwie.administrator.yuekaomoni.adapter.MyFragmentAdapter;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class ShowFragment extends Fragment {
private RadioGroup My_Rg;
private ViewPager Show_vp;
private MyFragmentAdapter adapter;
private ArrayList<Fragment> datas = new ArrayList<Fragment>();
private MovieFragment movieFragment;
private CinemaFragment cinemaFragment;
private VipFragment vipFragment;
private SetFragment setFragment;
private View v;
private MyFragmentAdapter myFragmentAdapter;
private ArrayList<String> lists = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_show, null);
initView();
initData();
myFragmentAdapter = new MyFragmentAdapter(getActivity().getSupportFragmentManager(), datas);
Show_vp.setAdapter(myFragmentAdapter);
My_Rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//進行判斷
if (checkedId == R.id.rb1) {
Show_vp.setCurrentItem(0);
} else if (checkedId == R.id.rb2) {
Show_vp.setCurrentItem(1);
} else if (checkedId == R.id.rb3) {
Show_vp.setCurrentItem(2);
} else {
Show_vp.setCurrentItem(3);
}
}
});
Show_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
//進行判斷
if (i == 0) {
My_Rg.check(R.id.rb1);
} else if (i == 1) {
My_Rg.check(R.id.rb2);
} else if (i == 2) {
My_Rg.check(R.id.rb3);
} else {
My_Rg.check(R.id.rb4);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
return v;
}
//初始化fragment頁面
private void initData() {
movieFragment = new MovieFragment();
cinemaFragment = new CinemaFragment();
vipFragment = new VipFragment();
setFragment = new SetFragment();
datas.add(movieFragment);
datas.add(cinemaFragment);
datas.add(vipFragment);
datas.add(setFragment);
}
private void initView() {
My_Rg = (RadioGroup) v.findViewById(R.id.My_Rg);
Show_vp = (ViewPager) v.findViewById(R.id.Show_vp);
}
}
展示fragment的xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragment.ShowFragment">
<RadioGroup
android:id="@+id/My_Rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="影片" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="影院" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="會員" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="設定" />
</RadioGroup>
<android.support.v4.view.ViewPager
android:id="@+id/Show_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/My_Rg"></android.support.v4.view.ViewPager>
</RelativeLayout>
電影的fragment
package com.bwie.administrator.yuekaomoni.fragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bwie.administrator.yuekaomoni.R;
import com.bwie.administrator.yuekaomoni.adapter.MyTabFragmentAdapter;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class MovieFragment extends Fragment {
private TabLayout My_Tab;
private ViewPager My_Vp;
private ArrayList<Fragment> datas = new ArrayList<>();
private ArrayList<String> lists = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 引入檢視
View v = inflater.inflate(R.layout.fragment_movie, null);
//判斷為空才初始化資料
if (datas.size() <= 0) {
initData();
}
;
initView(v);
return v;
}
private void initData() {
datas.add(new HotVideoFragment());
datas.add(new NowShowingFragment());
datas.add(new RaoulRuizFragment());
lists.add("熱門電影");
lists.add("正在上映");
lists.add("即將上映");
}
private void initView(View v) {
My_Tab = (TabLayout) v.findViewById(R.id.My_Tab);
My_Vp = (ViewPager) v.findViewById(R.id.My_Vp);
My_Vp.setAdapter(new MyTabFragmentAdapter(getChildFragmentManager(), datas, lists));
My_Tab.setupWithViewPager(My_Vp);
}
}
電影的xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.MovieFragment">
<android.support.design.widget.TabLayout
android:id="@+id/My_Tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/My_Vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/My_Tab"></android.support.v4.view.ViewPager>
</RelativeLayout>
熱門電影的fragment
package com.bwie.administrator.yuekaomoni.fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.bwie.administrator.yuekaomoni.R;
import com.bwie.administrator.yuekaomoni.adapter.MyAdapter;
import com.bwie.administrator.yuekaomoni.bean.MyData;
import com.bwie.administrator.yuekaomoni.util.HttpUtil;
import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.google.gson.Gson;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class HotVideoFragment extends Fragment {
private ArrayList<MyData.ResultBean> datas = new ArrayList<>();
private String mUrl = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?userId=18&sessionId=15320748258726&page=1&count=10";
private PullToRefreshListView Pull_To;
private MyAdapter adapter;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
datas.addAll((ArrayList<MyData.ResultBean>) msg.obj);
//重新整理介面卡
adapter.notifyDataSetChanged();
//停止重新整理動畫
Pull_To.onRefreshComplete();
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 引入檢視
View v = inflater.inflate(R.layout.fragment_hot_video, null);
initView(v);
adapter = new MyAdapter(getActivity(), datas);
Pull_To.setAdapter(adapter);
initData();
//重新整理監聽事件
setListener();
return v;
}
private void setListener() {
Pull_To.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
datas.clear();
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
Toast.makeText(getActivity(), "沒有更多資料了", Toast.LENGTH_SHORT).show();
}
});
}
private void initData() {
//建立子執行緒重新整理資料
new Thread(new Runnable() {
@Override
public void run() {
String jsonstr = HttpUtil.getStr(mUrl);
Gson gson = new Gson();
MyData myData = gson.fromJson(jsonstr, MyData.class);
handler.sendMessage(handler.obtainMessage(0, myData.getResult()));
}
}).start();
}
private void initView(View v) {
Pull_To = (PullToRefreshListView) v.findViewById(R.id.Pull_To);
//設定上下拉滑動
Pull_To.setMode(PullToRefreshBase.Mode.BOTH);
}
}
熱門電影的xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.HotVideoFragment">
<com.example.pulltolibrary.PullToRefreshListView
android:id="@+id/Pull_To"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.pulltolibrary.PullToRefreshListView>
</RelativeLayout>
正在上映的fragment
package com.bwie.administrator.yuekaomoni.fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.bwie.administrator.yuekaomoni.R;
import com.bwie.administrator.yuekaomoni.adapter.MyWeiShang;
import com.bwie.administrator.yuekaomoni.bean.WeiShangYing;
import com.bwie.administrator.yuekaomoni.util.HttpUtil;
import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.google.gson.Gson;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class NowShowingFragment extends Fragment {
private ArrayList<WeiShangYing.ResultBean> datas = new ArrayList<>();
private String mUrl = "http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?userId=18&sessionId=15320748258726&count=10&page=";
private MyWeiShang adapter;
private int index = 1;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
datas.addAll((ArrayList<WeiShangYing.ResultBean>) msg.obj);
//重新整理介面卡
adapter.notifyDataSetChanged();
//停止重新整理動畫
PPPP.onRefreshComplete();
}
}
};
private PullToRefreshListView PPPP;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 引入檢視
View v = inflater.inflate(R.layout.fragment_now_showing, null);
initView(v);
adapter = new MyWeiShang(getActivity(), datas);
PPPP.setAdapter(adapter);
initData(index);
//重新整理監聽事件
setListener();
return v;
}
private void setListener() {
PPPP.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
index = 1;
datas.clear();
initData(index);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
index++;
initData(index);
}
});
}
private void initData(final int index) {
//建立子執行緒重新整理資料
new Thread(new Runnable() {
@Override
public void run() {
String jsonstr = HttpUtil.getStr(mUrl + index);
Gson gson = new Gson();
WeiShangYing ying = gson.fromJson(jsonstr, WeiShangYing.class);
handler.sendMessage(handler.obtainMessage(0, ying.getResult()));
}
}).start();
}
private void initView(View v) {
PPPP = (PullToRefreshListView) v.findViewById(R.id.PPPP);
//設定上下拉滑動
PPPP.setMode(PullToRefreshBase.Mode.BOTH);
}
}
正在上映的exml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.NowShowingFragment">
<com.example.pulltolibrary.PullToRefreshListView
android:id="@+id/PPPP"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.pulltolibrary.PullToRefreshListView>
</RelativeLayout>
剩下的即將上映也就不用寫了和其他的都一樣,主要就是佈局巢狀的問題
以上