功能实现了viewpager切换fragment,利用xutils网络请求xml数据,进行解析利用的解析为pull解析
这里由于示例,只做了一个界面 导入的jar包有 gson xutils
首先网络请求 有个bean包,
package com.lqy.text.bean;
public class bean {
private String portrait;
private String author;
private String body;
private String pubDate;
public bean() {
super();
// TODO Auto-generated constructor stub
}
public bean(String portrait, String author, String body, String pubDate) {
super();
this.portrait = portrait;
this.author = author;
this.body = body;
this.pubDate = pubDate;
}
@Override
public String toString() {
return "bean [portrait=" + portrait + ", author=" + author + ", body="
+ body + ", pubDate=" + pubDate + "]";
}
public String getPortrait() {
return portrait;
}
public void setPortrait(String portrait) {
this.portrait = portrait;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
}
package com.lqy.text;
import java.util.ArrayList;
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.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener {
private ListView listview;
private ViewPager viewpager;
private TextView text1;
private TextView text2;
private TextView text3;
ArrayList tlist = new ArrayList();
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 控件的查找赋值
findlayout();
// 添加fragment切换的适配器
viewpageradapter();
// 进行滑动的监听
viewpagerscrociew();
// 缓存当前页面的左右两侧的页面,(解决界面左右滑动超过两个信息丢失问题)
viewpager.setOffscreenPageLimit(2);
}
@SuppressWarnings("deprecation")
private void viewpagerscrociew() {
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
// 对textview值进行切换
for (int i = 0; i < tlist.size(); i++) {
if (i == arg0) {
tlist.get(i).setTextColor(Color.RED);
} else {
tlist.get(i).setTextColor(Color.BLACK);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void viewpageradapter() {
viewpager.setAdapter(new FragmentPagerAdapter(
getSupportFragmentManager()) {
@Override
public int getCount() {
// TODO Auto-generated method stub
return tlist.size();
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
// 必须添加v4包的fragment
Fragment fragment = null;
switch (arg0) {
case 0:
fragment = new fragment1();
break;
case 1:
fragment = new fragment2();
break;
case 2:
fragment = new fragment3();
break;
default:
break;
}
return fragment;
}
});
}
private void findlayout() {
// 首先找到需要寻找的控件
viewpager = (ViewPager) findViewById(R.id.viewpager);
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
// 将textview控件添加进textview
tlist.add(text1);
tlist.add(text2);
tlist.add(text3);
// 点击事件的添加
for (TextView t : tlist) {
t.setOnClickListener(this);
}
}
// 点击切换fragment的按钮
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.text1:
viewpager.setCurrentItem(0);
break;
case R.id.text2:
viewpager.setCurrentItem(1);
break;
case R.id.text3:
viewpager.setCurrentItem(2);
break;
default:
break;
}
}
}
mactivity的xml
package com.lqy.text;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
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 com.lqy.text.bean.bean;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class fragment1 extends Fragment {
private View view;
private ListView listview1;
private String url = "http://www.oschina.net/action/api/tweet_list";
ArrayList onelist = new ArrayList();
private XmlPullParser newPullParser;
private int eventType;
private bean b;
boolean refresh = false;
private mybaseadapater adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
listview1 = (ListView) view.findViewById(R.id.listview1);
HttpUtils http = new HttpUtils();
http.send(HttpMethod.GET, url, new RequestCallBack() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo arg0) {
// TODO Auto-generated method stub
String info = arg0.result;
System.out.println(info + "=========");
// 读取xml的流
final ByteArrayInputStream input = new ByteArrayInputStream(
info.getBytes());
// 利用方法解析,放入集合
ArrayList twolist = startannlye(input);
System.out.println(twolist + "00000000000000000000");
if (refresh) {
onelist.clear();
}
onelist.addAll(twolist);
if (adapter == null) {
adapter = new mybaseadapater(getActivity(), onelist);
listview1.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
});
}
private ArrayList startannlye(ByteArrayInputStream input) {
// TODO Auto-generated method stub
ArrayList threelist = new ArrayList();
try {
// 开
newPullParser = Xml.newPullParser();
newPullParser.setInput(input, "utf-8");
eventType = newPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String name = newPullParser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if ("tweet".equals(name)) {
b = new bean();
} else if ("portrait".equals(name)) {
b.setPortrait(newPullParser.nextText());
} else if ("body".equals(name)) {
b.setBody(newPullParser.nextText());
} else if ("pubData".equals(name)) {
b.setPubDate(newPullParser.nextText());
} else if ("author".equals(name)) {
b.setAuthor(newPullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if ("tweet".equals(name)) {
threelist.add(b);
b = null;
}
break;
default:
break;
}
eventType = newPullParser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return threelist;
}
}
package com.lqy.text;
import java.util.ArrayList;
import com.lidroid.xutils.BitmapUtils;
import com.lqy.text.bean.bean;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
@SuppressLint("ViewHolder")
public class mybaseadapater extends BaseAdapter {
private Context context;
private ArrayList list;
private View inflate;
private TextView author;
private TextView body;
private TextView data;
private ImageView image;
public mybaseadapater(Context context, ArrayList list) {
// TODO Auto-generated constructor stub
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
inflate = View.inflate(context, R.layout.item, null);
author = (TextView) inflate.findViewById(R.id.auther);
body = (TextView) inflate.findViewById(R.id.writer);
data = (TextView) inflate.findViewById(R.id.data);
image = (ImageView) inflate.findViewById(R.id.image);
author.setText(list.get(arg0).getAuthor());
body.setText(list.get(arg0).getBody());
data.setText(list.get(arg0).getPubDate());
BitmapUtils map = new BitmapUtils(context);
map.display(image, list.get(arg0).getPortrait());
return inflate;
}
}