public class FirstpageFragmentextends MyBaseFragment {
private PullToRefreshListViewptrListView;
private ImageView setBtn;
private ImageView msgBtn;
private FirstPagePtrAdapterptradapter;
//控制当前页数
private int page = 0;
//用于适配器添加数据的列表
privateList<NewsEntity.DataBean> list;
//
private ListView listView;
//头视图
private View headerView;
private LinearLayout linearLayout;
private TextView titleTv;
private ViewPager viewPager;
private FirstPageViewPagerAdapterviewpageradapter;
//用于首页viewpager展示的图片列表
private List<ImageView>imageViewList;
// 记录ViewPager当前展示的页码
private int currentPosition = 1;
//用于控制自动轮播
private Timer timer;
private int MSG_WHAT = 1;
private Handler handler = newHandler() {
@Override
public void handleMessage(Messagemsg) {
super.handleMessage(msg);
if (msg.what == MSG_WHAT) {
currentPosition++;
viewPager.setCurrentItem(currentPosition);
}
}
};
public static FirstpageFragmentnewInstance() {
Bundle args = new Bundle();
FirstpageFragment fragment = newFirstpageFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public ViewonCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @NullableBundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.fragment_firstpage, container, false);
//初始化碎片页面内容
init(rootView);
return rootView;
}
private void init(View rootView) {
//查找控件
ptrListView =((PullToRefreshListView)rootView.findViewById(R.id.fragment_firstpage_ptrListViewId));
setBtn = (ImageView)rootView.findViewById(R.id.fragment_firstpage_set_btnId);
msgBtn = ((ImageView)rootView.findViewById(R.id.fragment_firstpage_msg_btnId));
list = new ArrayList<>();
imageViewList = newArrayList<>();
//初始化ptrListView的适配器
ptradapter = newFirstPagePtrAdapter(getContext());
//获得ptrListView可操作的listView
listView =ptrListView.getRefreshableView();
//添加头视图操作
initHeaderView();
listView.addHeaderView(headerView);
//设置适配器
listView.setAdapter(ptradapter);
//设置上啦和下拉刷新监听
ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
ptrListView.setOnRefreshListener(newPullToRefreshBase.OnRefreshListener2<ListView>() {
//下拉刷新
@Override
public voidonPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
ptradapter.clear();
page = 0;
loadData(0);
sleepThread();
}
//上啦加载更多
@Override
public voidonPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page++;
loadData(page);
sleepThread();
}
});
//加载数据
if (page == 0) {
loadData(page);
}
//设置点击监听
ptradapter.setListener(newFirstPagePtrAdapter.OnPtrItemClickListener() {
@Override
public void response(Viewview, int position) {
Intent intent = newIntent(getActivity(), WebViewActivity.class);
intent.putExtra("id",Integer.parseInt(ptradapter.getItem(position).getId()));
intent.putExtra("index", 2);
startActivity(intent);
}
});
}
private void initHeaderView() {
headerView =LayoutInflater.from(getContext()).inflate(R.layout.fragment_firstpage_header,listView, false);
//查找头视图中的控件
linearLayout = (LinearLayout)headerView.findViewById(R.id.fragment_firstpage_header_linearLayoutId);
titleTv = (TextView)headerView.findViewById(R.id.fragment_firstpage_header_titleTvId);
viewPager = (ViewPager)headerView.findViewById(R.id.fragment_firstpage_header_viewPagerId);
loadHeaderData();
}
//加载头视图的数据
private void loadHeaderData() {
String url =AppInterface.HEAR_VIEW_URL;
OkHttpUtils.get().url(url).build().execute(new ByteCallback() {
@Override
public void onError(Callcall, Exception e, int id) {
}
@Override
public void onResponse(byte[]response, int id) {
try {
String str = newString(response, "UTF-8");
final NewsEntitynewsEntity = JSON.parseObject(response, NewsEntity.class);
//添加第一张图片、也就是最后一张
ImageView img1 = newImageView(getContext());
img1.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(getContext()).load(Uri.parse(newsEntity.getData().get(3).getLitpic())).error(R.drawable.li_pic1).placeholder(R.drawable.li_pic1).config(Bitmap.Config.RGB_565).into(img1);
imageViewList.add(img1);
for (int i = 0; i< 4; i++) {
final int t = i;
ImageViewimageView = new ImageView(getContext());
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(getContext()).load(Uri.parse(newsEntity.getData().get(i).getLitpic())).error(R.drawable.li_pic1).placeholder(R.drawable.li_pic1).config(Bitmap.Config.RGB_565).into(imageView);
imageViewList.add(imageView);
}
//添加最后一张图片、也就是第一张
ImageView imageView1= new ImageView(getContext());
ImageView imageView1 = new ImageView(getContext());
imageView1.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(getContext()).load(Uri.parse(newsEntity.getData().get(0).getLitpic())).error(R.drawable.li_pic1).placeholder(R.drawable.li_pic1).config(Bitmap.Config.RGB_565).into(imageView1);
imageViewList.add(imageView1);
//给viewpager初始化适配器
viewpageradapter =new FirstPageViewPagerAdapter(imageViewList);
//设置适配器
viewPager.setAdapter(viewpageradapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public voidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public voidonPageSelected(int position) {
currentPosition = position;
int t =position % 4;
int temp =-1;
switch (t) {
case 0:
temp= 3;
break;
case 1:
temp= 0;
break;
case 2:
temp= 1;
break;
case 3:
temp= 2;
break;
}
for (int i =0; i < 4; i++) {
linearLayout.getChildAt(i).setSelected(i == temp);
if (i ==temp) {
titleTv.setText(newsEntity.getData().get(i).getTitle());
}
}
final int m =temp;
//viewpager设置点击事件
viewpageradapter.setListener(new FirstPageViewPagerAdapter.OnViewPagerItemClick(){
@Override
publicvoid response(View view, int position) {
Intent intent = new Intent(getActivity(), WebViewActivity.class);
intent.putExtra("id",Integer.parseInt(newsEntity.getData().get(m).getId()));
intent.putExtra("index", 2);
//startActivity(intent);
startActivityForResult(intent, 1);
}
});
}
@Override
public voidonPageScrollStateChanged(int state) {
switch(state) {
//拖拽的时候停止timer
caseViewPager.SCROLL_STATE_DRAGGING:
if(timer != null) {
timer.cancel();
timer = null;
}
break;
caseViewPager.SCROLL_STATE_IDLE:
if (currentPosition == 0) {
viewPager.setCurrentItem(4, false);
}else if (currentPosition == imageViewList.size() - 1) {
viewPager.setCurrentItem(1, false);
}
//viewpager停止时,重新开始播放
if(timer == null) {
startTimer();
}
break;
}
}
});
//默认第一页
viewPager.setCurrentItem(1);
linearLayout.getChildAt(0).setSelected(true);
//开始执行timer
startTimer();
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onActivityResult(intrequestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
switch (resultCode) {
case 6:
if (timer != null) {
timer.cancel();
timer = null;
}
viewPager.setCurrentItem(1);
startTimer();
break;
}
}
}
public void startTimer() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Message msg =Message.obtain();
msg.what = MSG_WHAT;
handler.sendEmptyMessage(msg.what);
}
}, 2000, 3000);
}
private void sleepThread() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2 * 1000);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run(){
ptrListView.onRefreshComplete();
}
});
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private void loadData(int p) {
String url = String.format(AppInterface.FIRSTPAGE_CONTENT_URL,p);
OkHttpUtils.get().url(url).build().execute(new ByteCallback() {
@Override
public void onError(Callcall, Exception e, int id) {
}
@Override
public void onResponse(byte[]response, int id) {
try {
String str = newString(response, "UTF-8");
NewsEntity newsEntity= JSON.parseObject(response, NewsEntity.class);
//Log.i("1606","kkkkk========="+newsEntity.getData().get(0).getTitle());
list =newsEntity.getData();
ptradapter.addAll(list);
//每次添加完清空列表、以免重复添加
list.clear();
} catch (UnsupportedEncodingExceptione) {
e.printStackTrace();
}
}
});
}
}