最近有朋友问我,关于用listView做成表格,最左边一列横向不变,右边的可以横向滑动,同时listview也可以上下滑动。用HorizontalScrollView包了下listview的item,发现可行。所以拿来记录下。
listItemView.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView android:text="TextView"
android:id="@+id/textView1"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<HorizontalScrollView
android:layout_marginLeft="10dip"
android:scrollbars="none"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="TextView"
android:id="@+id/textView2"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</HorizontalScrollView>
</LinearLayout>
MainActivity.java
public class listviewTest extends Activity implements OnItemClickListener{
String mIndex[];
String mItem[];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIndex = getResources().getStringArray(R.array.index);
mItem = getResources().getStringArray(R.array.item);
ListView mList = (ListView) findViewById(R.id.listView1);
mList.setAdapter(getAdapter(mIndex, mItem));
mList.setOnItemClickListener(this);
}
private SimpleAdapter getAdapter(String[] indexArray,
String[] itemArray) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < indexArray.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("index", indexArray);
map.put("itemText", itemArray);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
R.layout.itemview, new String[] { "index", "itemText" },
new int[] { R.id.textView1, R.id.textView2 });
return simperAdapter;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
listItemView.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView android:text="TextView"
android:id="@+id/textView1"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<HorizontalScrollView
android:layout_marginLeft="10dip"
android:scrollbars="none"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="TextView"
android:id="@+id/textView2"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</HorizontalScrollView>
</LinearLayout>
MainActivity.java
public class listviewTest extends Activity implements OnItemClickListener{
String mIndex[];
String mItem[];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIndex = getResources().getStringArray(R.array.index);
mItem = getResources().getStringArray(R.array.item);
ListView mList = (ListView) findViewById(R.id.listView1);
mList.setAdapter(getAdapter(mIndex, mItem));
mList.setOnItemClickListener(this);
}
private SimpleAdapter getAdapter(String[] indexArray,
String[] itemArray) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < indexArray.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("index", indexArray);
map.put("itemText", itemArray);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
R.layout.itemview, new String[] { "index", "itemText" },
new int[] { R.id.textView1, R.id.textView2 });
return simperAdapter;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}