package com.example.a1.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.a1.Bean.Bean;
import com.example.a1.Bean.Bean02;
import com.example.a1.Bean.Bean03;
import com.example.a1.R;
import com.example.a1.View.XListView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by WYH on 2017/9/20.
*/
public class Fragment1 extends Fragment implements XListView.IXListViewListener{
private View view;
private ViewPager viewpager;
private XListView myxlistview;
private LinearLayout linearlayout;
List<ImageView> imglist;
List<Bean02> list;
List<Bean03> list02;
Handler handler = new Handler();
private List<Bean.DataBean.ComicsBean> comics;
private Myadapter myadapter;
private StringBuilder builder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment1,container,false);
return view;
}
Handler handler2=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i = viewpager.getCurrentItem() + 1;
viewpager.setCurrentItem(i);
handler2.sendEmptyMessageDelayed(0,3000);
}
};
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myxlistview = (XListView)view.findViewById(R.id.myxlistview);
myxlistview.setPullLoadEnable(true);
View vie = View.inflate(getActivity(), R.layout.viewpager, null);
myxlistview.addHeaderView(vie);
viewpager = (ViewPager)vie.findViewById(R.id.viewpager);
linearlayout = (LinearLayout)vie.findViewById(R.id.linearlayout);
new Myasynctask().execute("http://api.kkmh.com/v1/daily/comic_lists/0?since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3NzQyMjQwNjE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjQuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2VUYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjEzIiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6ImJpZ25veCIsIkZyb21Ib21lcGFnZVVwZGF0ZURhdGUiOjAsIiRzY3JlZW5faGVpZ2h0IjoxMjgwLCJIb21lcGFnZVVwZGF0ZURhdGUiOjAsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZSI6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6MTEsIiRzY3JlZW5fd2lkdGgiOjcyMCwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNoaW5hIE1vYmlsZSIsIiRtb2RlbCI6IlZQaG9uZSIsIiRhcHBfdmVyc2lvbiI6IjMuNi4yIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo2YWRkYzdhZTQ1MjUwMzY1Iiwib3JpZ2luYWxfaWQiOiJBOjZhZGRjN2FlNDUyNTAzNjUiLCJldmVudCI6IlJlYWRIb21lUGFnZSJ9");
myxlistview.setXListViewListener(this);
}
private void inindot() {
imglist = new ArrayList<>();
if (imglist != null) {
imglist.clear();
}
for (int i = 0; i < list02.size(); i++) {
ImageView iv = new ImageView(getActivity());
if (i == 0) {
iv.setImageResource(R.drawable.dot_focus);
} else {
iv.setImageResource(R.drawable.dot_normal);
}
imglist.add(iv);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
params.setMargins(10, 0, 10, 0);
linearlayout.addView(iv, params);
}
}
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
linearlayout.removeAllViews();
list.clear();
new Myasynctask().execute("http://api.kkmh.com/v1/daily/comic_lists/0?since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3NzQyMjQwNjE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjQuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2VUYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjEzIiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6ImJpZ25veCIsIkZyb21Ib21lcGFnZVVwZGF0ZURhdGUiOjAsIiRzY3JlZW5faGVpZ2h0IjoxMjgwLCJIb21lcGFnZVVwZGF0ZURhdGUiOjAsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZSI6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6MTEsIiRzY3JlZW5fd2lkdGgiOjcyMCwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNoaW5hIE1vYmlsZSIsIiRtb2RlbCI6IlZQaG9uZSIsIiRhcHBfdmVyc2lvbiI6IjMuNi4yIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo2YWRkYzdhZTQ1MjUwMzY1Iiwib3JpZ2luYWxfaWQiOiJBOjZhZGRjN2FlNDUyNTAzNjUiLCJldmVudCI6IlJlYWRIb21lUGFnZSJ9");
completeRefresh();
}
},2000);
}
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 0; i <comics.size() ; i++) {
Bean.DataBean.ComicsBean.TopicBean topic = comics.get(i).getTopic();
list.add(new Bean02(topic.getCover_image_url(),topic.getVertical_image_url(),topic.getTitle()));
}
myadapter.notifyDataSetChanged();
completeRefresh();
}
},2000);
}
public void completeRefresh(){
myxlistview.stopRefresh();
myxlistview.stopLoadMore();
myxlistview.setRefreshTime("刚刚");
}
class Myasynctask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try {
//获取url
URL url = new URL(strings[0]);
//请求网络
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//设置post请求
//urlConnection.setRequestMethod("POST");
int code = urlConnection.getResponseCode();
//判断是否返回成功
if(code==200){
//获取网络信息
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
builder = new StringBuilder();
String s = "";
//拼接
while((s=bf.readLine())!=null){
builder.append(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("TAG",""+s);
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
Bean.DataBean data = bean.getData();
comics = data.getComics();
list = new ArrayList<>();
for (int i = 0; i < comics.size() ; i++) {
Bean.DataBean.ComicsBean.TopicBean topic = comics.get(i).getTopic();
list.add(new Bean02(topic.getCover_image_url(),topic.getVertical_image_url(),topic.getTitle()));
}
myadapter = new Myadapter();
myxlistview.setAdapter(myadapter);
list02 = new ArrayList<>();
for (int j = 0; j < 5; j++) {
list02.add(new Bean03(comics.get(j).getCover_image_url()));
}
viewpager.setAdapter(new Myadapter2());
inindot();
handler2.sendEmptyMessageDelayed(0,3000);
viewpager.setCurrentItem(1000*imglist.size());
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for (int x = 0; x < imglist.size(); x++) {
if (position%imglist.size() == x) {
imglist.get(position%imglist.size()).setImageResource(R.drawable.dot_focus);
} else {
imglist.get(x).setImageResource(R.drawable.dot_normal);
}
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
class Myadapter2 extends PagerAdapter {
@Override
public int getCount() {
// return list02.size();
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(getActivity(), R.layout.vp, null);
ImageView imageview3 = (ImageView)view.findViewById(R.id.imageView3);
ImageLoader.getInstance().displayImage(list02.get(position%imglist.size()).getImg(),imageview3);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
class Myadapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class ViewHowder{
TextView tv;
ImageView img,img2;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Bean02 bean02 = list.get(i);
int type = getItemViewType(i);
ViewHowder howder = null;
if(type==0){
if(view==null){
howder=new ViewHowder();
view = View.inflate(getActivity(), R.layout.list1, null);
howder.tv = (TextView)view.findViewById(R.id.textView);
howder.img = view.findViewById(R.id.imageView);
howder.img2 = view.findViewById(R.id.imageView2);
view.setTag(howder);
}else{
howder = (ViewHowder) view.getTag();
}
howder.tv.setText(bean02.getTitle());
ImageLoader.getInstance().displayImage(bean02.getImg1(),howder.img);
ImageLoader.getInstance().displayImage(bean02.getImg2(),howder.img2);
}else{
if(view==null){
howder=new ViewHowder();
view = View.inflate(getActivity(), R.layout.list2, null);
howder.tv = (TextView)view.findViewById(R.id.textView);
howder.img = view.findViewById(R.id.imageView);
view.setTag(howder);
}else{
howder = (ViewHowder) view.getTag();
}
howder.tv.setText(bean02.getTitle());
ImageLoader.getInstance().displayImage(bean02.getImg1(),howder.img);
}
return view;
}
@Override
public int getItemViewType(int position) {
if(position%2==0){
return 0;
}else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
}
}
package com.example.a1.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.a1.Bean.Img;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import com.example.a1.R;
/**
* Created by 壹颗大金星 on 2017/9/15.
*/
public class Fragment2 extends Fragment{
private View view;
private ViewPager vp;
List<Img> list = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment2,container,false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
vp = (ViewPager)view.findViewById(R.id.vp);
list.add(new Img(R.drawable.a));
list.add(new Img(R.drawable.b));
list.add(new Img(R.drawable.c));
list.add(new Img(R.drawable.d));
list.add(new Img(R.drawable.e));
vp.setAdapter(new Myadapter2());
}
class Myadapter2 extends PagerAdapter {
@Override
public int getCount() {
return list.size();
//return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(getActivity(), R.layout.vp, null);
ImageView imageview3 = (ImageView)view.findViewById(R.id.imageView3);
imageview3.setImageResource(list.get(position).getImg());
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
package com.example.a1.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.a1.R;
/**
* Created by 壹颗大金星 on 2017/9/15.
*/
public class Fragment3 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment3,container,false);
return view;
}
}