Android中实现ListView横向滑动时间:2021-03-24 19:43:49这是我做的一个查询列表的页面 列表部分实现了 ListView的横向滑动 ListView页面: package com.test.querylist;import java.util.ArrayList;import java.util.HashMap;import com.test.R;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.LinearLayout.LayoutParams;public class QueryListActivity extends Activity {private static String str1 = "str1";private static String str2 = "str2";private static String str3 = "str3";@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(generateContentView());}private View generateContentView(){//主页面LinearLayout layMain = createLayout(LinearLayout.VERTICAL);layMain.setPadding(5, 0, 5, 0);//查询部分LinearLayout layQuery = createLayout(LinearLayout.HORIZONTAL);layQuery.setPadding(0, 0, 0, 0);layQuery.setGravity(Gravity.CENTER);generateQuery(layQuery);//列表部分LinearLayout layList = createLayout(LinearLayout.VERTICAL);layList.setPadding(0, 0, 0, 0);generateList(layList);layMain.addView(layQuery);layMain.addView(layList);return layMain;}/** * 列表部分布局 * */private void generateList(LinearLayout layList) {HorizontalScrollView hsView = new HorizontalScrollView(this);hsView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));LinearLayout lly = createLayout(LinearLayout.VERTICAL);// TODO Auto-generated method stubArrayList<HashMap<String, Object>> myLists = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < 10; i++) {HashMap<String, Object> myList = new HashMap<String, Object>();myList.put(str1, "asd");myList.put(str2, "姓名(" + (i+1) + ")");myList.put(str3, (5 + i) + "");myLists.add(myList);}SimpleAdapter saImageItems = new SimpleAdapter(this, myLists,// 数据来源R.layout.testuser,// 每一个user xml 相当ListView的一个组件new String[] { str1, str2, str3 },// 分别对应view 的idnew int[] { R.id.test_id1, R.id.test_id2, R.id.test_id3 });// 获取listviewListView listQuery = new ListView(this);listQuery.setAdapter(saImageItems);lly.addView(listQuery);hsView.addView(lly);layList.addView(hsView);}/** * 查询部分布局 * */private void generateQuery(LinearLayout layQuery) {// TODO Auto-generated method stubEditText txtQuery = new EditText(this);txtQuery.setLayoutParams(new LayoutParams(400, LayoutParams.WRAP_CONTENT));txtQuery.setMinHeight(60);txtQuery.setMaxHeight(120);Button btnQuery = new Button(this);btnQuery.setText(R.string.querylist_query);btnQuery.setOnClickListener(new Button.OnClickListener(){public void onClick(View v) {// TODO 设置点击事件}});layQuery.addView(txtQuery);layQuery.addView(btnQuery);}private LinearLayout createLayout(int iOrientation) {LinearLayout lay = new LinearLayout(this);lay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));lay.setOrientation(iOrientation);return lay;}} ListView组件的XML:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content" android:layout_width="fill_parent"android:paddingBottom="5dip" android:paddingTop="5dip"android:paddingLeft="5dip" android:paddingRight="5dip"android:orientation="vertical"><LinearLayout android:layout_height="wrap_content"android:paddingLeft="6dip" android:paddingRight="6dip"android:orientation="horizontal" android:layout_width="fill_parent"android:gravity="center_vertical"><TextView android:id="@+id/test_id1" android:gravity="center"android:layout_height="wrap_content" android:layout_width="300"android:layout_weight="1" android:layout_margin = "5dip"></TextView><TextView android:id="@+id/test_id2" android:gravity="center"android:layout_height="wrap_content" android:layout_width="300"android:layout_weight="1" android:layout_margin = "5dip"></TextView><TextView android:id="@+id/test_id3" android:gravity="center"android:layout_height="wrap_content" android:layout_width="300" android:layout_weight="1" android:layout_margin = "5dip"></TextView></LinearLayout></LinearLayout>