1.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.lxwo.MainActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="首页"
android:onClick="b1"
android:textSize="18dp"
/>
<Button
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="想法"
android:onClick="b2"
android:textSize="18dp"
/>
<Button
android:id="@+id/b3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="市场"
android:onClick="b3"
android:textSize="18dp"
/>
<Button
android:id="@+id/b4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="通知"
android:onClick="b4"
android:textSize="18dp"
/>
<Button
android:id="@+id/b5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="更多"
android:onClick="b5"
android:textSize="18dp"
/>
</LinearLayout>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ll"
></FrameLayout>
</RelativeLayout>
2
<?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" xmlns:app="http://schemas.android.com/apk/res-auto"> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="羞羞的铁拳 开心麻花" android:layout_centerHorizontal="true" /> <android.support.design.widget.TabLayout android:id="@+id/tl" android:layout_width="wrap_content" android:layout_height="50dp" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabTextColor="@color/colorPrimary" android:layout_below="@+id/et" android:layout_centerHorizontal="true" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tl" ></android.support.v4.view.ViewPager> </RelativeLayout>3
6<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"></TextView> </LinearLayout>4<?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"> </RelativeLayout>5<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5"/> </LinearLayout>
7
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.lxwo.view.XListView android:id="@+id/xls" android:layout_width="match_parent" android:layout_height="match_parent"></com.example.lxwo.view.XListView> </LinearLayout>89<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text4"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ty"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/th"/> </LinearLayout>10
package com.example.lxwo; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.example.lxwo.Fragment_s.Fragment1; import com.example.lxwo.Fragment_s.Fragment2; import com.example.lxwo.Fragment_s.Fragment3; import com.example.lxwo.Fragment_s.Fragment4; import com.example.lxwo.Fragment_s.Fragment5; import com.example.lxwo.Fragment_s.Gls.Gj; public class MainActivity extends AppCompatActivity { private Button b1; private Button b2; private Button b3; private Button b4; private Button b5; private Fragment fragment1; private Fragment5 fragment5; private Fragment2 fragment2; private Fragment3 fragment3; private Fragment4 fragment4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(MainActivity.this,"您现在的网络状态是"+ Gj.getNetype(MainActivity.this),Toast.LENGTH_SHORT).show(); intview(); initFragment(); } private void initFragment() { fragment1 = new Fragment1(); fragment2 = new Fragment2(); fragment3 = new Fragment3(); fragment4 = new Fragment4(); fragment5 = new Fragment5(); getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment1).commit(); getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment2).commit(); getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment3).commit(); getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment4).commit(); getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment5).commit(); getSupportFragmentManager().beginTransaction().hide(fragment1).commit(); getSupportFragmentManager().beginTransaction().hide(fragment2).commit(); getSupportFragmentManager().beginTransaction().hide(fragment3).commit(); getSupportFragmentManager().beginTransaction().hide(fragment4).commit(); getSupportFragmentManager().beginTransaction().hide(fragment5).commit(); getSupportFragmentManager().beginTransaction().show(fragment1).commit(); } private void intview() { b1 = (Button) findViewById(R.id.b1); b2 = (Button) findViewById(R.id.b2); b3 = (Button) findViewById(R.id.b3); b4 = (Button) findViewById(R.id.b4); b5 = (Button) findViewById(R.id.b5); b1.setTextColor(Color.RED); } public void b1(View view){ b1.setTextColor(Color.RED); b2.setTextColor(Color.BLACK); b3.setTextColor(Color.BLACK); b4.setTextColor(Color.BLACK); b5.setTextColor(Color.BLACK); getSupportFragmentManager().beginTransaction().show(fragment1).commit(); getSupportFragmentManager().beginTransaction().hide(fragment2).commit(); getSupportFragmentManager().beginTransaction().hide(fragment3).commit(); getSupportFragmentManager().beginTransaction().hide(fragment4).commit(); getSupportFragmentManager().beginTransaction().hide(fragment5).commit(); } public void b2(View view){ b1.setTextColor(Color.BLACK); b2.setTextColor(Color.RED); b3.setTextColor(Color.BLACK); b4.setTextColor(Color.BLACK); b5.setTextColor(Color.BLACK); getSupportFragmentManager().beginTransaction().hide(fragment1).commit(); getSupportFragmentManager().beginTransaction().show(fragment2).commit(); getSupportFragmentManager().beginTransaction().hide(fragment3).commit(); getSupportFragmentManager().beginTransaction().hide(fragment4).commit(); getSupportFragmentManager().beginTransaction().hide(fragment5).commit(); } public void b3(View view){ b1.setTextColor(Color.BLACK); b2.setTextColor(Color.BLACK); b3.setTextColor(Color.RED); b4.setTextColor(Color.BLACK); b5.setTextColor(Color.BLACK); getSupportFragmentManager().beginTransaction().hide(fragment1).commit(); getSupportFragmentManager().beginTransaction().hide(fragment2).commit(); getSupportFragmentManager().beginTransaction().show(fragment3).commit(); getSupportFragmentManager().beginTransaction().hide(fragment4).commit(); getSupportFragmentManager().beginTransaction().hide(fragment5).commit(); } public void b4(View view){ b1.setTextColor(Color.BLACK); b2.setTextColor(Color.BLACK); b3.setTextColor(Color.BLACK); b4.setTextColor(Color.RED); b5.setTextColor(Color.BLACK); getSupportFragmentManager().beginTransaction().hide(fragment1).commit(); getSupportFragmentManager().beginTransaction().hide(fragment2).commit(); getSupportFragmentManager().beginTransaction().hide(fragment3).commit(); getSupportFragmentManager().beginTransaction().show(fragment4).commit(); getSupportFragmentManager().beginTransaction().hide(fragment5).commit(); } public void b5(View view){ b1.setTextColor(Color.BLACK); b2.setTextColor(Color.BLACK); b3.setTextColor(Color.BLACK); b4.setTextColor(Color.BLACK); b5.setTextColor(Color.RED); getSupportFragmentManager().beginTransaction().hide(fragment1).commit(); getSupportFragmentManager().beginTransaction().hide(fragment2).commit(); getSupportFragmentManager().beginTransaction().hide(fragment3).commit(); getSupportFragmentManager().beginTransaction().hide(fragment4).commit(); getSupportFragmentManager().beginTransaction().show(fragment5).commit(); } }11
package com.example.lxwo.Fragment_s; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.lxwo.R; import java.util.ArrayList; import java.util.List; /** * author:Created by WangZhiQiang on 2017/10/17. */ public class Fragment1 extends Fragment{ private View view; private TabLayout tl; private ViewPager vp; private List<String> list = new ArrayList<>(); private List<Fragment> flist= new ArrayList<>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.f1,container,false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); tl = (TabLayout) view.findViewById(R.id.tl); vp = (ViewPager) view.findViewById(R.id.vp); //集合添加数据 list.add("动态"); list.add("热门"); list.add("发现"); //fragment集合 flist.add(new Fragment_dt()); flist.add(new Frm()); flist.add(new Fragment_fx()); //适配器 vp.setAdapter(new MAdapter(getActivity().getSupportFragmentManager())); tl.setupWithViewPager(vp); } class MAdapter extends FragmentPagerAdapter { public MAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return flist.get(position); } @Override public int getCount() { return flist.size(); } @Override public CharSequence getPageTitle(int position) { return list.get(position); } } }f2 f3 f4
viewpage的三个fragment
package com.example.lxwo.Fragment_s; 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.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.lxwo.Beans; import com.example.lxwo.R; import com.example.lxwo.view.XListView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.DisplayImageOptions; 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; /** * author:Created by WangZhiQiang on 2017/10/17. */ public class Frm extends Fragment implements XListView.IXListViewListener { int n; private View view; private XListView xls; private DisplayImageOptions onptions; private Madpter madpter; private List<Beans.ResultsBean> list=new ArrayList<>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fram_xl, container, false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); xls = (XListView) view.findViewById(R.id.xls); xls.setPullLoadEnable(true); xls.setXListViewListener(this); onptions = new DisplayImageOptions.Builder().cacheOnDisk(true) .cacheInMemory(true) .build(); madpter = new Madpter(); xls.setAdapter(madpter); new MAsyncTask().execute("http://gank.io/api/data/Android/10/1"); } public void onLoad(){ xls.stopRefresh(); xls.stopLoadMore(); xls.setRefreshTime("刚刚"); } @Override public void onRefresh() { list.clear(); new MAsyncTask().execute("http://gank.io/api/data/Android/10/1"); onLoad(); } @Override public void onLoadMore() { n++; new MAsyncTask().execute("http://gank.io/api/data/Android/10/1"+n); onLoad(); } class MAsyncTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... strings) { URL url = null; try { url = new URL(strings[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); int code = connection.getResponseCode(); if (code == 200) { InputStream inputStream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer buffer = new StringBuffer(); String str = ""; StringBuffer stringBuffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { stringBuffer.append(str); } return stringBuffer.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Beans beans = gson.fromJson(s, Beans.class); List<Beans.ResultsBean> results = beans.getResults(); list.addAll(results); madpter.notifyDataSetChanged(); } } class Madpter 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 ViewHolder1{ TextView desc; } class ViewHolder2{ TextView desc; ImageView img; } public View getView(int i, View view, ViewGroup viewGroup) { int type = getItemViewType(i); switch(type){ case 0: ViewHolder1 vh1; if (view == null){ view = View.inflate(getActivity(),R.layout.item1,null); vh1 = new ViewHolder1(); vh1.desc =(TextView) view.findViewById(R.id.text4); view.setTag(vh1); }else{ vh1 = (ViewHolder1) view.getTag(); } vh1.desc.setText(list.get(i).getDesc()); break; case 1: ViewHolder2 vh2; if (view == null){ view = View.inflate(getActivity(),R.layout.item2,null); vh2 = new ViewHolder2(); vh2.desc =(TextView) view.findViewById(R.id.ty); vh2.img =(ImageView) view.findViewById(R.id.th); view.setTag(vh2); }else{ vh2 = (ViewHolder2) view.getTag(); } vh2.desc.setText(list.get(i).getDesc()); ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),vh2.img,onptions); break; } return view; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { if (list.get(position).getImages()==null){ return 0; }else{ return 1; } } } }bean类
导报tablayout和权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />