Fragment1中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"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp" android:layout_above="@+id/rg" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:id="@+id/dot_layout" android:layout_marginTop="170dp" android:layout_centerHorizontal="true" android:layout_height="wrap_content"> </LinearLayout> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/vp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> </ListView> </RelativeLayout> Fragment2中XML====
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:text="我的频道" android:gravity="center_vertical" android:padding="10dp" /> <GridView android:layout_width="match_parent" android:layout_height="187dp" android:id="@+id/gv1" android:numColumns="5" > </GridView> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:text="更多频道" android:gravity="center_vertical" android:padding="10dp" /> <GridView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/gv2" android:numColumns="5" > </GridView> </LinearLayout>
Fragment1的class=====
package com.example.jiajiajia.yuekaolianxi.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.app.FragmentPagerAdapter; 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.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.example.jiajiajia.yuekaolianxi.R; import com.example.jiajiajia.yuekaolianxi.bean.Imager; import com.example.jiajiajia.yuekaolianxi.bean.NewsData; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * 姓名:jiajiajia * 时间:2017/8/23. */ public class Fragment1 extends Fragment { ViewPager vp; LinearLayout dot_layou; ListView lv; Imager img; NewsData data; DisplayImageOptions dis; int currposition; List<ImageView> list_doc; private MyPagerAdapter adapter; private MyBaseAdapter adapter1; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { currposition++; vp.setCurrentItem(currposition); handler.sendEmptyMessageDelayed(0,2000); } }; private View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getActivity(), R.layout.fragment1,null); //获取控件 vp= view.findViewById(R.id.vp); dot_layou= view.findViewById(R.id.dot_layout); lv= view.findViewById(R.id.lv); initImg(); initData(); adapter = new MyPagerAdapter(); adapter1 = new MyBaseAdapter(); handler.sendEmptyMessageDelayed(0,2000); return view; } private void initData() { new AsyncTask<String,String,String>(){ @Override protected String doInBackground(String... strings) { return getData2(); } @Override protected void onPostExecute(String s) { Gson gson=new Gson(); data=gson.fromJson(s,NewsData.class); lv.setAdapter(adapter1); super.onPostExecute(s); } }.execute(); } private void initImg() { new AsyncTask<String,String,String>(){ @Override protected String doInBackground(String... strings) { return getData(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); img=gson.fromJson(s,Imager.class); vp.setAdapter(adapter); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { int index = position % list_doc.size(); for (int i = 0; i < list_doc.size(); i++) { list_doc.get(i).setBackgroundResource(R.drawable.dot_normal); } list_doc.get(index).setBackgroundResource(R.drawable.dot_selectl); } @Override public void onPageScrollStateChanged(int state) { } }); initDoc(); } }.execute(); } private String getData() { String result=""; try { URL url=new URL("http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=banner"); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int code=connection.getResponseCode(); if(code==200) { InputStream is=connection.getInputStream(); ByteArrayOutputStream bos=new ByteArrayOutputStream(); int len=-1; byte[] b=new byte[1024]; while((len=is.read(b))!=-1){ bos.write(b,0,len); } result=new String(bos.toByteArray()); } } catch (Exception e) { e.printStackTrace(); } return result; } private String getData2() { String result=""; try { URL url=new URL("http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=one"); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int code=connection.getResponseCode(); if(code==200) { InputStream is=connection.getInputStream(); ByteArrayOutputStream bos=new ByteArrayOutputStream(); int len=-1; byte[] b=new byte[1024]; while((len=is.read(b))!=-1){ bos.write(b,0,len); } result=new String(bos.toByteArray()); } } catch (Exception e) { e.printStackTrace(); } return result; } class MyPagerAdapter extends PagerAdapter{ @Override public int getCount() { if(img!=null) { return Integer.MAX_VALUE; } return 0; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView iv=new ImageView(getActivity()); iv.setScaleType(ImageView.ScaleType.CENTER_CROP); ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(getActivity()); ImageLoader.getInstance().init(configuration); DisplayImageOptions options=new DisplayImageOptions.Builder().cacheInMemory(true).build(); ImageLoader.getInstance().displayImage(img.getBanner().get(position%img.getBanner().size()).getImage_url(),iv,options); container.addView(iv); return iv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } class MyBaseAdapter extends BaseAdapter{ @Override public int getCount() { if(data!=null) { return data.getData().size(); } return 0; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder=null; if(view==null) { view=View.inflate(getActivity(),R.layout.item,null); holder=new ViewHolder(); holder.tv_title=view.findViewById(R.id.item_title); holder.item_img=view.findViewById(R.id.item_img); view.setTag(holder); } else { holder= (ViewHolder) view.getTag(); } holder.tv_title.setText(data.getData().get(i).getTitle()); ImageLoader.getInstance().displayImage(data.getData().get(i).getImage_url(),holder.item_img); return view; } class ViewHolder{ TextView tv_title; ImageView item_img; } } public void initDoc(){ list_doc=new ArrayList<>(); if(img!=null) { for (int i = 0; i <img.getBanner().size() ; i++) { ImageView iv2=new ImageView(getActivity()); LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20,20); params.setMargins(10,5,10,5); if(i == 0){ iv2.setBackgroundResource(R.drawable.dot_selectl); }else{ iv2.setBackgroundResource(R.drawable.dot_normal); } dot_layou.addView(iv2,params); list_doc.add(iv2); } } } } ====Fragment2的class===
package com.example.jiajiajia.yuekaolianxi.Fragment; import android.os.AsyncTask; 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 android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.TextView; import com.example.jiajiajia.yuekaolianxi.R; import com.example.jiajiajia.yuekaolianxi.adapter.MyGridAdapter; import com.example.jiajiajia.yuekaolianxi.bean.PinDaoData; import com.google.gson.Gson; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class Fragment2 extends Fragment { GridView gv1,gv2; PinDaoData data; List<String> listMe; List<String> listMore; private MyGridAdapter adapter1; private MyGridAdapter adapter2; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=View.inflate(getActivity(), R.layout.fragment2,null); gv1=view.findViewById(R.id.gv1); gv2=view.findViewById(R.id.gv2); adapter1 = new MyGridAdapter(listMe,getActivity()); adapter2 = new MyGridAdapter(listMore,getActivity()); gv1.setAdapter(adapter1); gv2.setAdapter(adapter2); gv1.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { listMore.add(listMe.get(i)); listMe.remove(i); adapter1.setData(listMe); adapter2.setData(listMore); } }); gv2.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { listMe.add(listMore.get(i)); listMore.remove(i); adapter1.setData(listMe); adapter2.setData(listMore); } }); initData(); return view; } private void initData() { new AsyncTask<String,String,String>(){ @Override protected String doInBackground(String... strings) { return getData(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); data=gson.fromJson(s,PinDaoData.class); listMe=new ArrayList<String>(); listMore=new ArrayList<String>(); for (int i = 0; i <data.getChannel().size() ; i++) { listMe.add(data.getChannel().get(i).getChannel_me()); listMore.add(data.getChannel().get(i).getChannel_more()); } adapter1.setData(listMe); adapter2.setData(listMore); } }.execute(); } private String getData() { String result=""; try { URL url=new URL("http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=channel"); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int code=connection.getResponseCode(); if(code==200) { InputStream is=connection.getInputStream(); ByteArrayOutputStream bos=new ByteArrayOutputStream(); int len=-1; byte[] b=new byte[1024]; while((len=is.read(b))!=-1){ bos.write(b,0,len); } result=new String(bos.toByteArray()); } } catch (Exception e) { e.printStackTrace(); } return result; } }
==Fragment2的适配器==
package com.example.jiajiajia.yuekaolianxi.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.jiajiajia.yuekaolianxi.R; import java.util.List; /** * 姓名:jiajiajia * 时间:2017/8/24. */ public class MyGridAdapter extends BaseAdapter{ List<String> list; Context context; public MyGridAdapter(List<String> list, Context context) { super(); this.list = list; this.context = context; } @Override public int getCount() { if(list!=null) { return list.size(); } return 0; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder2 holder2; if(view==null) { view=View.inflate(context, R.layout.grid_item,null); holder2=new ViewHolder2(); holder2.tv=view.findViewById(R.id.grid_tv); view.setTag(holder2); } else{ holder2= (ViewHolder2) view.getTag(); } holder2.tv.setText(list.get(i)); return view; } class ViewHolder2{ TextView tv; } public void setData(List<String> list){ this.list=list; notifyDataSetChanged(); } }