1:布局文件
activity_main.xml
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/include" android:layout_width="match_parent" android:layout_height="50dp" layout="@layout/includ"> </include> <HorizontalScrollView android:id="@+id/horizontalscrollView" android:layout_width="match_parent" android:layout_height="50dp" android:scrollbars="none" > <LinearLayout android:id="@+id/ll_titles" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> </HorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> </LinearLayout>fragment.xml布局
<?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"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>includ.xml布局
<?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:id="@+id/texttitle" android:layout_width="match_parent" android:layout_height="60dp" android:text="今日头条" android:textSize="36sp" android:gravity="center" android:background="#df1414"/> </LinearLayout>list_item.xml布局
<?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="horizontal"> <LinearLayout android:layout_weight="3" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="山西" android:textSize="36sp"/> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" android:textSize="16sp"/> </LinearLayout> <ImageView android:id="@+id/image" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> </LinearLayout>title_item.xml布局
<?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/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="26sp" android:text="呵呵" android:gravity="center_horizontal"/> <TextView android:id="@+id/tv_indicator" android:layout_width="match_parent" android:layout_height="3dp" android:background="#ff0000"/> </LinearLayout>2:MainActivity
package com.example.a1.zhaoshan20170410; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.view.View; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.TextView; import fragment.NewsFragment; public class MainActivity extends FragmentActivity implements View.OnClickListener { private LinearLayout mLltitles; private ViewPager mViewPager; private HorizontalScrollView mScrollView; //定义要展示的内容 private String[]titleArray = new String[]{"推荐","热点","视频","北京","社会", "图片","科技","汽车","体育","头条"}; //把URL的值写入集合 private String[]typeArray =new String[]{"gj","ss","cj","kj", "js","ty","yl","gn","shehui","tt"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initview(); initViewpagerIndicator(); initviewpager(); } private void initviewpager() { mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { //获取fragmen并传参数 return NewsFragment.getInstance(typeArray[position]); } @Override public int getCount() { return titleArray.length; } }); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //让顶部指示器跟着动 for(int i=0;i<mLltitles.getChildCount();i++){ View childAt = mLltitles.getChildAt(i); TextView tv_title = (TextView) childAt.findViewById(R.id.tv_title); TextView tv_indicator= (TextView) childAt.findViewById(R.id.tv_indicator); if(i==position){ //切换颜色 tv_indicator.setVisibility(View.VISIBLE); tv_title.setTextColor(Color.RED); //点击条目时,viewpager切换页面 mViewPager.setCurrentItem(i); }else { tv_indicator.setVisibility(View.GONE); tv_title.setTextColor(Color.BLACK); } } //拿到当前条目的位置,让HorizontalScrollView跟着动, mLltitles.scrollTo((int)mLltitles.getChildAt(position).getX(), 0); } @Override public void onPageScrollStateChanged(int state) { } }); } private void initViewpagerIndicator() { //定义布局参数,设置标题条目是包裹内容 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); //设置外边距 layoutParams.setMargins(10,5,10,5); //动态的为线性布局添加view条目 for (int i=0;i<titleArray.length;i++){ View view = View.inflate(this, R.layout.title_item, null); TextView tv_title = (TextView) view.findViewById(R.id.tv_title); tv_title.setText(titleArray[i]); TextView tv_indicator = (TextView) view.findViewById(R.id.tv_indicator); if(i==0){ tv_title.setTextColor(Color.RED); tv_indicator.setVisibility(View.VISIBLE); }else { tv_title.setTextColor(Color.BLACK); tv_indicator.setVisibility(View.GONE); } view.setOnClickListener(this); mLltitles.addView(view,layoutParams); } } private void initview() { mScrollView = (HorizontalScrollView) findViewById(R.id.scrollView); mViewPager = (ViewPager) findViewById(R.id.viewpager); mLltitles = (LinearLayout) findViewById(R.id.ll_titles); } @Override public void onClick(View v) { //字体颜色变,indicator显示隐藏,viewpager选中某一个对应的界面 for(int i=0;i<mLltitles.getChildCount();i++){ View childAt = mLltitles.getChildAt(i); TextView tv_title = (TextView) childAt.findViewById(R.id.tv_title); TextView tv_indicator= (TextView) childAt.findViewById(R.id.tv_indicator); if(v==childAt){ //切换颜色 tv_indicator.setVisibility(View.VISIBLE); tv_title.setTextColor(Color.RED); //点击条目时,viewpager切换页面 mViewPager.setCurrentItem(i); }else { tv_indicator.setVisibility(View.GONE); tv_title.setTextColor(Color.BLACK); } } } }
3:NewsFragment
package fragment; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.example.a1.zhaoshan20170410.R; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import adapter.MyAdapter; import bean.MyBean; import utils.MyXutils; /** * data:2017/4/10 * author: * function: */ public class NewsFragment extends Fragment { private View mView; String path ="http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri="; private ListView mLv; private List<MyBean.ResultBean.DataBean> mDatalist; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mView = inflater.inflate(R.layout.fragment, null); initview(); return mView; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //获取传过来的数据 String type = getArguments().getString("type"); String url = path + type; MyXutils myXutils=new MyXutils(getActivity(),mLv); myXutils.getXutil(url); // AsyncTask<String,Integer,String>asyncTask = new AsyncTask<String, Integer, String>() { // // @Override // protected String doInBackground(String... params) { // try { // URL url = new URL(params[0]); // HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // connection.setConnectTimeout(3000); // connection.setReadTimeout(3000); // connection.setRequestMethod("GET"); // int code = connection.getResponseCode(); // if (code==200){ // InputStream inputStream = connection.getInputStream(); // BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8")); // StringBuilder builder = new StringBuilder(); // String str; // while ((str=reader.readLine())!=null){ // builder.append(str); // } // return builder.toString(); // } // } catch (Exception e) { // e.printStackTrace(); // } // return null; // } // // @Override // protected void onPostExecute(String s) { // super.onPostExecute(s); // //解析json // Gson gson = new Gson(); // MyBean bean = gson.fromJson(s, MyBean.class); // //进行展示 // mDatalist = bean.getResult().getData(); // mLv.setAdapter(new MyAdapter(mDatalist,getActivity())); // } // }; // asyncTask.execute(url); } private void initview() { mLv = (ListView) mView.findViewById(R.id.listview); } public static NewsFragment getInstance(String type){ NewsFragment newsFragment = new NewsFragment(); Bundle bundle = new Bundle(); bundle.putString("type",type); newsFragment.setArguments(bundle); return newsFragment; } }4:MyXutils
package utils; import android.content.Context; import android.util.Log; import android.widget.ListView; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.List; import adapter.MyAdapter; import bean.MyBean; public class MyXutils { private Context context; private ListView mLv; private List<MyBean.ResultBean.DataBean> mDatalist; public MyXutils(Context context, ListView lv) { this.context = context; mLv = lv; } public void getXutil(String uri){ RequestParams params = new RequestParams(uri);//params.addQueryStringParameter("num","10"); //System.out.println(params); Log.d("zzz",params.toString()); x.http().get(params, new Callback.CommonCallback<String>() { public void onSuccess(String result) { // System.out.println(result); Gson gson=new Gson(); MyBean bean = gson.fromJson(result, MyBean.class); mDatalist = bean.getResult().getData(); mLv.setAdapter(new MyAdapter(mDatalist,context)); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } }5:MyApplication
package utils; import android.app.Application; import org.xutils.x; public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(false); } }6:MyAdapter
package adapter; import android.content.Context; import android.graphics.drawable.Drawable; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.a1.zhaoshan20170410.R; import org.xutils.common.Callback; import java.util.List; import org.xutils.common.Callback; import org.xutils.x; import bean.MyBean; import static android.R.id.list; /** * data:2017/4/7 * author: * function: */ public class MyAdapter extends BaseAdapter { private List<MyBean.ResultBean.DataBean> mDatalist; private Context context; public MyAdapter(List<MyBean.ResultBean.DataBean> mDatalist, Context context) { this.mDatalist = mDatalist; this.context = context; } @Override public int getCount() { return mDatalist.size(); } @Override public Object getItem(int position) { return mDatalist.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHodler hodler; if (convertView==null){ convertView =View.inflate(context, R.layout.list_item,null); hodler = new ViewHodler(); hodler.mImageView = (ImageView) convertView.findViewById(R.id.image); hodler.text1 = (TextView) convertView.findViewById(R.id.text); hodler.text2 = (TextView) convertView.findViewById(R.id.text2); convertView.setTag(hodler); }else { hodler = (ViewHodler) convertView.getTag(); } hodler.text1.setText(mDatalist.get(position).getTitle()); hodler.text2.setText(mDatalist.get(position).getAuthor_name()); x.image().bind(hodler.mImageView,mDatalist.get(position).getThumbnail_pic_s()); return convertView; } class ViewHodler{ ImageView mImageView; TextView text1,text2; } }