Xml pull解析,XUtils网络请求

用radioGroup和radioButton实现四个选项卡,“资讯”、“热点”、“博客”、“推荐”

使用Xutils方式实现请求网络接口

解析XML数据,封装到集合里面,用XListView显示解析数据,实现下拉刷新,上拉加载,  需要添加XListView依赖包

<span style="font-size:18px;">activity_main:

<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:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="40dp"
         >

        <RadioButton
            android:id="@+id/radio0"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="fill_parent"
            android:textColor="#00ff00"
            android:textSize="20sp"
            android:text="资讯" />

        <RadioButton
            android:id="@+id/radio1"
            android:button="@null"
            android:textSize="20sp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="fill_parent"
            android:text="热点" />

        <RadioButton
            android:id="@+id/radio2"
            android:button="@null"
            android:textSize="20sp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="fill_parent"
            android:text="博客" />
        
        <RadioButton
            android:id="@+id/radio3"
            android:button="@null"
            android:textSize="20sp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="fill_parent"
            android:text="推荐" />
    </RadioGroup>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/viewPager"
        />

</LinearLayout></span>


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" >
	 <me.maxwin.view.XListView
	     android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/xListView1"
	     ></me.maxwin.view.XListView>

</LinearLayout>

Bean

package com.bwie.test2.bean;

public class Zixun {
	private String title;
	private String body;
	private String pubDate;
	private String author;
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	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;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public Zixun(String title, String body, String pubDate, String author) {
		super();
		this.title = title;
		this.body = body;
		this.pubDate = pubDate;
		this.author = author;
	}
	public Zixun() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Zixun [title=" + title + ", body=" + body + ", pubDate="
				+ pubDate + ", author=" + author + "]";
	}
	
}

接下来是Fragment.java中的代码,一共有四个Fragment,代码一样,唯一区别是请求数据的地址不同

package com.bwie.test2.fragment;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;

import com.bwie.test2.R;
import com.bwie.test2.adapter.MyBaseAdapter;
import com.bwie.test2.bean.Zixun;
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.support.v4.app.Fragment;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Fragment1 extends Fragment <span style="color:#FF0000;">implements IXListViewListener</span>{
	private XListView xlistView;
	boolean isRefresh = false;
	ArrayList<Zixun> mZxList=new ArrayList<Zixun>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment1, container, false);
		xlistView = (XListView) view.findViewById(R.id.xListView1);
		return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		xlistView.setPullLoadEnable(true);
		xlistView.setXListViewListener(this);
		myBaseAdapter=null;
		questNet();
	}
	private int index=0;
	private Zixun zixun;
	private MyBaseAdapter myBaseAdapter;
	private void questNet() {
		String path="http://www.oschina.net/action/api/news_list?pageIndex="+index;
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {

			

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				ByteArrayInputStream arrayInputStream=new ByteArrayInputStream(result.getBytes());
				ArrayList<Zixun> startAnalyze = startAnalyze(arrayInputStream);
				if (isRefresh) {
					mZxList.clear();
				}
				mZxList.addAll(startAnalyze);
				if (myBaseAdapter == null) {
					myBaseAdapter = new MyBaseAdapter(getActivity(), mZxList);
					xlistView.setAdapter(myBaseAdapter);
				} else {
					myBaseAdapter.notifyDataSetChanged();
				}
				onLoad();
			}
		});
	}
	protected ArrayList<Zixun> startAnalyze(ByteArrayInputStream arrayInputStream) {
		ArrayList<Zixun> zxList = new ArrayList<Zixun>();
		try {
			XmlPullParser newPullParser = Xml.newPullParser();
			newPullParser.setInput(arrayInputStream, "utf-8");
			int eventType = newPullParser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String name = newPullParser.getName();
				switch (eventType) {
				case XmlPullParser.START_TAG:
					if ("news".equals(name)) {
						zixun = new Zixun();
					} else if ("title".equals(name)) {
						zixun.setTitle(newPullParser.nextText());
					} else if ("body".equals(name)) {
						zixun.setBody(newPullParser.nextText());
					} else if ("pubDate".equals(name)) {
						zixun.setPubDate(newPullParser.nextText());
					} else if ("author".equals(name)) {
						zixun.setAuthor(newPullParser.nextText());
					} 
					break;

				case XmlPullParser.END_TAG:
					if ("news".equals(name)) {
						zxList.add(zixun);
						zixun = null;
					}
					break;

				default:
					break;
				}
				eventType = newPullParser.next();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return zxList;
	}
	@Override
	public void onRefresh() {
		index = 0;
		isRefresh = true;
	}
	@Override
	public void onLoadMore() {
		index++;
		isRefresh = false;
		// 重新请求
		questNet();
	}
	private void onLoad() {
		xlistView.stopRefresh();
		xlistView.stopLoadMore();
		xlistView.setRefreshTime("刚刚");
	}
}
fragment1_item.xml布局,XListView的布局

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:padding="10dp"
        android:textStyle="bold"
        android:id="@+id/tv_title"
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_body"
        />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        >
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_author"
            />
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_time"
            android:layout_marginLeft="20dp"
            />
    </LinearLayout>
    

</LinearLayout>
BaseAdapter适配器

package com.bwie.test2.adapter;

import java.util.ArrayList;

import com.bwie.test2.R;
import com.bwie.test2.bean.Zixun;

import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyBaseAdapter extends BaseAdapter {

	private FragmentActivity activity;
	private ArrayList<Zixun> mZxList;

	public MyBaseAdapter(FragmentActivity activity, ArrayList<Zixun> mZxList) {
		this.activity=activity;
		this.mZxList=mZxList;
	}

	@Override
	public int getCount() {
		return mZxList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = View.inflate(activity, R.layout.fragment1_item, null);
		TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
		TextView tv_body = (TextView) view.findViewById(R.id.tv_body);
		TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
		TextView tv_author = (TextView) view.findViewById(R.id.tv_author);
		tv_title.setText(mZxList.get(position).getTitle());
		tv_body.setText(mZxList.get(position).getBody());
		tv_time.setText(mZxList.get(position).getPubDate());
		tv_author.setText(mZxList.get(position).getAuthor());
		return view;
	}

}
MainActivity中设置title与ViewPager关联,滑动ViewPager,title相应的改变颜色,点击按钮,跳到对应的ViewPager的页码

package com.bwie.test2;

import java.util.ArrayList;
import java.util.List;

import com.bwie.test2.fragment.Fragment1;
import com.bwie.test2.fragment.Fragment2;
import com.bwie.test2.fragment.Fragment3;
import com.bwie.test2.fragment.Fragment4;

import android.os.Bundle;
import android.app.Activity;
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.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends FragmentActivity implements OnClickListener{

	private RadioGroup group;
	private RadioButton button;
	private RadioButton button1;
	private RadioButton button2;
	private RadioButton button3;
	private ViewPager viewPager;
	private List<Button> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		group = (RadioGroup) findViewById(R.id.radioGroup1);
		button = (RadioButton) findViewById(R.id.radio0);
		button1 = (RadioButton) findViewById(R.id.radio1);
		button2 = (RadioButton) findViewById(R.id.radio2);
		button3 = (RadioButton) findViewById(R.id.radio3);
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		list = new ArrayList<Button>();
		list.add(button);
		list.add(button1);
		list.add(button2);
		list.add(button3);
		for (Button button : list) {
			button.setOnClickListener(this);
		}
		viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
			
			@Override
			public int getCount() {
				return 4;
			}
			
			@Override
			public Fragment getItem(int arg0) {
				Fragment fragment = null;
				switch (arg0) {
				case 0:
					fragment=new Fragment1();
					break;
				case 1:
					fragment=new Fragment2();
					break;
				case 2:
					fragment=new Fragment3();
					break;
				case 3:
					fragment=new Fragment4();
					break;

				default:
					break;
				}
				return fragment;
			}
		});
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				for (int i = 0; i < list.size(); i++) {
					if (position == i) {
						list.get(i).setTextColor(Color.GREEN);

					} else {
						list.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

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.radio0 :
			button.setTextColor(Color.GREEN);
			button1.setTextColor(Color.BLACK);
			button2.setTextColor(Color.BLACK);
			button3.setTextColor(Color.BLACK);
			viewPager.setCurrentItem(0);
			break;
		case R.id.radio1 :
			button.setTextColor(Color.BLACK);
			button1.setTextColor(Color.GREEN);
			button2.setTextColor(Color.BLACK);
			button3.setTextColor(Color.BLACK);
			viewPager.setCurrentItem(1);
			break;
		case R.id.radio2 :
			button.setTextColor(Color.BLACK);
			button1.setTextColor(Color.BLACK);
			button2.setTextColor(Color.GREEN);
			button3.setTextColor(Color.BLACK);
			viewPager.setCurrentItem(2);
			break;
		case R.id.radio3 :
			button.setTextColor(Color.BLACK);
			button1.setTextColor(Color.BLACK);
			button2.setTextColor(Color.BLACK);
			button3.setTextColor(Color.GREEN);
			viewPager.setCurrentItem(3);
			break;

		default:
			break;
		}
	}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值