android ViewPager学习笔记1

1、客户端代码

1.1第一个界面

public class MainActivity extends FragmentActivity {
	private ViewPager viewPager;
	private List<Fragment> items;
	private List<String> 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<Fragment>();
		Fragment1 fragment1 = new Fragment1();
		Fragment2 fragment2 = new Fragment2();
		items.add(fragment1);
		items.add(fragment2);
		titles = new ArrayList<String>();
		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;
	}

}

对应的布局:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" >

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pagertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" >
        </android.support.v4.view.PagerTitleStrip>
        
    </android.support.v4.view.ViewPager>

</RelativeLayout>

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<String> 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<String, Void, List<String>> {
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub
			List<String> list = new ArrayList<String>();

			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<String> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// 调用适配器绑定数据
			adapter.bindData(result);
			setListAdapter(adapter);
			adapter.notifyDataSetChanged();
			pageNo++;// 页码加一
		}

	}

	public class MyAdapter extends BaseAdapter {
		private List<String> list;

		public void bindData(List<String> 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;
		}
	}

}

对应布局:

<?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"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:drawSelectorOnTop="false" />

</LinearLayout>
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();
	}
}

对应的布局:

<?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" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

效果:



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。