Android中实现ListView横向滑动

一个查询列表的页面 列表部分实现了 ListView的横向滑动

ListView页面:

复制代码
   
   
   
   
1 package com.test.querylist; 2 3   import java.util.ArrayList; 4   import java.util.HashMap; 5 6   import com.test.R; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.view.Gravity; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.EditText; 14 import android.widget.HorizontalScrollView; 15 import android.widget.LinearLayout; 16 import android.widget.ListView; 17 import android.widget.SimpleAdapter; 18 import android.widget.LinearLayout.LayoutParams; 19 20 public class QueryListActivity extends Activity { 21 22 private static String str1 = " str1 " ; 23 private static String str2 = " str2 " ; 24 private static String str3 = " str3 " ; 25 26 @Override 27 public void onCreate(Bundle icicle) { 28 super .onCreate(icicle); 29 setContentView(generateContentView()); 30 } 31 32 private View generateContentView(){ 33 // 主页面 34 LinearLayout layMain = createLayout(LinearLayout.VERTICAL); 35 layMain.setPadding( 5 , 0 , 5 , 0 ); 36 // 查询部分 37 LinearLayout layQuery = createLayout(LinearLayout.HORIZONTAL); 38 layQuery.setPadding( 0 , 0 , 0 , 0 ); 39 layQuery.setGravity(Gravity.CENTER); 40 generateQuery(layQuery); 41 42 // 列表部分 43 LinearLayout layList = createLayout(LinearLayout.VERTICAL); 44 layList.setPadding( 0 , 0 , 0 , 0 ); 45 generateList(layList); 46 47 layMain.addView(layQuery); 48 layMain.addView(layList); 49 return layMain; 50 } 51 52 /** 53 * 列表部分布局 54 * */ 55 private void generateList(LinearLayout layList) { 56 HorizontalScrollView hsView = new HorizontalScrollView( this ); 57 hsView.setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 58 59 LinearLayout lly = createLayout(LinearLayout.VERTICAL); 60 // TODO Auto-generated method stub 61 ArrayList < HashMap < String, Object >> myLists = new ArrayList < HashMap < String, Object >> (); 62 for ( int i = 0 ; i < 10 ; i ++ ) { 63 HashMap < String, Object > myList = new HashMap < String, Object > (); 64 myList.put(str1, " asd " ); 65 myList.put(str2, " 姓名( " + (i + 1 ) + " ) " ); 66 myList.put(str3, ( 5 + i) + "" ); 67 myLists.add(myList); 68 } 69 SimpleAdapter saImageItems = new SimpleAdapter( this , myLists, // 数据来源 70 R.layout.testuser, // 每一个user xml 相当ListView的一个组件 71 new String[] { str1, str2, str3 }, 72 // 分别对应view 的id 73 new int [] { R.id.test_id1, R.id.test_id2, R.id.test_id3 }); 74 // 获取listview 75 ListView listQuery = new ListView( this ); 76 listQuery.setAdapter(saImageItems); 77 78 lly.addView(listQuery); 79 hsView.addView(lly); 80 layList.addView(hsView); 81 } 82 83 /** 84 * 查询部分布局 85 * */ 86 private void generateQuery(LinearLayout layQuery) { 87 // TODO Auto-generated method stub 88 EditText txtQuery = new EditText( this ); 89 90 txtQuery.setLayoutParams( new LayoutParams( 400 , LayoutParams.WRAP_CONTENT)); 91 txtQuery.setMinHeight( 60 ); 92 txtQuery.setMaxHeight( 120 ); 93 94 Button btnQuery = new Button( this ); 95 btnQuery.setText(R.string.querylist_query); 96 btnQuery.setOnClickListener( new Button.OnClickListener(){ 97 public void onClick(View v) { 98 // TODO 设置点击事件 99 } 100 }); 101 layQuery.addView(txtQuery); 102 layQuery.addView(btnQuery); 103 } 104 105 private LinearLayout createLayout( int iOrientation) { 106 LinearLayout lay = new LinearLayout( this ); 107 lay.setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 108 lay.setOrientation(iOrientation); 109 return lay; 110 } 111 }
复制代码

ListView组件的XML:

复制代码
   
   
   
   
1 <? xml version="1.0" encoding="utf-8" ?> 2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" 3 android:layout_height ="wrap_content" android:layout_width ="fill_parent" 4 android:paddingBottom ="5dip" android:paddingTop ="5dip" 5 android:paddingLeft ="5dip" android:paddingRight ="5dip" 6 android:orientation ="vertical" > 7 < LinearLayout android:layout_height ="wrap_content" 8 android:paddingLeft ="6dip" android:paddingRight ="6dip" 9 android:orientation ="horizontal" android:layout_width ="fill_parent" 10 android:gravity ="center_vertical" > 11 < TextView android:id ="@+id/test_id1" android:gravity ="center" 12 android:layout_height ="wrap_content" android:layout_width ="300" 13 android:layout_weight ="1" android:layout_margin = "5dip" > 14 </ TextView > 15 < TextView android:id ="@+id/test_id2" android:gravity ="center" 16 android:layout_height ="wrap_content" android:layout_width ="300" 17 android:layout_weight ="1" android:layout_margin = "5dip" > 18 </ TextView > 19 < TextView android:id ="@+id/test_id3" android:gravity ="center" 20 android:layout_height ="wrap_content" android:layout_width ="300" 21 android:layout_weight ="1" android:layout_margin = "5dip" > 22 </ TextView > 23 </ LinearLayout > 24 </ LinearLayout >
复制代码

你可能感兴趣的:(android,ListView,object,String,layout,import)