首页 > 代码库 > android ViewPager学习笔记1
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>
效果:
android ViewPager学习笔记1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。