編輯:關於Android編程
1、客戶端代碼
1.1第一個界面
public class MainActivity extends FragmentActivity { private ViewPager viewPager; private Listitems; 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; } }
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 Listtotal_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(); } }
在上一篇博文的最後,我說要寫一個下拉刷新的ListView和RecyclerView,並且可以直接使用QQheader本篇博文准備講如何實現下拉刷新和上拉加載,寫的比較
android繪制圓形圖片的兩種方式看下效果先下面有完整的示例代碼使用BitmapShader(著色器)我們在繪制view 的時候 就是小學上美術課 用水彩筆在本子上畫畫
這篇文章篇幅較長,可以使用版權聲明下面的目錄,找到感興趣的進行閱讀目錄 3.1 Android Studio使用初探 Project面板 Stucture面板 Andro
1.創建HelloWorld項目任何編程語言寫出的第一個程序毫無疑問都會是Hello World,這已經是自20世紀70年代一直流傳下來的傳統,在編程界已成為永恆的經典,