如图所示效果,列表+手势分页

java代码:

 

 
  
  1. private final static String TAG = AlarmCenterActivity.class.getName(); 
  2.      
  3.     private ScrollLayout mScrollLayout; 
  4.     private List<SpinnerItem> agentList = new ArrayList<SpinnerItem>(); 
  5.     private Spinner sp_agent;// 主机下拉框 
  6.     private EditText ealarm_name;// 实例名称 
  7.     private Button serach_btn;// 搜索按钮 
  8.     private ScrollPageControlView pageControl; 
  9.     public MyHandler myHandler; 
  10.     private DataLoading dataLoad; 
  11.     List<Map> list = new ArrayList<Map>();//web端获取的数据 
  12.  
  13.     // 存放下拉选中的值 
  14.     String tmpAgent = "";// 主机 
  15.     // 告警中心列表 
  16.     final String ALARM_URL = "xxxxxx"
  17.     // 主机下拉 
  18.     final String AGENT_URL = "xxxxxxxxxx"
  19.  
  20.     // 查询条件--主机名称 
  21.     String s_agent_name = ""
  22.     // 查询条件--实例名称 
  23.     String s_alarm_name = ""
  24.  
  25. @Override 
  26.     protected void onCreate(Bundle savedInstanceState) { 
  27.         super.onCreate(savedInstanceState); 
  28.         setContentView(R.layout.activity_alarmcenter); 
  29.         initUI(); 
  30.         Toast.makeText(AlarmCenterActivity.this"正在加载数据...", Toast.LENGTH_SHORT).show(); 
  31.         //起一个线程更新数据 
  32.         MyThread m = new MyThread(); 
  33.         new Thread(m).start(); 
  34.     } 
  35.  
  36. // 获取组件初始化值
  37.     private void initUI() { 
  38.         dataLoad = new DataLoading(); 
  39.         mScrollLayout = (ScrollLayout) findViewById(R.id.ScrollLayoutTest); 
  40.         myHandler = new MyHandler(this1); 
  41.         sp_agent = (Spinner) findViewById(R.id.sp_agent); 
  42.         ealarm_name = (EditText) findViewById(R.id.Ealarm_name); 
  43.  
  44.         serach_btn = (Button) findViewById(R.id.search); 
  45.  
  46.         // 主机下拉数据来源 
  47.         List<NameValuePair> params = new ArrayList<NameValuePair>(); 
  48.         params.add(new BasicNameValuePair("id""agent_id")); 
  49.         params.add(new BasicNameValuePair("name""agent_name")); 
  50.         try { 
  51.             String strAgents = HttpIface.doPost(AGENT_URL, params); 
  52.             JSONObject json = new JSONObject(strAgents); 
  53.             Iterator<String> it = json.keys(); 
  54.             SpinnerItem spinnerItem; 
  55.             String key; 
  56.             // 默认全部 
  57.             SpinnerItem add_spinnerItem = new SpinnerItem("""全部"); 
  58.             agentList.add(add_spinnerItem); 
  59.             while (it.hasNext()) { 
  60.                 key = it.next(); 
  61.                 spinnerItem = new SpinnerItem(key, (String) json.get(key)); 
  62.                 agentList.add(spinnerItem); 
  63.             } 
  64.         } catch (JSONException e) { 
  65.             Log.e(TAG, "json convert error!"); 
  66.             e.printStackTrace(); 
  67.         } catch (ClientProtocolException e) { 
  68.             Log.e(TAG, "HttpIface.doPost ClientProtocolException"); 
  69.             e.printStackTrace(); 
  70.         } catch (IOException e) { 
  71.             String message = ""
  72.             if (message.indexOf("The operation timed out") > -1){ 
  73.                 message = "调用服务超时!"
  74.             } else { 
  75.                 message = "调用服务出现网络异常!"
  76.             } 
  77.             DialogUtil.displayError(AlarmCenterActivity.this, message, new OnClickListener() { 
  78.                 @Override 
  79.                 public void onClick(View v) { 
  80.                      
  81.                 } 
  82.             }); 
  83.             Log.e(TAG, "调用服务超时或出现网络异常:" + ExceptionHandler.getErrorMessage(e)); 
  84.         } 
  85.          
  86.  
  87.         /** 
  88.          * 下拉框处理 
  89.          */ 
  90.         // 将可选内容与ArrayAdapter连接 
  91.         ArrayAdapter s_agent_adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, agentList); 
  92.         // 设置下拉列表的风格 
  93.         s_agent_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
  94.         // 将s_agent_adapter添加到sp_agent中 
  95.         sp_agent.setAdapter(s_agent_adapter); 
  96.         // 添加主机下拉框Spinner事件监听 
  97.         sp_agent.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { 
  98.  
  99.             @Override 
  100.             public void onItemSelected(AdapterView<?> arg0, View arg1, 
  101.                     int arg2, long arg3) { 
  102.                 tmpAgent = agentList.get(arg2).getKey(); 
  103.                 // 设置显示当前选择的项 
  104.                 arg0.setVisibility(View.VISIBLE); 
  105.             } 
  106.  
  107.             @Override 
  108.             public void onNothingSelected(AdapterView<?> arg0) { 
  109.                 arg0.setVisibility(View.VISIBLE); 
  110.             } 
  111.  
  112.         }); 
  113.          
  114.          
  115.  
  116.         // 查询按钮的事件监听 
  117.         serach_btn.setOnClickListener(new Button.OnClickListener() { 
  118.             public void onClick(View v) { 
  119.                 s_alarm_name = ealarm_name.getText().toString(); 
  120.                 s_agent_name = tmpAgent; 
  121.                  
  122.                 mScrollLayout.removeAllViews(); 
  123.                 //重新加载 
  124.                 list.clear(); 
  125.                 getListData(); 
  126.                 int totalPages = 0
  127.                 if ((list.size() % AlarmCenterAdapter.APP_PAGE_SIZE) == 0) { 
  128.                     totalPages = list.size() / AlarmCenterAdapter.APP_PAGE_SIZE; 
  129.                 } else { 
  130.                     totalPages = list.size() / AlarmCenterAdapter.APP_PAGE_SIZE + 1
  131.                 } 
  132.                 for (int i = 0; i < totalPages; i++) { 
  133.                     ListView appPage = new ListView(AlarmCenterActivity.this); 
  134.                     appPage.setAdapter(new AlarmCenterAdapter(AlarmCenterActivity.this, list, i)); 
  135.                     appPage.setOnItemClickListener(listener); 
  136.                     mScrollLayout.addView(appPage); 
  137.                 } 
  138.                 // 加载分页 
  139.                 pageControl = (ScrollPageControlView) findViewById(R.id.pageControl); 
  140.                 pageControl.bindScrollViewGroup(mScrollLayout); 
  141.                 // 加载分页数据 
  142.                 dataLoad.bindScrollViewGroup(mScrollLayout); 
  143.  
  144.                  
  145.                  
  146.                 mScrollLayout.snapToScreen(0); 
  147.             } 
  148.         }); 
  149.     } 
  150.  
  151.     // 更新后台数据 
  152.     class MyThread implements Runnable { 
  153.         public void run() { 
  154.             Message msg = myHandler.obtainMessage(); 
  155.             Bundle bundle = new Bundle();// 存放数据 
  156.             getListData(); 
  157.             JSONArray ja = new JSONArray(list); 
  158.             bundle.putString("alarmList", ja.toString()); 
  159.             msg.setData(bundle); 
  160.             AlarmCenterActivity.this.myHandler.sendMessage(msg); // 向Handler发送消息,更新UI 
  161.         } 
  162.     } 
  163.  
  164.     //获取数据 
  165.     class MyHandler extends Handler { 
  166.         private AlarmCenterActivity mContext; 
  167.  
  168.         public MyHandler(Context conn, int a) { 
  169.             mContext = (AlarmCenterActivity) conn; 
  170.         } 
  171.  
  172.         // 子类必须重写此方法,接收数据 
  173.         @Override 
  174.         public void handleMessage(Message msg) { 
  175.             super.handleMessage(msg); 
  176.             Bundle bundle = msg.getData(); 
  177.             String alarmList = bundle.getString("alarmList"); 
  178.             if (null != alarmList) { 
  179.                 int totalPages = 0
  180.                 if ((list.size() % AlarmCenterAdapter.APP_PAGE_SIZE) == 0) { 
  181.                     totalPages = list.size() / AlarmCenterAdapter.APP_PAGE_SIZE; 
  182.                 } else { 
  183.                     totalPages = list.size() / AlarmCenterAdapter.APP_PAGE_SIZE + 1
  184.                 } 
  185.                 for (int i = 0; i < totalPages; i++) { 
  186.                     ListView appPage = new ListView(mContext); 
  187.                     appPage.setAdapter(new AlarmCenterAdapter(mContext, list, i)); 
  188.                     appPage.setOnItemClickListener(listener); 
  189.                     mScrollLayout.addView(appPage); 
  190.                 } 
  191.                 // 加载分页 
  192.                 pageControl = (ScrollPageControlView) findViewById(R.id.pageControl); 
  193.                 pageControl.bindScrollViewGroup(mScrollLayout); 
  194.                 // 加载分页数据 
  195.                 dataLoad.bindScrollViewGroup(mScrollLayout); 
  196.             } 
  197.         } 
  198.     } 
  199.      
  200.  
  201.     // 从服务器获取数据并转换 
  202.     public void getListData() { 
  203.         Map<String, String> map = null
  204.         String strResult = HttpIface.doGet(ALARM_URL); 
  205.         if (!"".equals(strResult) && null != strResult) { 
  206.             JSONTokener jsonParser = new JSONTokener(strResult); 
  207.             JSONObject jsonObject; 
  208.             try { 
  209.                 jsonObject = (JSONObject) jsonParser.nextValue(); 
  210.                 String prgjson = jsonObject.getString("alarmCur"); 
  211.                 JSONArray jsonArray = new JSONArray(prgjson); 
  212.                 for (int i = 0; i < jsonArray.length(); i++) {// 
  213.                     JSONObject item = jsonArray.getJSONObject(i); // 每条记录又由几个Object对象组成 
  214.                     String kpi_instance_id = item.getString("KPI_INSTANCE_ID"); // 获取对象对应的值 
  215.                     String agent_name = item.getString("AGENT_NAME");// 主机 
  216.                    
  217.                    。。。。。。
  218.  
  219.                     //查询条件全部不为空 
  220.                     if (!s_agent_name.equals("") && !s_alarm_name.equals("")) { 
  221.                         if (agent_name.equals(s_agent_name) && kpi_instance_name.trim().indexOf(s_alarm_name) != -1) { 
  222.                             map = new HashMap<String, String>(); // 存放到MAP里面 
  223.                             map.put("kpi_instance_id", kpi_instance_id); 
  224.                             map.put("agent_name", agent_name); 
  225.                           
  226.                         。。。。。。
  227.                             list.add(map); 
  228.                         } 
  229.                     } 
  230.                     // 按程序名称查询 
  231.                     if (s_agent_name.equals("") && !s_alarm_name.equals("")) { 
  232.                         if (kpi_instance_name.trim().indexOf(s_alarm_name) != -1) { 
  233.                             map = new HashMap<String, String>(); // 存放到MAP里面 
  234.                             map.put("kpi_instance_id", kpi_instance_id); 
  235.                             map.put("agent_name", agent_name); 
  236.                         
  237.                         。。。。。。
  238.                             list.add(map); 
  239.                         } 
  240.                     } 
  241.  
  242.  
  243.                     // 按主机名称模糊查询 
  244.                     if (!s_agent_name.equals("") && s_alarm_name.equals("")) { 
  245.                         if (agent_name.equals(s_agent_name)) { 
  246.                             map = new HashMap<String, String>(); // 存放到MAP里面 
  247.                             map.put("kpi_instance_id", kpi_instance_id); 
  248.                             map.put("agent_name", agent_name); 
  249.                             
  250.                         。。。。。。
  251.                             list.add(map); 
  252.                         } 
  253.                     } 
  254.                     // 查询全部 
  255.                     if (s_agent_name.equals("") && s_alarm_name.equals("")) { 
  256.                         map = new HashMap<String, String>(); // 存放到MAP里面 
  257.                         map.put("kpi_instance_id", kpi_instance_id); 
  258.                         map.put("agent_name", agent_name); 
  259.                       
  260.                        。。。。。。。
  261.                         map.put("user_id_c", user_id_c); 
  262.                         list.add(map); 
  263.                     } 
  264.                 } 
  265.             } catch (JSONException e) { 
  266.                 e.printStackTrace(); 
  267.             } 
  268.         } 
  269.     } 
  270.      
  271.      
  272.  
  273.     // 分页数据 
  274.     class DataLoading { 
  275.         private int count; 
  276.          
  277.         public void bindScrollViewGroup(ScrollLayout scrollViewGroup) { 
  278.             this.count = scrollViewGroup.getChildCount(); 
  279.             scrollViewGroup.setOnScreenChangeListenerDataLoad(new OnScreenChangeListenerDataLoad() { 
  280.                         public void onScreenChange(int currentIndex) { 
  281.                             Log.i(TAG, "onScreenChange 页码 = " + currentIndex); 
  282.                             generatePageControl(currentIndex); 
  283.                         } 
  284.                     }); 
  285.         } 
  286.  
  287.         private void generatePageControl(int currentIndex) { 
  288.             if (count == currentIndex + 1) { 
  289.             } 
  290.         } 
  291.     } 

 

 //列表展示给各列赋值

 
  
  1.  
  2. //获取组件赋值 
  3.     public View getView(int position, View convertView, ViewGroup parent) { 
  4.         // TODO Auto-generated method stub 
  5.         Map appInfo = mList.get(position); 
  6.         AppItem appItem; 
  7.         if (convertView == null) { 
  8.             View v = LayoutInflater.from(mContext).inflate(R.layout.item_alarmcenter, null); 
  9.              
  10.             appItem = new AppItem(); 
  11.             appItem.num = (TextView) v.findViewById(R.id.num); 
  12.             appItem.agent_name = (TextView) v.findViewById(R.id.agent_name); 
  13.            。。。。。。。。
  14.              
  15.             v.setTag(appItem); 
  16.              
  17.             convertView = v; 
  18.         } else { 
  19.             appItem = (AppItem)convertView.getTag(); 
  20.         } 
  21.         //赋值 
  22.         appItem.num.setText(position+1+""); 
  23.         appItem.agent_name.setText(appInfo.get("agent_name") == null ? "":appInfo.get("agent_name").toString()); 
  24.         appItem.coll_value.setText(appInfo.get("coll_value") == null ? "":appInfo.get("coll_value").toString()); 
  25.         appItem.kpi_instance_name.setText(appInfo.get("kpi_instance_name") == null ? "":appInfo.get("kpi_instance_name").toString()); 
  26.         。 。。。。。
  27.          
  28.         return convertView; 
  29.     } 

 

查询组件和列表表头的XML  list_alarmcenter.xml

 
  
  1. <!-- 查询条件 --> 
  2.  
  3.    <LinearLayout 
  4.        android:layout_width="fill_parent" 
  5.        android:layout_height="match_parent" 
  6.        android:orientation="horizontal" > 
  7.  
  8.        <LinearLayout 
  9.            android:layout_width="0dip" 
  10.            android:layout_height="wrap_content" 
  11.            android:layout_weight="9" 
  12.            android:background="@drawable/sys_btn_query_pressed" 
  13.            android:gravity="center_vertical|left" 
  14.            android:orientation="horizontal" > 
  15.  
  16.            <TextView 
  17.                android:id="@+id/txtagent" 
  18.                style="@style/label_text" 
  19.                android:layout_width="0dip" 
  20.                android:layout_weight="1" 
  21.                android:text="主机:" /> 
  22.  
  23.            <Spinner 
  24.                android:id="@+id/sp_agent" 
  25.                style="@style/edit_text" 
  26.                android:layout_width="0dip" 
  27.                android:layout_height="wrap_content" 
  28.                android:layout_weight="2" 
  29.                android:gravity="left" /> 
  30.  
  31.        </LinearLayout> 
  32.  
  33.        <Button 
  34.            android:id="@+id/search" 
  35.            style="@style/button" 
  36.            android:layout_width="0dip" 
  37.            android:layout_marginLeft="20dip" 
  38.            android:layout_weight="1" /> 
  39.    </LinearLayout> 
  40.  
  41.    <LinearLayout 
  42.        android:layout_width="fill_parent" 
  43.        android:layout_height="wrap_content" 
  44.        android:layout_marginTop="5dip" 
  45.        android:orientation="vertical" > 
  46.  
  47.        <!-- 标题 --> 
  48.  
  49.        <LinearLayout 
  50.            android:layout_width="fill_parent" 
  51.            android:layout_height="fill_parent" 
  52.            android:background="@drawable/page_title_bg" > 
  53.  
  54.            <View style="@style/vertical_layout" /> 
  55.  
  56.            <TextView 
  57.                android:id="@+id/alarm_num" 
  58.                android:layout_width="70dp" 
  59.                android:layout_height="wrap_content" 
  60.                android:gravity="center_vertical|center_horizontal" 
  61.                android:padding="10dp" 
  62.                android:singleLine="true" 
  63.                android:text="序号" 
  64.                android:textColor="#000000" 
  65.                android:textSize="16dp" 
  66.                android:textStyle="bold" /> 
  67.  
  68.            <View style="@style/vertical_layout" /> 
  69.  
  70.            <TextView 
  71.                android:id="@+id/textView1" 
  72.                android:layout_width="160dp" 
  73.                android:layout_height="wrap_content" 
  74.                android:gravity="center_vertical|center_horizontal" 
  75.                android:padding="10dp" 
  76.                android:singleLine="true" 
  77.                android:text="主机" 
  78.                android:textColor="#000000" 
  79.                android:textSize="16dp" 
  80.                android:textStyle="bold" /> 
  81.  
  82.            <View style="@style/vertical_layout" /> 
  83.  。。。。。。。。
  84.        </LinearLayout> 

 

// 给列表赋值组件 item_alarmcenter.xml

 
  
  1. <LinearLayout 
  2.     android:id="@+id/view" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="horizontal" > 
  6.  
  7.     <View style="@style/vertical_layout" /> 
  8.     <!-- 序号 --> 
  9.  
  10.     <TextView 
  11.         android:id="@+id/num" 
  12.         android:layout_width="70dp" 
  13.         android:layout_height="wrap_content" 
  14.         android:gravity="center_vertical|center_horizontal" 
  15.         android:padding="10dp" 
  16.         android:singleLine="true" 
  17.         android:textColor="#000000" 
  18.         android:textSize="16dp" /> 
  19.  
  20.     <View style="@style/vertical_layout" /> 
  21.     <!-- 主机 --> 
  22.  
  23.     <TextView 
  24.         android:id="@+id/agent_name" 
  25.         android:layout_width="160dp" 
  26.         android:layout_height="wrap_content" 
  27.         android:gravity="center_vertical|center_horizontal" 
  28.         android:padding="10dp" 
  29.         android:singleLine="true" 
  30.         android:textColor="#000000" 
  31.         android:textSize="16dp" /> 
  32.  
  33.     <View style="@style/vertical_layout" /> 
  34.    
  35.  
  36.   
  37.     。。。。。。。。
  38. </LinearLayout> 

//滑动组件ScrollLayout  scroll_page_main.xml

 
  
  1. <RelativeLayout 
  2.        android:id="@+id/myView" 
  3.        android:layout_width="fill_parent" 
  4.        android:layout_height="fill_parent" > 
  5.  
  6.        <com.sunrise.common.component.ScrollLayout 
  7.            xmlns:android="http://schemas.android.com/apk/res/android" 
  8.            android:id="@+id/ScrollLayoutTest" 
  9.            android:layout_width="fill_parent" 
  10.            android:layout_height="fill_parent" /> 
  11.  
  12.        <com.sunrise.common.component.ScrollPageControlView 
  13.            android:id="@+id/pageControl" 
  14.            android:layout_width="fill_parent" 
  15.            android:layout_height="40px" 
  16.            android:layout_alignParentBottom="true" 
  17.            android:gravity="center" /> 
  18.    </RelativeLayout> 

 

以上俩个xml 文件  分别引入到总的 xml 文件中。

 
  
  1. <include layout="@layout/list_alarmcenter" /> 
  2.  
  3.    <include layout="@layout/scroll_page_main" /> 

列表展示详解:

  listView开始绘制的时候,首先调用getCount()函数,根据他的返回值得到listView的长度,然后根据这个长度,调用getView()逐一绘制每一行。如果你的getCount()返回值是0的话,列表将不显示同样return 1,就只显示一行。

getView()有三个参数,position表示将显示的是第几行,covertView是从布局文件中inflate来的布局。我们用LayoutInflater的方法将定义好的 item_alarmcenter.xml 文件提取成View实例用来显示。然后将xml文件中的各个组件实例化(简单的findViewById()方法)。这样便可以将数据对应到各个组件上了。至此一个自定义的listView就完成了,现在让我们回过头从新审视这个过程。系统要绘制ListView了,他首先获得要绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用getView()函数。在这个函数里面首先获得一个View(实际上是一个ViewGroup),然后再实例并设置各个组件,显示之。好了,绘制完这一行了。那 再绘制下一行,直到绘完为止。

手势滑动:

 

1. 该类和LinearLayoutRelativeLayout等布局都是ViewGroup的子类。LinearLayout可以在属性中指定view的排列方式——横向或纵向,而我们自己写的这个类是通过onLayout(boolean changed, int l, int t, int r, int b)方法来自行指定排列方向的。我们这里指定的是横向。

2. 该类中用到了一些我们不常用的类,如VelocityTracker和Scroller,大家可以参考Android开发文档研究一下。