Fragement

Posted 总有奸臣想害朕

tags:

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

package com.exmple.frage;

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

import com.bawei.shuaxi.PullToRefreshLayout;
import com.bawei.shuaxi.PullToRefreshLayout.OnRefreshListener;
import com.bawei.shuaxi.PullableListView;
import com.exmple.apa.NewShuju;
import com.exmple.help.MyHelp;
import com.exmple.jinritoutiao.R;
import com.exmple.jinritoutiao.XiangqingActivity;
import com.exmple.net.NetUtil;
import com.exmple.tools.Urlclass;
import com.exmple.vo.MyData;
import com.exmple.vo.Super;
import com.google.gson.Gson;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment_2 extends Fragment {

	Handler ha = new Handler() {

		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				String ma = (String) msg.obj;
				Gson gs = new Gson();
				Super fromJson = gs.fromJson(ma, Super.class);
				list = fromJson.getData();
				lists = list;
				update();
			}
			if (msg.what == 2) {
				String ma = (String) msg.obj;
				Gson gs = new Gson();
				Super fromJson = gs.fromJson(ma, Super.class);
				list = fromJson.getData();
				lists.addAll(list);
				update();
			}

			// lists.addAll(list);

			listview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					Intent ints = new Intent(getActivity(),
							XiangqingActivity.class);
					ints.putExtra("info1", list.get(position).getUrl());
					ints.putExtra("im", list.get(position).getDisplay_url());
					ints.putExtra("title", list.get(position).getTitle());

					startActivity(ints);

				}
			});

		}

	};
	int page=0;
	private TextView category;
	private List<MyData> list;
	private List<MyData> lists = new ArrayList<MyData>();
	private PullableListView listview;

	private NewShuju ne;

	private SQLiteDatabase sql;

	private View view;

	private PullToRefreshLayout layout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (view == null) {
			view = inflater.inflate(R.layout.news_fragment, null);
			layout = (PullToRefreshLayout) view.findViewById(R.id.layout);

		}
		ViewGroup vp = (ViewGroup) view.getParent();
		if (vp != null) {
			vp.removeAllViews();

		}

		MyHelp myHelp = new MyHelp(getActivity(), "navs", null, 1);
		sql = myHelp.getWritableDatabase();
		listview = (PullableListView) view.findViewById(R.id.listview1);

		category = (TextView) view.findViewById(R.id.category_line);
		Date();
		setJson();

		layout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
				ha.postDelayed(new Runnable() {
					@Override
					public void run() {

						setJson();
						pullToRefreshLayout
								.refreshFinish(PullToRefreshLayout.SUCCEED);
					}

				}, 1000);

				pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);

			}

			@Override
			public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {

				ha.postDelayed(new Runnable() {

					@Override
					public void run() {

						// setJson();
						Bundle b = getArguments();
						if (b != null) {
							String string1 = b.getString("text");
							System.out.println(string1 + "0000000");

							// 调用方法
							HashMap<String, String> urls = Urlclass.getUrl();
							// 获取对应的 URL
							final String url = urls.get(string1);

							new Thread() {
								public void run() {

									String str1 = NetUtil.getUrl(url+page);
									ha.sendMessage(ha.obtainMessage(2, str1));

								};
							}.start();

						}
						// 千万别忘了告诉控件加载完毕了哦!
						pullToRefreshLayout
								.loadmoreFinish(PullToRefreshLayout.SUCCEED);

					}
				}, 1000);
			}
		});

		return view;
	}

	private void setJson() {
		Bundle b = getArguments();
		if (b != null) {
			String string1 = b.getString("text");
			System.out.println(string1 + "0000000");

			// 调用方法
			HashMap<String, String> urls = Urlclass.getUrl();
			// 获取对应的 URL
			final String url = urls.get(string1);

			new Thread() {
				public void run() {

					String str1 = NetUtil.getUrl(url);
					ha.sendMessage(ha.obtainMessage(1, str1));

				};
			}.start();

		}

	}

	private void Date() {
		Calendar c = Calendar.getInstance();
		// 获取当前年份
		String mYear = String.valueOf(c.get(Calendar.YEAR));
		// 获取当前月份
		String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);
		// 获取当前月份的日期号码
		String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
		// 获取当前星期 他获得的是数字1 2 3 4 5 6所以根据数字换一下
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "天";
		} else if ("2".equals(mWay)) {
			mWay = "一";
		} else if ("3".equals(mWay)) {
			mWay = "二";
		} else if ("4".equals(mWay)) {
			mWay = "三";
		} else if ("5".equals(mWay)) {
			mWay = "四";
		} else if ("6".equals(mWay)) {
			mWay = "五";
		} else if ("7".equals(mWay)) {
			mWay = "六";
		}
		category.setText(mYear + "." + mMonth + "." + mDay + "             星期"
				+ mWay + "              " + "今天");

	}

	private void update() {
		if (ne == null) {
			ne = new NewShuju(list, getActivity(), listview);
			listview.setAdapter(ne);

		} else {
			ne.UpDataAdapter(lists);
		}

	};

}

  

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

java Android Swipe页面(fragement解决方案,较好)

FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity

在PreferenceAcitity中使用Fragement时避免额外的Left和RightPadding

我的长大app开发教程第一弹:Fragement布局

Android第一行代码-Fragment

Android第一行代码-Fragment