Mainxml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dl" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="今日头条" android:textSize="28sp" android:gravity="center" /> </RelativeLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" > <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tab_layout" ></android.support.design.widget.TabLayout> </android.support.v4.view.ViewPager> </LinearLayout> <LinearLayout android:layout_width="300dp" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="start" android:background="#ff0" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
fragment1:
<?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" android:orientation="vertical" tools:context=".fragment.fragment1"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp"></android.support.v4.view.ViewPager> </LinearLayout>
fragment2:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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.fragment2"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout>
item_layout
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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.fragment2"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout>
mainactivity:
package com.example.zklx4; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import com.example.zklx4.fragment.fragment1; import com.example.zklx4.fragment.fragment2; import com.example.zklx4.myadapter.Fragmentadapter; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private TabLayout tab_layout; private ViewPager vp; private List<Fragment> fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 tab_layout = findViewById(R.id.tab_layout); vp = findViewById(R.id.vp); //创建一个集合 fragments = new ArrayList<>(); fragments.add(new fragment1()); fragments.add(new fragment2()); fragments.add(new fragment2()); fragments.add(new fragment2()); fragments.add(new fragment2()); //创建一个适配器 vp.setAdapter(new Fragmentadapter(getSupportFragmentManager(),fragments)); } }
fragment1:
package com.example.zklx4.fragment; import android.annotation.SuppressLint; 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.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.widget.ImageView; import com.example.zklx4.R; import com.example.zklx4.bean.user; import com.example.zklx4.myadapter.padapter; import com.example.zklx4.myadapter.vadapter; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class fragment1 extends Fragment { private ViewPager vp; @SuppressLint("HandlerLeak") /*private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==0){ String s = (String) msg.obj; Gson gson = new Gson(); user fromJson = gson.fromJson(s, user.class); List<user.ResultsBean> results = fromJson.getResults(); Log.i("bbb",results+""); //Log.i("aaa",results.get(0).getWho()); //vp.setAdapter(new padapter(getActivity(),results)); handler.sendEmptyMessageDelayed(1,2000); } else if (msg.what==1) { int currentItem = vp.getCurrentItem(); vp.setCurrentItem(currentItem+1); handler.sendEmptyMessageDelayed(1,2000); } } };*/ public fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_fragment1, container, false); vp = view.findViewById(R.id.vp); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); new Thread(){ @Override public void run() { super.run(); String path="http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=banner"; try { URL url = new URL(path); HttpURLConnection coon = (HttpURLConnection) url.openConnection(); coon.setConnectTimeout(5000); coon.setRequestMethod("GET"); if (coon.getResponseCode()==200){ InputStream inputStream = coon.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int len =-1; byte[] bytes = new byte[1024]; while((len=inputStream.read(bytes))!=-1){ byteArrayOutputStream.write(bytes,0,len); } String s2 = byteArrayOutputStream.toString(); String s = new String(s2); Log.i("aaa",s); /*Message message = new Message(); message.what=0; message.obj=s; handler.sendMessage(message);*/ Gson gson=new Gson(); user s1 = gson.fromJson("s", user.class); List<user.ResultsBean> results = s1.getResults(); vp.setAdapter(new padapter(getActivity(),results)); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
fragment2:
package com.example.zklx4.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.zklx4.R; /** * A simple {@link Fragment} subclass. */ public class fragment2 extends Fragment { public fragment2() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_fragment2, container, false); return view; } }
app:
package com.example.zklx4.app; import android.app.Application; import android.content.Context; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; public class App extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(getApplicationContext()).build(); ImageLoader.getInstance().init(build); } }
fragmentAdapter:
package com.example.zklx4.myadapter; import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.view.ViewGroup; import java.util.List; public class Fragmentadapter extends FragmentPagerAdapter { private List<Fragment> fragments; private String title[]={"推荐","社会","咨询","热点","头条"}; public Fragmentadapter(FragmentManager fm, List<Fragment> fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } @Override public CharSequence getPageTitle(int position) { return title[position]; } }
padapter:
package com.example.zklx4.myadapter; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.example.zklx4.bean.user; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; public class padapter extends PagerAdapter { private Context context; private List<user.ResultsBean> results; public padapter(Context context, List<user.ResultsBean> results) { this.context = context; this.results = results; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_XY); Log.i("aaaa","11111"); Log.i("aaa",results.get(1).getWho()); try{ ImageLoader.getInstance().displayImage(results.get(position % results.size()).getImages().get(0), imageView); }catch (Exception e){ e.getMessage(); } Log.i("aaa",imageView+""); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
vadapter:
package com.example.zklx4.myadapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import com.example.zklx4.R; import com.example.zklx4.bean.user; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; public class vadapter extends BaseAdapter { private Context context; private List<user.ResultsBean> results; public vadapter(Context context, List<user.ResultsBean> results) { this.context = context; this.results = results; } @Override public int getCount() { return results.size(); } @Override public Object getItem(int i) { return results.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { holder holder = null; if (view==null){ holder = new holder(); view=View.inflate(context, R.layout.item_layout,null); holder.imag_view=view.findViewById(R.id.imag_view); view.setTag(holder); }else{ holder= (vadapter.holder) view.getTag(); } ImageLoader.getInstance().displayImage(results.get(i).getImages().get(0),holder.imag_view); return view; } class holder{ ImageView imag_view; } }