Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android ViewPager學習筆記1

android ViewPager學習筆記1

編輯:關於Android編程

1、客戶端代碼

1.1第一個界面

public class MainActivity extends FragmentActivity {
	private ViewPager viewPager;
	private List items;
	private List titles;
	private MyFragmentPagerAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewPager = (ViewPager) this.findViewById(R.id.viewpager);
		items = new ArrayList();
		Fragment1 fragment1 = new Fragment1();
		Fragment2 fragment2 = new Fragment2();
		items.add(fragment1);
		items.add(fragment2);
		titles = new ArrayList();
		titles.add("第一個頁面");
		titles.add("第二個頁面");
		adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return items.get(arg0);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return titles.get(position);
		}

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

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

對應的布局:



    

        
        
        
    


1.2第二個界面

public class Fragment1 extends ListFragment implements OnScrollListener {
	private String path = "http://172.22.122.1:8080/viewpager/servlet/CityAction?pageNo=";
	private static int pageNo = 1;// 默認是第一頁
	private List total_list;
	private MyAdapter adapter;
	private boolean is_scrolling = false;// 是否需要滾動

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		adapter = new MyAdapter();
		new MyTask().execute(path + pageNo);
	}

	public class MyTask extends AsyncTask> {
		@Override
		protected List doInBackground(String... params) {
			// TODO Auto-generated method stub
			List list = new ArrayList();

			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(params[0]);
			HttpResponse response = null;
			try {
				response = httpClient.execute(httpPost);
				if (response.getStatusLine().getStatusCode() == 200) {
					String jsonString = EntityUtils.toString(
							response.getEntity(), "utf-8");
					JSONArray jsonArray = new JSONObject(jsonString)
							.getJSONArray("citys");
					for (int i = 0; i < jsonArray.length(); i++) {
						list.add(jsonArray.getString(i));
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				httpClient.getConnectionManager().shutdown();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// 調用適配器綁定數據
			adapter.bindData(result);
			setListAdapter(adapter);
			adapter.notifyDataSetChanged();
			pageNo++;// 頁碼加一
		}

	}

	public class MyAdapter extends BaseAdapter {
		private List list;

		public void bindData(List list) {
			this.list = list;
		}

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

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = null;
			if (convertView == null) {
				view = LayoutInflater.from(getActivity()).inflate(
						R.layout.item, null);
			} else {
				view = convertView;
			}
			TextView textView = (TextView) view.findViewById(R.id.textView1);
			textView.setText(list.get(position));
			return view;
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.f1, null);
		return view;
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getListView().setOnScrollListener(this);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		// 表示用戶不在滑動屏幕
		if (is_scrolling && OnScrollListener.SCROLL_STATE_IDLE == scrollState) {
			new MyTask().execute(path + pageNo);
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if (firstVisibleItem + visibleItemCount == totalItemCount
				&& totalItemCount != 0) {
			is_scrolling = true;
		} else {
			is_scrolling = false;
		}
	}

}

對應布局:




    

1.3第三個界面


public class Fragment2 extends Fragment {

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

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.f2, null);
		return view;
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
}

對應的布局:




    

    



效果:

\


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