新闻资讯

news/2024/11/6 15:35:15/

主页面布局

<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" ><RadioGroup
        android:layout_width="fill_parent"android:layout_height="50dp"android:orientation="horizontal" ><RadioButton
            android:id="@+id/bt1"android:layout_width="50dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@drawable/radiobutton_item"android:checked="true"android:button="@null"android:gravity="center"android:text="资讯"android:textColor="#00ff00"android:textSize="20sp" /><RadioButton
            android:id="@+id/bt2"android:layout_width="50dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@drawable/radiobutton_item"android:button="@null"android:gravity="center"android:text="热点"android:textSize="20sp" /><RadioButton
            android:id="@+id/bt3"android:layout_width="50dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@drawable/radiobutton_item"android:button="@null"android:gravity="center"android:text="博客"android:textSize="20sp" /><RadioButton
            android:id="@+id/bt4"android:layout_width="50dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@drawable/radiobutton_item"android:button="@null"android:gravity="center"android:text="推荐"android:textSize="20sp" /></RadioGroup><FrameLayout android:id="@+id/fragmentlayout"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"></FrameLayout>
</LinearLayout>

radiobutton_item

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true"><layer-list><item android:top="44dp"><shape android:shape="rectangle"><solid android:color="#00ff00" /></shape></item></layer-list></item></selector>

tuijianfragment

<?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" ><com.bwie.test.xlistview.XListView
        android:id="@+id/xlistview"android:layout_width="fill_parent"android:layout_height="fill_parent"></com.bwie.test.xlistview.XListView></LinearLayout>

tuijian_item

<?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/tuijian_title"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="title"android:textColor="#000000"android:textSize="30sp" /><TextView
        android:id="@+id/tuijian_body"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView"android:textSize="20sp" /><LinearLayout
        android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView
            android:id="@+id/tuijian_author"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="author" /><TextView
            android:id="@+id/tuijian_pubDate"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="pubDate" /><TextView
            android:id="@+id/tuijian_commentCount"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="commentCount" /></LinearLayout></LinearLayout>

bean类

package com.bwie.test.bean;public class NewsInformation {public String title;public String id;public String body;public String commentCount;public String author;public String authorid;public String pubDate;@Overridepublic String toString() {return "NewsInformation [title=" + title + ", id=" + id + ", body="+ body + ", commentCount=" + commentCount + ", author="+ author + ", authroid=" + authorid + ", pubDate=" + pubDate+ "]";}}

适配器类

package com.bwie.test.adpater;import java.util.ArrayList;
import java.util.List;import com.bwie.test.R;
import com.bwie.test.bean.NewsInformation;import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;public class MyAdapter extends BaseAdapter {Context context;List<NewsInformation> list = new ArrayList<NewsInformation>();public MyAdapter(Context context) {super();this.context = context;}public void changeUp(List<NewsInformation> list) {this.list.addAll(0, list);this.notifyDataSetChanged();}public void changeDown(List<NewsInformation> list) {this.list.addAll(list);this.notifyDataSetChanged();}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup arg2) {ViewHolder holder;if (convertView == null) {holder = new ViewHolder();convertView = View.inflate(context, R.layout.tuijian_item, null);holder.tuijian_title = (TextView) convertView.findViewById(R.id.tuijian_title);holder.tuijian_body = (TextView) convertView.findViewById(R.id.tuijian_body);holder.tuijian_author = (TextView) convertView.findViewById(R.id.tuijian_author);holder.tuijian_pubDate = (TextView) convertView.findViewById(R.id.tuijian_pubDate);holder.tuijian_commentCount = (TextView) convertView.findViewById(R.id.tuijian_commentCount);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.tuijian_title.setText(list.get(position).title);holder.tuijian_body.setText(list.get(position).body);holder.tuijian_author.setText(list.get(position).author);holder.tuijian_pubDate.setText(list.get(position).pubDate);holder.tuijian_commentCount.setText(list.get(position).commentCount);return convertView;}class ViewHolder {TextView tuijian_title, tuijian_body, tuijian_author, tuijian_pubDate,tuijian_commentCount;}}

博客fragment

package com.bwie.test.fragment;import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;import com.bwie.test.R;
import com.bwie.test.adpater.MyAdapter;
import com.bwie.test.bean.NewsInformation;
import com.bwie.test.xlistview.XListView;
import com.bwie.test.xlistview.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class BoKeFragment extends Fragment implements IXListViewListener {private List<NewsInformation> list;private XListView listView;boolean type = true;private MyAdapter adapter;int j = 1;Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {if (msg.what == 1) {adapter.changeUp(list);} else if (msg.what == 2) {adapter.changeDown(list);}};};private String path;@Overridepublic View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.tuijianfragment, null);listView = (XListView) view.findViewById(R.id.xlistview);Bundle bundle = getArguments();path = (String) bundle.get("path");getXML();adapter = new MyAdapter(getActivity());listView.setAdapter(adapter);listView.setXListViewListener(this);listView.setPullLoadEnable(true);listView.setPullRefreshEnable(true);return view;}private void getXML() {new Thread() {public void run() {System.out.println("开始解析");HttpUtils httpUtils = new HttpUtils();httpUtils.send(HttpMethod.GET, path,new RequestCallBack<String>() {@Overridepublic void onFailure(HttpException arg0,String arg1) {System.out.println("解析失败");}@Overridepublic void onSuccess(ResponseInfo<String> arg0) {String xml = arg0.result;System.out.println(xml);InputStream inputStream = new ByteArrayInputStream(xml.getBytes());getPull(inputStream);if (type == true) {handler.sendEmptyMessage(1);} else {handler.sendEmptyMessage(2);}}});};}.start();}public void getPull(InputStream inputStream) {try {NewsInformation newsInfo = null;// 使用pull解析XmlPullParserFactory factory = XmlPullParserFactory.newInstance();XmlPullParser parser = factory.newPullParser();parser.setInput(inputStream, "utf-8");int type_name = parser.getEventType();while (XmlPullParser.END_DOCUMENT != parser.getEventType()) {switch (type_name) {case XmlPullParser.START_DOCUMENT:list = new ArrayList<NewsInformation>();break;case XmlPullParser.START_TAG:if ("blog".equals(parser.getName())) {newsInfo = new NewsInformation();}if (newsInfo != null) {if ("title".equals(parser.getName())) {newsInfo.title = parser.nextText();} else if ("id".equals(parser.getName())) {newsInfo.id = parser.nextText();} else if ("body".equals(parser.getName())) {newsInfo.body = parser.nextText();} else if ("commentCount".equals(parser.getName())) {newsInfo.commentCount = parser.nextText();} else if ("author".equals(parser.getName())) {newsInfo.author = parser.nextText();} else if ("pubDate".equals(parser.getName())) {newsInfo.pubDate = parser.nextText();} else if ("authorid".equals(parser.getName())) {newsInfo.authorid = parser.nextText();}}break;case XmlPullParser.END_TAG:if ("blog".equals(parser.getName())) {list.add(newsInfo);System.out.println(newsInfo.toString());}break;default:break;}type_name=parser.next();}} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onRefresh() {path=path+1;type=true;getXML();close();}public void close(){listView.stopLoadMore();listView.stopRefresh();}@Overridepublic void onLoadMore() {System.out.println("上拉-----");j++;path=path+j;type=false;getXML();close();}}

推荐fragment

package com.bwie.test.fragment;import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;import com.bwie.test.R;
import com.bwie.test.adpater.MyAdapter;
import com.bwie.test.bean.NewsInformation;
import com.bwie.test.xlistview.XListView;
import com.bwie.test.xlistview.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class TuiJianFragment extends Fragment implements IXListViewListener {private List<NewsInformation> list;private XListView listView;boolean type = true;private MyAdapter adapter;int j = 1;Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {if (msg.what == 1) {adapter.changeUp(list);} else if (msg.what == 2) {adapter.changeDown(list);}};};private String path;private String str;@Overridepublic View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.tuijianfragment, null);listView = (XListView) view.findViewById(R.id.xlistview);Bundle bundle = getArguments();path = (String) bundle.get("path");str = path + 1;getXML();adapter = new MyAdapter(getActivity());listView.setAdapter(adapter);listView.setXListViewListener(this);listView.setPullLoadEnable(true);listView.setPullRefreshEnable(true);return view;}private void getXML() {new Thread() {public void run() {HttpUtils httpUtils = new HttpUtils();System.out.println(str);httpUtils.send(HttpMethod.GET, str,new RequestCallBack<String>() {// 解析失败@Overridepublic void onFailure(HttpException arg0,String arg1) {System.out.println("解析失败");}// 解析成功@Overridepublic void onSuccess(ResponseInfo<String> arg0) {String xml = arg0.result;InputStream inputStream = new ByteArrayInputStream(xml.getBytes());getPull(inputStream);if (type == true) {handler.sendEmptyMessage(1);} else {handler.sendEmptyMessage(2);}}});};}.start();}public void getPull(InputStream inputStream) {try {NewsInformation newsInfo = null;// 使用pull解析数据XmlPullParserFactory factory = XmlPullParserFactory.newInstance();XmlPullParser parser = factory.newPullParser();parser.setInput(inputStream, "utf-8");int type_name = parser.getEventType();while (XmlPullParser.END_DOCUMENT != parser.getEventType()) {switch (type_name) {case XmlPullParser.START_DOCUMENT:list = new ArrayList<NewsInformation>();break;case XmlPullParser.START_TAG:if ("news".equals(parser.getName())) {newsInfo = new NewsInformation();}if (newsInfo != null) {if ("title".equals(parser.getName())) {newsInfo.title = parser.nextText();} else if ("id".equals(parser.getName())) {newsInfo.id = parser.nextText();} else if ("body".equals(parser.getName())) {newsInfo.body = parser.nextText();} else if ("commentCount".equals(parser.getName())) {newsInfo.commentCount = parser.nextText();} else if ("author".equals(parser.getName())) {newsInfo.author = parser.nextText();} else if ("pubDate".equals(parser.getName())) {newsInfo.pubDate = parser.nextText();} else if ("authorid".equals(parser.getName())) {newsInfo.authorid = parser.nextText();}}break;case XmlPullParser.END_TAG:if ("news".equals(parser.getName())) {list.add(newsInfo);// System.out.println(news.toString());}break;default:break;}type_name=parser.next();}} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onRefresh() {str=path+1;type=true;getXML();close();}public void close(){listView.stopRefresh();listView.stopLoadMore();}@Overridepublic void onLoadMore() {System.out.println("上拉---");j++;str=path+j;type=false;getXML();close();}}

mainactivity

package com.bwie.test;import com.bwie.test.fragment.BoKeFragment;
import com.bwie.test.fragment.TuiJianFragment;import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;public class MainActivity extends FragmentActivity {private RadioButton bt1;private RadioButton bt2;private RadioButton bt3;private RadioButton bt4;String path[] = {"http://www.oschina.net/action/api/news_list?catalog=1&pageSize=10&pageIndex=","http://www.oschina.net/action/api/news_list?catalog=4&pageSize=10&show=week&pageIndex=","http://www.oschina.net/action/api/blog_list?type=latest&pageSize=10&pageIndex=","http://www.oschina.net/action/api/blog_list?type=recommend&pageSize=10&pageIndex=" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt1 = (RadioButton) findViewById(R.id.bt1);bt2 = (RadioButton) findViewById(R.id.bt2);bt3 = (RadioButton) findViewById(R.id.bt3);bt4 = (RadioButton) findViewById(R.id.bt4);FragmentManager manager = getSupportFragmentManager();FragmentTransaction transaction = manager.beginTransaction();TuiJianFragment fragment = new TuiJianFragment();Bundle bundle = new Bundle();   bundle.putString("path", path[0]);fragment.setArguments(bundle);transaction.replace(R.id.fragmentlayout, fragment);transaction.commit();bt1.setOnClickListener(new MyClick());bt2.setOnClickListener(new MyClick());bt3.setOnClickListener(new MyClick());bt4.setOnClickListener(new MyClick());}class MyClick implements OnClickListener {@Overridepublic void onClick(View v) {FragmentManager manager = getSupportFragmentManager();FragmentTransaction transaction = manager.beginTransaction();switch (v.getId()) {case R.id.bt1:TuiJianFragment fragment1 = new TuiJianFragment();Bundle bundle1 = new Bundle();bundle1.putString("path", path[0]);fragment1.setArguments(bundle1);bt1.setTextColor(0xff00ff00);bt2.setTextColor(0xff000000);bt3.setTextColor(0xff000000);bt4.setTextColor(0xff000000);transaction.replace(R.id.fragmentlayout, fragment1);break;case R.id.bt2:TuiJianFragment fragment2 = new TuiJianFragment();Bundle bundle2 = new Bundle();bundle2.putString("path", path[1]);fragment2.setArguments(bundle2);bt1.setTextColor(0xff000000);bt2.setTextColor(0xff00ff00);bt3.setTextColor(0xff000000);bt4.setTextColor(0xff000000);transaction.replace(R.id.fragmentlayout, fragment2);break;case R.id.bt3:BoKeFragment fragment3 = new BoKeFragment();Bundle bundle3 = new Bundle();bundle3.putString("path", path[2]);fragment3.setArguments(bundle3);bt1.setTextColor(0xff000000);bt2.setTextColor(0xff000000);bt3.setTextColor(0xff00ff00);bt4.setTextColor(0xff000000);transaction.replace(R.id.fragmentlayout, fragment3);break;case R.id.bt4:BoKeFragment fragment4 = new BoKeFragment();Bundle bundle4 = new Bundle();bundle4.putString("path", path[3]);fragment4.setArguments(bundle4);bt1.setTextColor(0xff000000);bt2.setTextColor(0xff000000);bt3.setTextColor(0xff000000);bt4.setTextColor(0xff00ff00);transaction.replace(R.id.fragmentlayout, fragment4);default:break;}transaction.commit();}}}

http://www.ppmy.cn/news/189664.html

相关文章

【UNI-APP】新闻资讯APP总结

环境开发模板语法工作内容1 首页功能开发1.1 初始化数据库1.2. 配置tabbar1.3. 自定义导航栏1.4. 添加小程序的状态栏&#xff08;导航栏适配小程序&#xff09;1.5. 使用字体图标1.6. 选项卡展示1.7. 选项卡数据初始化1.8. 封装数据请求1.9. 选项卡切换1.10.基础卡片视图实现\…

新闻资讯APP调研

今日头条app 今日头条app苹果版国内最大的非四大门户网站之外的手机新闻客户端&#xff0c;今日头条app苹果版免费的新闻客户端&#xff0c;通过手机了解世界上各个角落里正在发生的事情&#xff0c;今日头条免费版参与到世界正在发生的大事情&#xff0c;今日头条app让你拥有世…

【Android】新闻资讯APP的简易实现

src/main/AndroidManifest.xml <?xml version"1.0" encoding"utf-8"?> <manifest xmlns:android"http://schemas.android.com/apk/res/android"package"com.example.myapplication"><uses-permission android:name&…

新闻资讯网站

科技网站&#xff1a; 里面有科技媒体排行 中国科技媒体指数&#xff1a;IT之家&#xff0c;第一&#xff01; - IT之家 测评 ZEALER 官网_Technology Stays True Here 是中国具有影响力的科技视频平台。创始人兼CEO王自如 和 ZEALER 坚持打造兼具行业洞察力与品质感的科技视…

新闻资讯android版

又有一段时间没有更新自己的博客了&#xff0c;前段时间较忙&#xff0c;也没能抽时间来写博客。 这几天公司头让我熟悉一下环境&#xff0c;准备下周给公司做一个自己的新闻资讯&#xff0c;那么首先就要找一个类似的东西来研究一下&#xff0c;看搜狐新闻不错&#xff0c;就按…

cesium-native编译

我相信点进这个博客的都是一些cesium专业人才&#xff0c;这文章只起了一个抛砖引玉的作用&#xff0c;希望各位人才不惜赐教。 Github地址&#xff1a;CesiumGS/cesium-native (github.com) 编译需求&#xff1a;升级公司的3dtile的架构&#xff0c;提高性能 博客目的&…

一维数组名的sizeof计算大小

int main() { //数组名是首元素地址 // 1&#xff0c;sizeof&#xff08;数组名&#xff09;——数组名表示整个数组 // 2&#xff0c;&数组名——表示整个数组 // 除这两种情况外&#xff0c;都是首元素地址 // int a[] { 1,2,3,4 }; printf…

中关村2019高成长企业TOP100发布 百卓网络上榜

近日&#xff0c;由北京中关村高新技术企业协会&#xff08;简称“高企协”&#xff09;、中关村创业投资和股权投资基金协会联合举办的“2019高成长企业TOP100评选”活动结果正式揭晓。该活动是中关村地区最具权威性和公信力的大型公益性评选活动之一。评选旨在发掘优质的、高…