Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

king

編輯:關於Android編程

測試demo:(布局顏色需要改變)Fragment之間的滑動 網路請求解析

bean 1

import java.util.List;

public class Data1 {

	public List data;
	
	public class MyData1{
		
		public String FROMNAME;
		public String ID;
		public String IMAGEURL;
		public String RN;
		public String SHOWTIME;
		public String SUBTITLE;
		public String TITLE;
	}
}

 

bean 2

 

import java.util.List;

public class Data2 {

	public List data;
	
	public class MyData2{
		
		public String ID;
		public String RN;
		public String SHOWTIME;
		public String SUBTITLE;
		public String TITLE;
	}
}

 

MainActivity.class

 

 

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.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.view.Window;
import android.widget.TextView;
import fragment.Fragment1;
import fragment.Fragment2;
import fragment.Fragment3;

public class MainActivity extends FragmentActivity implements OnClickListener {

	private TextView t1;
	private TextView t2;
	private TextView t3;
	private ViewPager viewPager;
	private TextView t11;
	private TextView t22;
	private TextView t33;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去掉頭部
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		//初始化組件
		initView();

	}

	/**
	 * 初始化組件
	 */

	@SuppressWarnings("deprecation")
	public void initView() {
		t1 = (TextView) findViewById(R.id.t1);
		t2 = (TextView) findViewById(R.id.t2);
		t3 = (TextView) findViewById(R.id.t3);
		t11 = (TextView) findViewById(R.id.t11);
		t22 = (TextView) findViewById(R.id.t22);
		t33 = (TextView) findViewById(R.id.t33);
		viewPager = (ViewPager) findViewById(R.id.viewPager);

		// 設置監聽
		t1.setOnClickListener(this);
		t2.setOnClickListener(this);
		t3.setOnClickListener(this);

		t11.setOnClickListener(this);
		t22.setOnClickListener(this);
		t33.setOnClickListener(this);
		
		//給ViewPager設置適配器
		viewPager.setAdapter(new FragmentPagerAdapter(
				getSupportFragmentManager()) {

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return 3;
			}

			@Override
			public Fragment getItem(int arg0) {
				//根據ViewPager 索引 返回相應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;
			}
		});
		
		//給ViewPager設置滑動監聽
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				//根據position改變游標狀態
				switch (position) {
				case 0:
					t11.setBackgroundColor(Color.RED);
					t22.setBackgroundColor(Color.parseColor("#00000000"));
					t33.setBackgroundColor(Color.parseColor("#00000000"));
					break;
				case 1:
					t22.setBackgroundColor(Color.RED);
					t11.setBackgroundColor(Color.parseColor("#00000000"));
					t33.setBackgroundColor(Color.parseColor("#00000000"));
					break;
				case 2:
					t33.setBackgroundColor(Color.RED);
					t22.setBackgroundColor(Color.parseColor("#00000000"));
					t11.setBackgroundColor(Color.parseColor("#00000000"));
					break;
				default:
					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

			}
		});
	}

	/**
	 * 點擊事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//點擊選項卡 跳轉到對應的Fragment頁
		case R.id.t1:
			viewPager.setCurrentItem(0);
			break;
		case R.id.t2:
			viewPager.setCurrentItem(1);
			break;
		case R.id.t3:
			viewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
	}

}


 

Fragment1

 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import bean.Data1;
import bean.Data1.MyData1;

import com.bwie.test.R;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;

public class Fragment1 extends Fragment {

	private View view;
	private ListView listView1;
	private final String path = "http://www.93.gov.cn/93app/data.do";// 接口
	private String resultJson;
	private Context context;
	private List datas = new ArrayList();
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				//給listView 設置數據  更新UI
				resultJson = (String) msg.obj;
				Gson gson = new Gson();
				Data1 fromJson = gson.fromJson(resultJson, Data1.class);
				datas.addAll(fromJson.data);
				listView1.setAdapter(new MyBaseAdapter1());
				break;

			default:
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment1, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		context = getActivity();
		listView1 = (ListView) view.findViewById(R.id.listView1);

		new Thread() {
			public void run() {
				requestDataByData(0, 0);

			}
		}.start();
	}

	/**
	 * 請求網絡數據
	 */

	public void requestDataByData(final int channelId, final int startNum) {

		try {
			URL url = new URL(path);
			String entity = "channelId=" + channelId + "&startNum=" + startNum;
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			OutputStream outputStream = conn.getOutputStream();
			outputStream.write(entity.getBytes());
			int responseCode = conn.getResponseCode();
			if (responseCode == 200) {
				ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
				InputStream is = conn.getInputStream();
				//將流轉化為字符換
				int len = 0;
				byte[] buffer = new byte[1024];
				while ((len = is.read(buffer)) != -1) {
					arrayOutputStream.write(buffer, 0, len);
				}
				//將結果發送的Handler
				handler.obtainMessage(0, arrayOutputStream.toString("utf-8"))
						.sendToTarget();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class MyBaseAdapter1 extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datas.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 arg1, ViewGroup arg2) {
			View view2 = View.inflate(context, R.layout.list_item1, null);
			TextView title1 = (TextView) view2.findViewById(R.id.title1);
			TextView subTitle1 = (TextView) view2.findViewById(R.id.subTitle);
			TextView time1 = (TextView) view2.findViewById(R.id.time1);
			TextView author1 = (TextView) view2.findViewById(R.id.author1);
			ImageView img = (ImageView) view2.findViewById(R.id.img);
			title1.setText(datas.get(position).TITLE);
			subTitle1.setText(datas.get(position).SUBTITLE);
			time1.setText(datas.get(position).SHOWTIME);
			author1.setText(datas.get(position).FROMNAME);
			if(TextUtils.isEmpty(datas.get(position).IMAGEURL)){
				img.setVisibility(View.GONE);
			}else{
				BitmapUtils bitmapUtils = new BitmapUtils(context);
				bitmapUtils.display(img, datas.get(position).IMAGEURL);
			}
			
			return view2;
		}

	}

}


 

Fragment2

 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import bean.Data1;
import bean.Data1.MyData1;
import bean.Data2;
import bean.Data2.MyData2;

import com.bwie.test.R;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;

public class Fragment2 extends Fragment {

	private View view;
	private ListView listView2;
	private final String path = "http://www.93.gov.cn/93app/data.do";// 接口
	private String resultJson;
	private Context context;
	private List datas = new ArrayList();
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				//給listView 設置數據  更新UI
				resultJson = (String) msg.obj;
				Log.i("result", resultJson);
				Gson gson = new Gson();
				Data2 fromJson = gson.fromJson(resultJson, Data2.class);
				datas.addAll(fromJson.data);
				listView2.setAdapter(new MyBaseAdapter1());
				break;

			default:
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment2, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		//獲得上下文對象
		context = getActivity();
		listView2 = (ListView) view.findViewById(R.id.listView2);

		//開啟子線程  進行請求網絡 操作
		new Thread() {
			public void run() {
				requestDataByData(1, 21);

			}
		}.start();
	}

	/**
	 * 請求網絡數據   POST
	 */

	public void requestDataByData(final int channelId, final int startNum) {

		try {
			URL url = new URL(path);
			String entity = "channelId=" + channelId + "&startNum=" + startNum;
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			OutputStream outputStream = conn.getOutputStream();
			outputStream.write(entity.getBytes());
			int responseCode = conn.getResponseCode();
			if (responseCode == 200) {
				ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
				InputStream is = conn.getInputStream();
				
				//將流轉化為字符串
				int len = 0;
				byte[] buffer = new byte[1024];
				while ((len = is.read(buffer)) != -1) {
					arrayOutputStream.write(buffer, 0, len);
				}

				handler.obtainMessage(0, arrayOutputStream.toString("utf-8"))
						.sendToTarget();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//自定義適配器
	class MyBaseAdapter1 extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datas.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 arg1, ViewGroup arg2) {
			View view2 = View.inflate(context, R.layout.list_item2, null);
			TextView title2 = (TextView) view2.findViewById(R.id.title2);
			TextView subTitle2 = (TextView) view2.findViewById(R.id.subTitle2);
			TextView time2 = (TextView) view2.findViewById(R.id.time2);
			title2.setText(datas.get(position).TITLE);
			subTitle2.setText(datas.get(position).SUBTITLE);
			time2.setText(datas.get(position).SHOWTIME);

			return view2;
		}

	}

}

 

 

 

Fragment3

 

 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import bean.Data1;
import bean.Data1.MyData1;
import bean.Data2;
import bean.Data2.MyData2;

import com.bwie.test.R;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;

public class Fragment3 extends Fragment {

	private View view;
	private ListView listView2;
	private final String path = "http://www.93.gov.cn/93app/data.do";// 接口
	private String resultJson;
	private Context context;
	private List datas = new ArrayList();
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				//給listView 設置數據  更新UI
				resultJson = (String) msg.obj;
				Log.i("result", resultJson);
				Gson gson = new Gson();
				Data2 fromJson = gson.fromJson(resultJson, Data2.class);
				datas.addAll(fromJson.data);
				listView2.setAdapter(new MyBaseAdapter1());
				break;

			default:
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment2, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		// 獲得上下文對象
		context = getActivity();
		listView2 = (ListView) view.findViewById(R.id.listView2);
		// 開啟一個子線程 進行請求網絡操作
		new Thread() {
			public void run() {
				requestDataByData(3, 21);

			}
		}.start();
	}

	/**
	 * 請求網絡數據
	 */

	public void requestDataByData(final int channelId, final int startNum) {

		try {
			URL url = new URL(path);
			String entity = "channelId=" + channelId + "&startNum=" + startNum;
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			OutputStream outputStream = conn.getOutputStream();
			outputStream.write(entity.getBytes());
			int responseCode = conn.getResponseCode();
			if (responseCode == 200) {
				ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
				InputStream is = conn.getInputStream();
				//將流轉化為字符串
				int len = 0;
				byte[] buffer = new byte[1024];
				while ((len = is.read(buffer)) != -1) {
					arrayOutputStream.write(buffer, 0, len);
				}

				handler.obtainMessage(0, arrayOutputStream.toString("utf-8"))
						.sendToTarget();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class MyBaseAdapter1 extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datas.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 arg1, ViewGroup arg2) {
			View view2 = View.inflate(context, R.layout.list_item2, null);
			TextView title2 = (TextView) view2.findViewById(R.id.title2);
			TextView subTitle2 = (TextView) view2.findViewById(R.id.subTitle2);
			TextView time2 = (TextView) view2.findViewById(R.id.time2);
			title2.setText(datas.get(position).TITLE);
			subTitle2.setText(datas.get(position).SUBTITLE);
			time2.setText(datas.get(position).SHOWTIME);

			return view2;
		}

	}

}

activity_main



    

        

            

            
        

        

            

            
        

        

            

            
        
    

    
    



 

fragment1布局

 



    

    


fragment2布局

 

 




    
    


 

fragment3布局

 

 



    


list_item1適配器布局

 

 




    

    
        

    
        
            
            
            
        
    


 

 

list_item2適配器布局

 

 




     

    
        

    
        
            
             
            
        
    

 

****************************************記得加上網絡權限************************
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved