用HorizontalScrollView布局
<?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.ningac.ningancheng20170918.MainActivity"> <HorizontalScrollView android:id="@+id/hori" android:background="@color/hong" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="今日头条" android:textSize="20dp" android:textColor="@color/bai" android:layout_width="160dp" android:gravity="center" android:textStyle="bold" android:layout_height="match_parent" /> <EditText android:hint="马云汽车|全新哈佛|岳云鹏斗地主" android:background="@color/bai" android:textSize="15dp" android:layout_width="200dp" android:layout_height="30dp" /> </LinearLayout> </HorizontalScrollView> <RadioGroup android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/rg" android:orientation="horizontal" android:layout_alignParentBottom="true" > <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/rb1" android:layout_weight="1" android:text="首页" android:gravity="center" android:button="@null" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/rb2" android:layout_weight="1" android:text="西瓜视频" android:gravity="center" android:button="@null" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/rb3" android:layout_weight="1" android:text="微头条" android:gravity="center" android:button="@null" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/rb4" android:layout_weight="1" android:text="我的" android:gravity="center" android:button="@null" /> </RadioGroup> <FrameLayout android:layout_above="@+id/rg" android:layout_below="@+id/hori" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/frame" ></FrameLayout> </RelativeLayout> ---------------------------------------------------------------------- MainActivity在项目Application中完成universal-image-loader的初始化,并且设置色彩模式为RGB565
package com.example.ningac.ningancheng20170918; import android.graphics.Bitmap; import android.graphics.Color; import android.support.annotation.IdRes; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; public class MainActivity extends AppCompatActivity {//主页面 private FrameLayout frame; private RadioButton rb1; private RadioButton rb2; private RadioButton rb3; private RadioButton rb4; private RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayImageOptions build = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true).build();//设置色彩模式 ImageLoaderConfiguration build1 = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(build).build(); ImageLoader.getInstance().init(build1);//设置图片格式 frame = (FrameLayout) findViewById(R.id.frame); rb1 = (RadioButton) findViewById(R.id.rb1); rb2 = (RadioButton) findViewById(R.id.rb2); rb3 = (RadioButton) findViewById(R.id.rb3); rb4 = (RadioButton) findViewById(R.id.rb4); rg = (RadioGroup) findViewById(R.id.rg); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {//控件监听 if(rb1.isChecked()){ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = manager.beginTransaction(); fragmentTransaction.replace(R.id.frame,new Two()); fragmentTransaction.commit(); rb1.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb4.setTextColor(Color.BLACK); }else if(rb2.isChecked()){ One one = new One(); Bundle bundle = new Bundle(); bundle.putString("data","页面二"); one.setArguments(bundle); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = manager.beginTransaction(); fragmentTransaction.replace(R.id.frame,one); fragmentTransaction.commit(); rb2.setTextColor(Color.RED); rb1.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb4.setTextColor(Color.BLACK); }else if(rb3.isChecked()){ One one = new One(); Bundle bundle = new Bundle(); bundle.putString("data","页面三"); one.setArguments(bundle); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = manager.beginTransaction(); fragmentTransaction.replace(R.id.frame,one); fragmentTransaction.commit(); rb3.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb1.setTextColor(Color.BLACK); rb4.setTextColor(Color.BLACK); }else if(rb4.isChecked()){ One one = new One(); Bundle bundle = new Bundle(); bundle.putString("data","页面四"); one.setArguments(bundle); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = manager.beginTransaction(); fragmentTransaction.replace(R.id.frame,one); fragmentTransaction.commit(); rb4.setTextColor(Color.RED); rb2.setTextColor(Color.BLACK); rb3.setTextColor(Color.BLACK); rb1.setTextColor(Color.BLACK); } } }); rb1.setChecked(true);//设置默认选择 } }-------------------------------------------------------------------------
Fragment布局页面一package com.example.ningac.ningancheng20170918; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by Ning.A.C on 2017/9/18. */ public class One extends Fragment{//fragment @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setText(getArguments().getString("data")); textView.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); return textView;//设置视图 } }----------------------------------------------------
Fragment布局页面二package com.example.ningac.ningancheng20170918; 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.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TableLayout; /** * Created by Ning.A.C on 2017/9/18. */ public class Two extends Fragment{//fragment private View view; private TabLayout tab; private ViewPager vp; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.item, null); tab = (TabLayout) view.findViewById(R.id.tab); vp = (ViewPager) view.findViewById(R.id.vp); return view;//设置视图 } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { vp.setAdapter(new FragmentPagerAdapter(getFragmentManager()) { @Override public CharSequence getPageTitle(int position) { if(position==0){ return "关注"; }else if(position==1){ return "推荐"; }else if(position==2){ return "热点"; }else if(position==3){ return "科技"; }else if(position==4){ return "视频"; }else if(position==5){ return "数码"; }else if(position==6){ return "汽车"; }else if(position==7){ return "军事"; } return "娱乐";//设置标题 } @Override public Fragment getItem(int position) { return new Three();//设置fragment } @Override public int getCount() { return 9; } }); tab.setupWithViewPager(vp);//设置viewpage关联 super.onActivityCreated(savedInstanceState); } }其中用到的布局<?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" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tex" android:layout_width="25dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@color/bai" android:text="+" /> <android.support.design.widget.TabLayout app:tabGravity="center" app:tabMode="scrollable" app:tabSelectedTextColor="@color/hong" app:tabTextColor="@color/hei" android:id="@+id/tab" app:tabIndicatorHeight="0dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/tex" ></android.support.design.widget.TabLayout> </RelativeLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" ></android.support.v4.view.ViewPager> </LinearLayout>-------------------------------------------------------------------------------------
页面三package com.example.ningac.ningancheng20170918; 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.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import com.jwenfeng.library.pulltorefresh.BaseRefreshListener; import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout; 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; /** *作者:张金玲 *编辑日期:2017/9/19 *更改日期:2017/9/19 */ public class Three extends Fragment{//fragment页面 View view; private PullToRefreshLayout pull; private ListView list; private Secce secce; private int page=1; private Myadapter myadapter; private boolean ass=true; private Handler han=new Handler(){ @Override public void handleMessage(Message msg) {//Handler内容 if(msg.what==1){ if(ass){ myadapter = new Myadapter(); list.setAdapter(myadapter);//设置adapter ass=false; }else{ myadapter.notifyDataSetChanged();//刷新适配器 } //刷新成功 }else if(msg.what==3){ MyTask myTask = new MyTask(); myTask.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=");//请求数据 pull.finishLoadMore(); pull.finishRefresh(); } super.handleMessage(msg); } }; class Myadapter extends BaseAdapter{ @Override public int getCount() { return secce.getData().size();//设置数据长度 } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { Con con; if(convertView==null){ convertView=View.inflate(getActivity(),R.layout.item3,null); con=new Con(); con.title = (TextView) convertView.findViewById(R.id.title); con.introduction = (TextView) convertView.findViewById(R.id.in); con.img= (ImageView) convertView.findViewById(R.id.img); convertView.setTag(con); }else{ con = (Con) convertView.getTag(); } con.introduction.setText(secce.getData().get(position).getIntroduction()); con.title.setText(secce.getData().get(position).getTitle()); ImageLoader.getInstance().displayImage(secce.getData().get(position).getImg(),con.img); return convertView;//优化显示视图 } class Con{ TextView title,introduction; ImageView img; } } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.item2,null); pull = (PullToRefreshLayout) view.findViewById(R.id.pull); list = (ListView) view.findViewById(R.id.list); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { final MyTask task = new MyTask(); task.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="); pull.setRefreshListener(new BaseRefreshListener() { @Override public void refresh() { page=1; new Handler().postDelayed(new Thread(){ @Override public void run() { Message message = new Message(); message.what=3; han.sendMessage(message); super.run(); } },2000); } @Override public void loadMore() { page++; new Handler().postDelayed(new Thread(){ @Override public void run() { Message message = new Message(); message.what=3; han.sendMessage(message); super.run(); } },2000); } }); super.onActivityCreated(savedInstanceState); } @Override public void onResume() { final MyTask task = new MyTask(); task.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+page); super.onResume(); } class MyTask extends AsyncTask<String,Integer,String>{ @Override protected void onPostExecute(String s) { super.onPostExecute(s); } @Override protected String doInBackground(String... params) {//请求数据 URL url = null; try { url = new URL(params[0]+page); HttpURLConnection cton = (HttpURLConnection) url.openConnection(); cton.setRequestMethod("GET"); cton.setReadTimeout(5000); cton.setConnectTimeout(5000); if(cton.getResponseCode()==200){ InputStream stream = cton.getInputStream(); String json = getJson(stream, "utf-8"); Gson gson = new Gson(); secce = gson.fromJson(json, Secce.class); Message message = new Message(); message.what=1; han.sendMessage(message);//返回handler } } catch (Exception e) { e.printStackTrace(); } return null; } public String getJson(InputStream stream, String s){ String a=""; String b; try { BufferedReader reader = new BufferedReader(new InputStreamReader(stream, s)); while((b=reader.readLine())!=null){ a=a+b;//解析json串 } } catch (Exception e) { e.printStackTrace(); } return a; } } @Override public void onDestroyView() { ass=true; super.onDestroyView(); } } 其中的布局---------------------------------------------------------<?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" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/in" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </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"> <com.jwenfeng.library.pulltorefresh.PullToRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pull" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </com.jwenfeng.library.pulltorefresh.PullToRefreshLayout> </LinearLayout>