标签:
Android——今日头条APP——高仿——ZYFNewsCTZF
图片效果:1
主页代码:MainActivity:
1.首页代码:SyFragment:
1.新闻频道代码:
2.新闻内容代码:
跳转——显示新闻详细内容
适配器——NewsBaseAdapter-NewsFragmentAdapter
entity——MyNews
2.我的代码:MyFragment:
MyFangment布局
跳转登录界面换头像
RoundImageView 插件
主页代码:MainActivity:FragmentTabHost
<span style="font-size:18px;"><strong>package com.example.zyfnewsctzf; import android.annotation.TargetApi; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTabHost; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TextView; import com.example.zyfnewsctzf.fragment.GzFragment; import com.example.zyfnewsctzf.fragment.MyFragment; import com.example.zyfnewsctzf.fragment.SpFragment; import com.example.zyfnewsctzf.fragment.SyFragment; public class MainActivity extends AppCompatActivity { private FragmentTabHost ft; private String[] str={"首页","视频","关注","我的"}; private int[] imgRes={R.drawable.sy ,R.drawable.sp,R.drawable.gz,R.drawable.my}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initWindow(); setContentView(R.layout.activity_main); ft=(FragmentTabHost)findViewById(R.id.ft); getSupportActionBar().hide(); init(); } //使状态栏透明的方法 @TargetApi(19) private void initWindow() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //状态栏 // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//导航栏 } } private void init() { ft.setup(this, getSupportFragmentManager(), R.id.f1); Fragment fragment = new SyFragment(); TabHost.TabSpec tabSpec0 = ft.newTabSpec(str[0]).setIndicator(getView(0)); ft.addTab(tabSpec0, fragment.getClass(), null); Fragment fragment1 = new SpFragment(); TabHost.TabSpec tabSpec1 = ft.newTabSpec(str[1]).setIndicator(getView(1)); ft.addTab(tabSpec1, fragment1.getClass(), null); Fragment fragment2 = new GzFragment(); TabHost.TabSpec tabSpec2 = ft.newTabSpec(str[2]).setIndicator(getView(2)); ft.addTab(tabSpec2, fragment2.getClass(), null); Fragment fragment3 = new MyFragment(); TabHost.TabSpec tabSpec3 = ft.newTabSpec(str[3]).setIndicator(getView(3)); ft.addTab(tabSpec3, fragment3.getClass(), null); } public View getView(int i){ View v=getLayoutInflater().inflate(R.layout.tab_layout,null); ImageView iv=(ImageView)v.findViewById(R.id.iv); iv.setImageResource(imgRes[i]); TextView tv=(TextView)v.findViewById(R.id.tv); tv.setText(str[i]); return v; } } </strong></span>
<span style="font-size:18px;"><strong><?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="com.example.zyfnewsctzf.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/f1" > </FrameLayout> <android.support.v4.app.FragmentTabHost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ft" > </android.support.v4.app.FragmentTabHost> </LinearLayout> </strong></span>
1.首页代码:SyFragment:
<span style="font-size:18px;"><strong><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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.example.zyfnewsctzf.fragment.SyFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:background="#f70404"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginTop="20dp" > <com.astuetz.PagerSlidingTabStrip android:layout_width="match_parent" android:layout_height="55dp" android:id="@+id/pst" android:background="#f70404" app:zmsSelectedTabTextColor="#fdfcfc" app:zmsTabTextColor="#efeeee" app:zmsTabTextSize="16sp" app:zmsSelectedTabTextSize="20sp" > </com.astuetz.PagerSlidingTabStrip> <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> </FrameLayout> </strong></span>
1.新闻频道代码:
<span style="font-size:18px;">package com.example.zyfnewsctzf.fragment; import android.os.AsyncTask; import android.os.Bundle; 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.widget.Toast; import com.astuetz.PagerSlidingTabStrip; import com.example.zyfnewsctzf.R; import com.example.zyfnewsctzf.adapter.NewsFragmentAdapter; import com.example.zyfnewsctzf.util.FileUitlity; import com.example.zyfnewsctzf.util.HttpUtil; import com.example.zyfnewsctzf.util.UrlUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * A simple {@link Fragment} subclass. */ public class SyFragment extends Fragment { private PagerSlidingTabStrip pst; private ViewPager vp; private List<Fragment> fragmentList; private List<Map<String,String>> channelList; private List<String> titles; private NewsFragmentAdapter nfa; public SyFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_sy, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); fragmentList = new ArrayList<>(); titles = new ArrayList<>(); pst = (PagerSlidingTabStrip) getView().findViewById(R.id.pst); vp = (ViewPager) getView().findViewById(R.id.vp); nfa = new NewsFragmentAdapter(getChildFragmentManager(), fragmentList, titles); vp.setAdapter(nfa); vp.setOffscreenPageLimit(2); pst.setViewPager(vp); pst.setIndicatorHeight(0); pst.setUnderlineHeight(0); pst.setShouldExpand(true); //读取文件 String fileName ="channel.txt"; String pn = getActivity().getPackageName(); //如果存在频道文件则读取 否则 从网络获取 if(FileUitlity.fileExists(pn+"/"+fileName)){ String ja = FileUitlity.readFileFromSdcard(pn+"/"+fileName); try { Log.d("=====","读取文件成功"); Log.d("====1=",ja); initFragment(new JSONArray(ja)); } catch (JSONException e) { e.printStackTrace(); } }else{ new GetChannel().execute(UrlUtil.channelUrl); } } public class GetChannel extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... strings) { return HttpUtil.HttpGet(strings[0]); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s==null) { Toast.makeText(getContext(),"没有数据网路不好",Toast.LENGTH_SHORT).show(); return; } try { JSONObject jsonObject = new JSONObject(s); JSONObject body= jsonObject.getJSONObject("showapi_res_body"); JSONArray ja = body.getJSONArray("channelList"); if(ja.length()>0){ String pn = getActivity().getPackageName(); FileUitlity.getInstance(getActivity(),pn); String result =FileUitlity.saveFileToSdcard(pn+"/channel.txt", ja.toString()); if(result.equals("ok")){ Log.d("=====","文件保存成功"); }else{ Log.d("=====","文件保存失败"); } } initFragment(ja); } catch (JSONException e) { e.printStackTrace(); } } } public void initFragment(JSONArray ja) throws JSONException { //解析频道json数据 channelList = new ArrayList<>(); for(int i=0;i<ja.length()/8;i++){ JSONObject obj = (JSONObject) ja.get(i); String id = obj.getString("channelId"); String name = obj.getString("name"); HashMap map = new HashMap(); map.put("id",id); map.put("name",name); channelList.add(map); titles.add(name); } for(int i=0;i<channelList.size();i++){ Fragment fragment = new NewsFragment(); Bundle bundle = new Bundle(); bundle.putString("id",channelList.get(i).get("id")); bundle.putString("name",channelList.get(i).get("name")); fragment.setArguments(bundle); fragmentList.add(fragment); } pst.notifyDataSetChanged(); nfa.notifyDataSetChanged(); } }</span><span style="font-size: 24px;"> </span>
2.新闻内容代码:
<span style="font-size:18px;">package com.example.zyfnewsctzf.fragment; 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.ListView; import android.widget.Toast; import com.example.zyfnewsctzf.R; import com.example.zyfnewsctzf.adapter.NewsBaseAdapter; import com.example.zyfnewsctzf.entity.MyNews; import com.example.zyfnewsctzf.util.HttpUtil; import com.example.zyfnewsctzf.util.UrlUtil; import com.handmark.pulltorefresh.library.ILoadingLayout; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class NewsFragment extends Fragment { private PullToRefreshListView prlv; private NewsBaseAdapter nba; private List<MyNews> newsList; String pages,channelId,channelName; private String[] strings; int page=1; public NewsFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_news, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); newsList = new ArrayList<>(); prlv = (PullToRefreshListView) getView().findViewById(R.id.prlv); nba = new NewsBaseAdapter(newsList, getActivity()); prlv.setAdapter(nba); Bundle bundle = getArguments(); channelId= bundle.getString("id"); channelName = bundle.getString("name"); pages= String.valueOf(page); strings=new String[]{channelName,channelId,pages}; new GetNews().execute(strings); //上拉下拉 prlv.setMode(PullToRefreshBase.Mode.BOTH); ILoadingLayout upLoading = prlv.getLoadingLayoutProxy(true, false); upLoading.setPullLabel("下拉客官请稍等"); upLoading.setRefreshingLabel("客官刷新中"); upLoading.setReleaseLabel("好了客官"); ILoadingLayout downLoading = prlv.getLoadingLayoutProxy(false, true); downLoading.setPullLabel("上拉客官请稍等"); downLoading.setRefreshingLabel("客官刷新中"); downLoading.setReleaseLabel("好了客官"); prlv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { //下拉 @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { page=1; newsList=new ArrayList<MyNews>(); pages= String.valueOf(page); strings=new String[]{channelName,channelId,pages}; new GetNews().execute(strings); } //上拉 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { page++; newsList=new ArrayList<MyNews>(); pages= String.valueOf(page); strings=new String[]{channelName,channelId,pages}; new GetNews().execute(strings); } }); } public class GetNews extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... strings) { String url = UrlUtil.newsUrl + "?channelId=" + strings[1] + "&channelName=" + strings[0] + "&needHtml=1" +"&page="+strings[2] +"&title="+"&needContent" ; return HttpUtil.HttpGet(url); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); //解析 新闻数据 if (s==null) { Toast.makeText(getContext(),"没有数据网路不好",Toast.LENGTH_SHORT).show(); return; } try { JSONObject obj = new JSONObject(s); JSONObject body = obj.getJSONObject("showapi_res_body"); JSONObject pageBean = body.getJSONObject("pagebean"); JSONArray contentList = pageBean.getJSONArray("contentlist"); for (int i=0;i<contentList.length();i++){ JSONObject jo = (JSONObject) contentList.get(i); MyNews myNews = new MyNews(); myNews.setTitle(jo.getString("title")); myNews.setPubDate(jo.getString("pubDate")); myNews.setImageurls(jo.getString("imageurls")); myNews.setSource(jo.getString("source")); myNews.setHtml(jo.getString("html")); newsList.add(myNews); } nba.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } } } </span>
<span style="font-size:18px;"><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="com.example.zyfnewsctzf.fragment.NewsFragment"> <!-- TODO: Update blank fragment layout --> <com.handmark.pulltorefresh.library.PullToRefreshListView android:descendantFocusability="blocksDescendants" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:dividerHeight="0dp" android:scrollbars="none" android:id="@+id/prlv"> > </com.handmark.pulltorefresh.library.PullToRefreshListView> </FrameLayout> </span>跳转——显示新闻详细内容——ContentActivity
package com.example.zyfnewsctzf; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.ImageView; import android.widget.TextView; import com.example.zyfnewsctzf.entity.MyNews; public class ContentActivity extends AppCompatActivity { private WebView wv; private ImageView back; private TextView title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_content); getSupportActionBar().hide(); wv=(WebView)findViewById(R.id.wv); back=(ImageView)findViewById(R.id.back) ; title=(TextView)findViewById(R.id.title) ; Intent intent=getIntent(); String html=""; String titles=""; if (intent!=null){ MyNews news= intent.getParcelableExtra("news"); html=news.getHtml(); titles=news.getTitle(); } title.setText(titles); WebSettings ws=wv.getSettings(); ws.setSupportZoom(true);//支持放大 ws.setDisplayZoomControls(true);//放大缩小控件 加减号 ws.setJavaScriptEnabled(true); ws.setDefaultTextEncodingName("utf-8"); wv.setBackgroundColor(getResources().getColor(R.color.backback)); wv.loadDataWithBaseURL("",html,"text/html","utf-8",""); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } }布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.zyfnewsctzf.ContentActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:id="@+id/linearLayout"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/back" android:src="@mipmap/black_down_video_details" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/title" android:text="qqqq" android:background="@color/red" android:textStyle="bold" android:textSize="20sp" android:gravity="center" /> </LinearLayout> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/wv" android:layout_below="@+id/linearLayout"></WebView> </RelativeLayout>
适配器——NewsBaseAdapter-NewsFragmentAdapter
package com.example.zyfnewsctzf.adapter; import android.content.Context; import android.content.Intent; import android.util.Log; 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.bumptech.glide.Glide; import com.example.zyfnewsctzf.ContentActivity; import com.example.zyfnewsctzf.R; import com.example.zyfnewsctzf.entity.MyNews; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; /** * Created by 冲天之峰 on 2016/8/18. */ public class NewsBaseAdapter extends BaseAdapter{ private List<MyNews> myNews; private Context context; private final int TYPE1=0,TYPE2=1,TYPE3=2,TYPE4=3,TYPE5=4; public NewsBaseAdapter( List<MyNews> myNews,Context context) { this.context = context; this.myNews = myNews; } @Override public int getCount() { return myNews.size(); } @Override public Object getItem(int i) { return myNews.get(i); } @Override public long getItemId(int i) { return 0; } @Override public int getItemViewType(int position) { switch (paserImageList(myNews.get(position).getImageurls()).size()){ case 0:return TYPE1; case 1:return TYPE2; case 2:return TYPE3; case 3:return TYPE4; case 4:return TYPE5; } return 0; } @Override public int getViewTypeCount() { return 5; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder vh; int type = getItemViewType(i); if(view == null){ vh = new ViewHolder(); if(type ==0){ view = LayoutInflater.from(context).inflate(R.layout.list_layout_base1,null); vh.title = (TextView) view.findViewById(R.id.title); vh.content = (TextView) view.findViewById(R.id.content); vh.pubDate = (TextView) view.findViewById(R.id.pubDate); vh.from = (TextView) view.findViewById(R.id.from); view.setTag(vh); }else if (type ==1){ view = LayoutInflater.from(context).inflate(R.layout.list_layout_base2,null); vh.title = (TextView) view.findViewById(R.id.title); vh.content = (TextView) view.findViewById(R.id.content); vh.pubDate = (TextView) view.findViewById(R.id.pubDate); vh.from = (TextView) view.findViewById(R.id.from); vh.ii = (TextView) view.findViewById(R.id.ii); vh.img = (ImageView) view.findViewById(R.id.img); view.setTag(vh); }else if (type ==2) { view = LayoutInflater.from(context).inflate(R.layout.list_layout_base3,null); vh.title = (TextView) view.findViewById(R.id.title); vh.content = (TextView) view.findViewById(R.id.content); vh.pubDate = (TextView) view.findViewById(R.id.pubDate); vh.from = (TextView) view.findViewById(R.id.from); vh.img1 = (ImageView) view.findViewById(R.id.img1); vh.img2 = (ImageView) view.findViewById(R.id.img2); view.setTag(vh); } else if (type ==3) { view = LayoutInflater.from(context).inflate(R.layout.list_layout_base4,null); vh.title = (TextView) view.findViewById(R.id.title); vh.content = (TextView) view.findViewById(R.id.content); vh.pubDate = (TextView) view.findViewById(R.id.pubDate); vh.from = (TextView) view.findViewById(R.id.from); vh.ii = (TextView) view.findViewById(R.id.ii); vh.img31 = (ImageView) view.findViewById(R.id.img31); vh.img32 = (ImageView) view.findViewById(R.id.img32); vh.img33 = (ImageView) view.findViewById(R.id.img33); view.setTag(vh); } else if (type ==4) { view = LayoutInflater.from(context).inflate(R.layout.list_layout_base5,null); vh.title = (TextView) view.findViewById(R.id.title); vh.content = (TextView) view.findViewById(R.id.content); vh.pubDate = (TextView) view.findViewById(R.id.pubDate); vh.from = (TextView) view.findViewById(R.id.from); vh.img41 = (ImageView) view.findViewById(R.id.img41); vh.img42 = (ImageView) view.findViewById(R.id.img42); vh.img43 = (ImageView) view.findViewById(R.id.img43); vh.img44 = (ImageView) view.findViewById(R.id.img44); view.setTag(vh); } }else{ vh = (ViewHolder) view.getTag(); } MyNews news = myNews.get(i); if(type == 0){ vh.title.setText(news.getTitle()); vh.content.setText(news.getHtml()); vh.pubDate.setText(news.getPubDate()); vh.from.setText(news.getSource()); }else if (type == 1){ vh.title.setText(news.getTitle()); vh.content.setText(news.getHtml()); vh.pubDate.setText(news.getPubDate()); vh.from.setText(news.getSource()); vh.ii.setText(news.getImageurls()); List list=paserImageList(news.getImageurls()); if(list.size()==1){ Glide.with(context).load(list.get(0)).into(vh.img); } }else if (type == 2){ vh.title.setText(news.getTitle()); vh.content.setText(news.getHtml()); vh.pubDate.setText(news.getPubDate()); vh.from.setText(news.getSource()); List list=paserImageList(news.getImageurls()); if(list.size()==2){ Glide.with(context).load(list.get(0)).into(vh.img1); Glide.with(context).load(list.get(1)).into(vh.img2); } } else if (type == 3){ vh.title.setText(news.getTitle()); vh.content.setText(news.getHtml()); vh.pubDate.setText(news.getPubDate()); vh.from.setText(news.getSource()); vh.ii.setText(news.getImageurls()); List list=paserImageList(news.getImageurls()); if(list.size()==3){ Glide.with(context).load(list.get(0)).into(vh.img31); Glide.with(context).load(list.get(1)).into(vh.img32); Glide.with(context).load(list.get(2)).into(vh.img33); } } else if (type == 4){ vh.title.setText(news.getTitle()); vh.content.setText(news.getHtml()); vh.pubDate.setText(news.getPubDate()); vh.from.setText(news.getSource()); List list=paserImageList(news.getImageurls()); if(list.size()==4){ Glide.with(context).load(list.get(0)).into(vh.img41); Glide.with(context).load(list.get(1)).into(vh.img42); Glide.with(context).load(list.get(2)).into(vh.img43); Glide.with(context).load(list.get(3)).into(vh.img44); } } view.setOnClickListener(new ClickLisner(i)); return view; } public class ClickLisner implements View.OnClickListener { private int position; //在类里生成构造 把position传进来 public ClickLisner(int position) { this.position = position; } @Override public void onClick(View v) { MyNews news = myNews.get(position); Log.d("======", news.getHtml()); Intent intent=new Intent(context, ContentActivity.class); //MyNews要实现序列化 intent.putExtra("news",news); context.startActivity(intent); } } //此处的控件变量是为了关联布局中的控件,以便传值。 private class ViewHolder{ //布局1中使用的控件 TextView title; TextView content; TextView pubDate; TextView ii; TextView from; ImageView img; ImageView img1; ImageView img2; ImageView img31; ImageView img32; ImageView img33; ImageView img41; ImageView img42; ImageView img43; ImageView img44; } public List paserImageList (String imgList){ List img=new ArrayList(); try { JSONArray jsonArray = new JSONArray(imgList); for(int i = 0;i<jsonArray.length();i++){ JSONObject jo = (JSONObject) jsonArray.get(i); if(jo.has("url")){ img.add(jo.getString("url")); } // String url = jo.getString("url"); // Log.d("=====", url); // img.add(url); // Log.d("==img===", img+""); } } catch (JSONException e) { e.printStackTrace(); } return img; } // public List<String> pictrue(MyNews my){ // List<String> imageCount = new ArrayList<>(); // try { // JSONArray jsonArray = new JSONArray(my.getImageurls()); // for(int i = 0;i<jsonArray.length();i++){ // JSONObject jo = (JSONObject) jsonArray.get(i); // String url = jo.getString("url"); // imageCount.add(url); // Log.d("=======",url); // } // } catch (JSONException e) { // e.printStackTrace(); // } // return imageCount; // } }
package com.example.zyfnewsctzf.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * Created by 冲天之峰 on 2016/8/18. */ public class NewsFragmentAdapter extends FragmentPagerAdapter { private List<Fragment> fragmentList; private List<String> titles; public NewsFragmentAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titles) { super(fm); this.fragmentList = fragmentList; this.titles = titles; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } }
entity——MyNews
<span style="font-size:18px;">package com.example.zyfnewsctzf.entity; import android.os.Parcel; import android.os.Parcelable; import java.util.List; /** * Created by Administrator on 2016/8/18. */ public class MyNews implements Parcelable{ private String pubDate; private String title; private String channelName; private String desc; private String source; private String channelId; private String link; private String html; private List<String> allList; private String imageurls; public MyNews() { } protected MyNews(Parcel in) { pubDate = in.readString(); title = in.readString(); channelName = in.readString(); desc = in.readString(); source = in.readString(); channelId = in.readString(); link = in.readString(); html = in.readString(); allList = in.createStringArrayList(); imageurls = in.readString(); } public static final Creator<MyNews> CREATOR = new Creator<MyNews>() { @Override public MyNews createFromParcel(Parcel in) { return new MyNews(in); } @Override public MyNews[] newArray(int size) { return new MyNews[size]; } }; public String getPubDate() { return pubDate; } public void setPubDate(String pubDate) { this.pubDate = pubDate; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getChannelName() { return channelName; } public void setChannelName(String channelName) { this.channelName = channelName; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getChannelId() { return channelId; } public void setChannelId(String channelId) { this.channelId = channelId; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getHtml() { return html; } public void setHtml(String html) { this.html = html; } public List<String> getAllList() { return allList; } public void setAllList(List<String> allList) { this.allList = allList; } public String getImageurls() { return imageurls; } public void setImageurls(String imageurls) { this.imageurls = imageurls; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeString(pubDate); parcel.writeString(title); parcel.writeString(channelName); parcel.writeString(desc); parcel.writeString(source); parcel.writeString(channelId); parcel.writeString(link); parcel.writeString(html); parcel.writeStringList(allList); parcel.writeString(imageurls); } } </span>
2.我的代码:MyFragment:
<span style="font-size:18px;">package com.example.zyfnewsctzf.fragment; import android.content.Intent; 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.ImageView; import com.example.zyfnewsctzf.MyIntentActivity; import com.example.zyfnewsctzf.R; /** * A simple {@link Fragment} subclass. */ public class MyFragment extends Fragment { private ImageView login; public MyFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_my, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); login=(ImageView)getView().findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent= new Intent(getActivity(),MyIntentActivity.class); getActivity().startActivity(intent); } }); } } </span>MyFangment布局
<span style="font-size:18px;"><ScrollView 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="com.example.zyfnewsctzf.fragment.MyFragment"> <!-- TODO: Update blank fragment layout --> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.flaviofaria.kenburnsview.KenBurnsView android:layout_width="match_parent" android:layout_height="230dp" android:src="@mipmap/hh" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="210dp" android:orientation="vertical" android:layout_marginTop="20dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:orientation="horizontal"> <ImageView android:id="@+id/login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:src="@mipmap/log_in_register" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginTop="10dp" android:background="#00111111" android:text="登录推荐更准确" android:textColor="#fff" android:textSize="12sp" /> </LinearLayout> </LinearLayout> <RadioGroup android:id="@+id/mine_rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="5dp"> <RadioButton android:id="@+id/collection" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="5dp" android:drawableTop="@mipmap/favoriteicon_profile" android:gravity="center_horizontal" android:paddingBottom="15dp" android:text="收藏" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="5dp" android:drawableTop="@mipmap/nighticon_profile" android:gravity="center_horizontal" android:paddingBottom="15dp" android:text="夜间" /> <RadioButton android:id="@+id/mine_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawablePadding="5dp" android:drawableTop="@mipmap/setupicon_profile" android:gravity="center_horizontal" android:paddingBottom="15dp" android:text="设置" /> </RadioGroup> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="25dp" android:paddingLeft="20dp" android:paddingTop="15dp" android:text="好友动态" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:src="@mipmap/arrow_listpage_pressed" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingTop="15dp" android:text="消息通知" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:src="@mipmap/arrow_listpage_pressed" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingTop="15dp" android:text="离线" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:src="@mipmap/arrow_listpage_pressed" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingTop="15dp" android:text="活动" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:src="@mipmap/arrow_listpage_pressed" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingTop="15dp" android:text="商城" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:src="@mipmap/arrow_listpage_pressed" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingTop="15dp" android:text="我要爆料" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:src="@mipmap/arrow_listpage_pressed" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingTop="15dp" android:text="反馈" android:textSize="16sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:src="@mipmap/arrow_listpage_pressed" /> </RelativeLayout> </LinearLayout> </RelativeLayout> </ScrollView></span><span style="font-size: 24px;"> </span>
<span style="font-size:18px;">package com.example.zyfnewsctzf; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import com.example.zyfnewsctzf.util.FileUitlity1; import com.example.zyfnewsctzf.util.RoundImageView; import java.io.File; public class MyIntentActivity extends AppCompatActivity implements View.OnClickListener { private PopupWindow pw; private View popView; private RoundImageView riv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent); getSupportActionBar().hide(); //加载PopWindow中的布局 popView = getLayoutInflater().inflate(R.layout.pop_layout, null); //从主布局中取得控件 //从PopWindow布局中取得控件 Button xc = (Button)popView.findViewById(R.id.xc); Button xj = (Button)popView.findViewById(R.id.xj); Button bt = (Button)popView.findViewById(R.id.bt); riv = (RoundImageView) findViewById(R.id.riv); //注册 本类监听 riv.setOnClickListener(this); xc.setOnClickListener(this); xj.setOnClickListener(this); bt.setOnClickListener(this); //显示Intent,明确指定要跳转的组件 // Intent intent=new Intent(IntentActivity.this,SecondActivity.class); // startActivity(intent); //--------------------------------------- } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.riv://点击头像打开PopWindow pw=getPopWindow(popView); break; case R.id.xc: phonePhoto(); break; case R.id.xj: takephoto(); break; case R.id.bt: pw.dismiss(); break; } } /* * 调用图库 * */ public void phonePhoto(){ Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent,2); } /* * 调用相机 * */ private String capturePath=""; public void takephoto(){ Intent camera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File parent= FileUitlity1.getInstance(getApplicationContext()) .makeDir("head_imag"); capturePath=parent.getPath()+File.separatorChar+System.currentTimeMillis()+".jpg"; camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(capturePath))); camera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); startActivityForResult(camera, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode!= Activity.RESULT_OK){ return; } //相机返回结果,调用系统裁剪啊 if (requestCode==1){ startPicZoom(Uri.fromFile(new File(capturePath))); } //相册返回结果调用系统裁剪 else if (requestCode==2){ Cursor cursor=getContentResolver() .query(data.getData(),new String[]{MediaStore.Images.Media.DATA} ,null,null,null); cursor.moveToFirst(); String capturePath=cursor.getString( cursor.getColumnIndex( MediaStore.Images.Media.DATA)); cursor.close(); startPicZoom(Uri.fromFile(new File(capturePath))); } else if (requestCode==3){ Bundle bundle= data.getExtras(); if (bundle!=null){ Bitmap bitmap=bundle.getParcelable("data"); riv.setImageBitmap(bitmap); } } } /* 调用系统裁剪功能 */ public void startPicZoom(Uri uri){ Intent intent=new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri,"image/*"); intent.putExtra("crop","true");//允许裁剪 intent.putExtra("aspectX",1);//设置裁剪比例 intent.putExtra("aspectY",1); //设置图片宽度高度 intent.putExtra("outputX",150); intent.putExtra("outputY",150); intent.putExtra("return-data",true); startActivityForResult(intent,3); } //设置屏幕背景透明度方法 public void backgroundAlpha(float bgAlpha){ WindowManager.LayoutParams ll=getWindow().getAttributes(); ll.alpha=bgAlpha; getWindow().setAttributes(ll); } //构建一个PopWindow public PopupWindow getPopWindow(View view){ PopupWindow popupWindow=new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,true); // popupWindow.setFocusable(true); //点击pop外面是否消失 popupWindow.setOutsideTouchable(true); popupWindow.setAnimationStyle(R.style.popStyle); //设置背景透明度 backgroundAlpha(0.3f); //———————— //设置View隐藏 riv.setVisibility(View.GONE); popupWindow.setBackgroundDrawable(new ColorDrawable()); popupWindow.showAtLocation(riv, Gravity.BOTTOM, 0, 0); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { //设置背景透明度 backgroundAlpha(1f); //设置View可见 riv.setVisibility(View.VISIBLE); } }); return popupWindow; } }</span><span style="font-size: 24px;"> </span>
布局1+2
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.zyfnewsctzf.MyIntentActivity"> <com.example.zyfnewsctzf.util.RoundImageView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/riv" android:src="@mipmap/zyf" android:layout_marginLeft="80dp" android:layout_marginTop="40dp" android:background="#f9081c" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/riv" android:id="@+id/tv" android:text="冲天之峰" android:gravity="center" android:textSize="40sp" android:textStyle="bold" android:textColor="#f9081c" android:layout_marginTop="20dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv" android:text="登录成功" android:gravity="center" android:id="@+id/tv1" android:textSize="40sp" android:textStyle="bold" android:textColor="#f9081c" android:layout_marginTop="20dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:text="ZYFNewsCTZF" android:gravity="center" android:textSize="40sp" android:textStyle="bold" android:textColor="#f9081c" android:layout_marginTop="20dp" /> </RelativeLayout>2pop
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="200dp" android:background="#fa0303" > <Button android:layout_width="300dp" android:background="#ffffff" android:layout_height="40dp" android:id="@+id/xj" android:layout_marginTop="20dp" android:gravity="center" android:layout_marginLeft="40dp" android:text="相机" android:textSize="30sp" /> <Button android:layout_width="300dp" android:layout_height="40dp" android:id="@+id/xc" android:text="相册" android:layout_marginTop="20dp" android:background="#ffffff" android:textSize="30sp" android:gravity="center" android:layout_marginLeft="40dp" /> <Button android:layout_width="300dp" android:layout_height="40dp" android:layout_marginTop="20dp" android:id="@+id/bt" android:text="退出" android:gravity="center" android:background="#ffffff" android:layout_marginLeft="40dp" android:textSize="30sp" /> </LinearLayout></span>
RoundImageView 插件
<span style="font-size:18px;">package com.example.zyfnewsctzf.util; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; public class RoundImageView extends ImageView { public RoundImageView(Context context) { super(context); // TODO Auto-generated constructor stub } public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public RoundImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap b = null; if(drawable instanceof BitmapDrawable){ b = ((BitmapDrawable) drawable).getBitmap(); }else if(drawable instanceof Drawable){ b = Bitmap.createBitmap( getWidth(), getHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas1 = new Canvas(b); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, getWidth(), getHeight()); drawable.draw(canvas1); } if (null == b) { return; } Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(), h = getHeight(); Bitmap roundBitmap = getCroppedBitmap(bitmap, w); canvas.drawBitmap(roundBitmap, 0, 0, null); } public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) { Bitmap sbmp; if (bmp.getWidth() != radius || bmp.getHeight() != radius) sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false); else sbmp = bmp; Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffa19774; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.parseColor("#BAB399")); canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(sbmp, rect, rect, paint); return output; } }</span>
作者:冲天之峰 20160821
Android——今日头条APP——高仿——ZYFNewsCTZF
标签:
原文地址:http://blog.csdn.net/zhangyufeng0126/article/details/52269277