Xml游标

Posted 总有奸臣想害朕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xml游标相关的知识,希望对你有一定的参考价值。

Mainactivity

package com.exmple.xmlstream;

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

import com.exmple.unet.NetUtil;
import com.exmple.vo.MyNewslist;
import com.exmple.vo.Super;
import com.thoughtworks.xstream.XStream;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.app.Activity;
import android.graphics.Color;

import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
		OnCheckedChangeListener {

	private ViewPager vp;

	private RadioGroup rg;
	String[] str = new String[] { "资讯", "热点", "博客", "推荐" };

	private List<Fragment> list;

	private RadioButton rb1;

	private RadioButton rb2;

	private RadioButton rb3;

	private RadioButton rb4;

	private LinearLayout line;

	private int width;

	private List<TextView> list_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		width = outMetrics.widthPixels;
		/*int heightPixels = outMetrics.heightPixels;*/
		rg = (RadioGroup) findViewById(R.id.rg);
		vp = (ViewPager) findViewById(R.id.vp);
		rb1 = (RadioButton) findViewById(R.id.button1);
		rb2 = (RadioButton) findViewById(R.id.button2);
		rb3 = (RadioButton) findViewById(R.id.button3);
		rb4 = (RadioButton) findViewById(R.id.button4);
		line = (LinearLayout) findViewById(R.id.line);
		rb1.setText(str[0]);
		rb2.setText(str[1]);
		rb3.setText(str[2]);
		rb4.setText(str[3]);
		getData();
		setColumn();
		FragmentManager fm = getSupportFragmentManager();
		setYoubiao(vp.getCurrentItem());

		Viewpage1 fa = new Viewpage1(fm, list);
		vp.setAdapter(fa);
		vp.setCurrentItem(0);
		rb1.setTextColor(Color.BLUE);

		rg.setOnCheckedChangeListener(this);
		vp.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {

				switch (arg0) {
				case 0:
					setYoubiao(arg0);
					rb1.setTextColor(Color.BLUE);
					rb2.setTextColor(Color.BLACK);
					rb3.setTextColor(Color.BLACK);
					rb4.setTextColor(Color.BLACK);
					break;

				case 1:
					setYoubiao(arg0);
					rb1.setTextColor(Color.BLACK);
					rb2.setTextColor(Color.BLUE);
					rb3.setTextColor(Color.BLACK);
					rb4.setTextColor(Color.BLACK);
					break;
				case 2:
					setYoubiao(arg0);
					rb1.setTextColor(Color.BLACK);
					rb2.setTextColor(Color.BLACK);
					rb3.setTextColor(Color.BLUE);
					rb4.setTextColor(Color.BLACK);
					break;
				case 3:
					setYoubiao(arg0);
					rb1.setTextColor(Color.BLACK);
					rb2.setTextColor(Color.BLACK);
					rb3.setTextColor(Color.BLACK);
					rb4.setTextColor(Color.BLUE);
					break;
				}

			}

			@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

			}
		});
		// http://www.oschina.net/action/api/news_list
		// http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20&pass=

	}

	private void setColumn() {

		list_tv = new ArrayList<TextView>();
		// for循环
		for (int i = 0; i < str.length; i++) {

			// 创建textview
			TextView tv = new TextView(this);
			tv.setBackgroundColor(Color.GREEN);
			LinearLayout.LayoutParams params_1 = new LayoutParams(width / 4, 10);
			tv.setVisibility(View.INVISIBLE);
			line.addView(tv, params_1);

			// 收集textview,即游标
			list_tv.add(tv);

		}

	}

	private void setYoubiao(int arg0) {
		TextView textView = list_tv.get(arg0);

		// 将当前的游标显示
		textView.setVisibility(View.VISIBLE);

		// 将其他的游标隐藏
		for (int i = 0; i < list_tv.size(); i++) {
			TextView textView2 = list_tv.get(i);

			if (textView2 != textView) {
				textView2.setVisibility(View.INVISIBLE);
			}
		}

	}

	private List<Fragment> getData() {
		list = new ArrayList<Fragment>();
		for (int i = 0; i < str.length; i++) {
			F1 f1 = new F1();
			Bundle bu = new Bundle();
			bu.putString("text", str[i]);
			f1.setArguments(bu);
			list.add(f1);

		}
		return list;

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.button1:
			vp.setCurrentItem(0);

			break;

		case R.id.button2:
			vp.setCurrentItem(1);

			break;

		case R.id.button3:
			vp.setCurrentItem(2);

			break;

		case R.id.button4:
			vp.setCurrentItem(3);

			break;

		}

	}

}

  适配器

package com.exmple.xmlstream;

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

import com.exmple.ap.Apater;
import com.exmple.ap.Apater1;
import com.exmple.bovo.BoSuper;
import com.exmple.bovo.MyBlogs;
import com.exmple.bovo.Myblog;
import com.exmple.tong.PullToRefreshLayout;
import com.exmple.tong.PullToRefreshLayout.OnRefreshListener;
import com.exmple.tong.PullableListView;
import com.exmple.tool.UserTool;
import com.exmple.unet.NetUtil;
import com.exmple.vo.MyNew;
import com.exmple.vo.MyNewslist;
import com.exmple.vo.Super;
import com.thoughtworks.xstream.XStream;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class F1 extends Fragment {
	Handler ha = new Handler() {

		public void handleMessage(Message msg) {
			int i = msg.what;
		
			

			if (i == 1) {
				String ma = msg.obj.toString();
				XStream xs = new XStream();
				xs.processAnnotations(Super.class);
				Super fromXML = (Super) xs.fromXML(ma);
				List<MyNew> newslist = fromXML.getNewslist().getNews();
				System.out.println(newslist.toString());
				if (page == 1) {
					list.clear();
				}
				list.addAll(newslist);
				update();

			}
			if (i == 2) {
				String ma = msg.obj.toString();
				XStream xs1 = new XStream();
				xs1.processAnnotations(BoSuper.class);
				BoSuper bo = (BoSuper) xs1.fromXML(ma);
				List<Myblog> blog = bo.getBlogs().getBlog();
				if (page == 1) {
					list1.clear();
				}
				list1.addAll(blog);
				update1();
			}

		};
	};
	private Apater ap;
	private Apater1 ap1;
	private List<MyNew> list = new ArrayList<MyNew>();
	private List<Myblog> list1 = new ArrayList<Myblog>();
	int page = 1;
	private PullableListView plistview;
	private PullToRefreshLayout playout;
	private String s;
	private String url;
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		Bundle bu = getArguments();
		s = bu.getString("text");
		HashMap<String, String> map = UserTool.getUrl();
		url = map.get(s);
		getData();

		if (view == null) {
			view = View.inflate(getActivity(), R.layout.f1, null);
			playout = (PullToRefreshLayout) view.findViewById(R.id.layout);

		}
		ViewGroup mParent = (ViewGroup) view.getParent();

		if (mParent != null) {
			mParent.removeView(view);
		}
		playout = (PullToRefreshLayout) view.findViewById(R.id.layout);
		plistview = (PullableListView) view.findViewById(R.id.listview);
		playout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
				ha.postDelayed(new Runnable() {
					@Override
					public void run() {
						page = 1;
						getData();
						// 千万别忘了告诉控件刷新完毕了哦!
						pullToRefreshLayout
								.refreshFinish(PullToRefreshLayout.SUCCEED);
					}

				}, 1000);

			}

			@Override
			public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
				ha.postDelayed(new Runnable() {
					@Override
					public void run() {
						page++;
						getData();
						// 千万别忘了告诉控件刷新完毕了哦!
						pullToRefreshLayout
								.loadmoreFinish(PullToRefreshLayout.SUCCEED);
					}

				}, 1000);

			}
		});

		return view;
	}

	private void getData() {

		new Thread() {
			public void run() {

				if (s.equals("资讯") || s.equals("热点")) {
					String str = NetUtil.getUrl(url,page);
					ha.sendMessage(ha.obtainMessage(1, str));
				}

				if (s.equals("博客") || s.equals("推荐")) {
					
					String str = NetUtil.getUrl(url,page);
					ha.sendMessage(ha.obtainMessage(2, str));
				}

			};
		}.start();

	}

	public void update() {
		// 判断ba是否为空
		if (ap == null) {
			ap = new Apater(list, getActivity());
			// 为lv添加适配器
			plistview.setAdapter(ap);
		} else {
			// 刷新适配器
			ap.UpDataAdapter(list);
		}
	}

	public void update1() {
		// 判断ba是否为空
		if (ap1 == null) {
			ap1 = new Apater1(list1, getActivity());
			// 为lv添加适配器
			plistview.setAdapter(ap1);
		} else {
			// 刷新适配器
			ap1.UpDataAdapter(list1);
		}
	}

}

  

以上是关于Xml游标的主要内容,如果未能解决你的问题,请参考以下文章

xml Eclipse模板(代码片段)检查参数并最终抛出IllegalArgumentException

需要示例代码片段帮助

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

从 XML 声明片段获取 XML 编码:部分内容解析不支持 XmlDeclaration

[AndroidStudio]_[初级]_[配置自动完成的代码片段]