//依赖
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
1.xlm
<com.astuetz.PagerSlidingTabStrip android:id="@+id/pstp" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="40dp"></com.astuetz.PagerSlidingTabStrip> <android.support.v4.view.ViewPager android:id="@+id/vip" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager>
2.MainActivty
String[] title={"推荐","野史","后宫","解密"}; private ViewPager vip; private PagerSlidingTabStrip pst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化id vip = (ViewPager) findViewById(R.id.vip); pst = (PagerSlidingTabStrip) findViewById(R.id.pstp); Adapter adapter = new Adapter(getSupportFragmentManager());
//如果要是在Fragment里面做的话,那么需要
getActivity().getSupportFragmentManager()
就行啦
//关联 vip.setAdapter(adapter); pst.setViewPager(vip); } // 适配器 class Adapter extends FragmentPagerAdapter{ public Adapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return title[position]; } @Override public Fragment getItem(int position) { Fragment fra=null; switch (position){ case 0: fra=new Fragment1(); break; case 1: fra=new Fragment2(); break; case 2: fra=new Fragment3(); break; case 3: fra=new Fragment4(); break; } return fra; } @Override public int getCount() { return title.length; } } }
3.fragmenty 以及里面的xlistview
// 网址 String path="http://169.254.56.108:8080/aa.json"; private XListView xlv; private List<Bean.ArrayBean> array; private MyAdapter myAdapter; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); myAdapter.notifyDataSetChanged(); xlv.stopRefresh(); xlv.stopLoadMore(); } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_fragment1, container, false); xlv = (XListView) view.findViewById(R.id.xlv); indata(); xlv.setPullRefreshEnable(true); xlv.setPullLoadEnable(true); xlv.setXListViewListener(this); // 点击传值 xlv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(getActivity(),MainActivity2.class); intent.putExtra("tyte",array.get(position-1).getTitle()); intent.putExtra("ty",array.get(position-1).getContent()); startActivity(intent); return false; } }); return view; } // 刷新 @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { array.add(0,array.get(4)); } },2000); handler.sendEmptyMessage(0); } //加载 @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { array.add(array.get(4)); } },2000); handler.sendEmptyMessage(0); } // 异步加载请求数据 private void indata(){ new AsyncTask<String,Void,String>(){ private ByteArrayOutputStream outputStream; @Override protected String doInBackground(String... params) { try { URL url = new URL(params[0]); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(3000); int responseCode = connection.getResponseCode(); if(responseCode==200){ InputStream inputStream = connection.getInputStream(); outputStream = new ByteArrayOutputStream(); int len; byte[] by=new byte[1024]; while ((len=inputStream.read(by))!=-1){ outputStream.write(by,0,len); } outputStream.close(); inputStream.close(); } } catch (Exception e) { e.printStackTrace(); } return outputStream.toString(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean fromJson = gson.fromJson(s, Bean.class); array = fromJson.getArray(); myAdapter = new MyAdapter(); xlv.setAdapter(myAdapter); } }.execute(path); } //xlistview适配器 class MyAdapter extends BaseAdapter{ @Override public int getCount() { return array.size(); } @Override public Object getItem(int position) { return array.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView=View.inflate(getActivity(),R.layout.item,null); TextView text= (TextView) convertView.findViewById(R.id.text); TextView text2= (TextView) convertView.findViewById(R.id.text2); TextView text3= (TextView) convertView.findViewById(R.id.text3); text.setText(array.get(position).getTitle()); text2.setText(array.get(position).getType()); text3.setText(array.get(position).getNumber()); return convertView; } } }
4.fragment.xml
<com.bawei.util.XListView android:id="@+id/xlv" android:layout_width="wrap_content" android:layout_height="wrap_content"></com.bawei.util.XListView>
5.item.xml
<LinearLayout android:layout_width="300dp" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="20sp" android:text="场表U盾逼的那次哦啊"/> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:text="野史密文"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:orientation="vertical"> <ImageView android:layout_width="50dp" android:layout_height="40dp" android:layout_marginLeft="30dp" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="18dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击数:"/> <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="前"/> </LinearLayout> </LinearLayout>6.MainAcitvty2接受值protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); String tyte = intent.getStringExtra("tyte"); String ty = intent.getStringExtra("ty"); TextView tv= (TextView) findViewById(R.id.tv); TextView ttv= (TextView) findViewById(R.id.ttv); tv.setText(tyte); ttv.setText(ty); }6.MainActvty的布局<TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="40dp" android:textSize="30sp" android:text="欢迎进入"/> <TextView android:id="@+id/ttv" android:layout_width="match_parent" android:layout_height="match_parent" android:text="数据"/>