主页面布局
<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;
@Override
public 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();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public 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;
@Override
public 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>() {
@Override
public void onFailure(HttpException arg0,
String arg1) {
System.out.println("解析失败");
}
@Override
public 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();
}
}
@Override
public void onRefresh() {
path=path+1;
type=true;
getXML();
close();
}
public void close(){
listView.stopLoadMore();
listView.stopRefresh();
}
@Override
public 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;
@Override
public 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>() {
// 解析失败
@Override
public void onFailure(HttpException arg0,
String arg1) {
System.out.println("解析失败");
}
// 解析成功
@Override
public 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();
}
}
@Override
public void onRefresh() {
str=path+1;
type=true;
getXML();
close();
}
public void close(){
listView.stopRefresh();
listView.stopLoadMore();
}
@Override
public 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=" };
@Override
protected 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 {
@Override
public 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();
}
}
}