Java代码
public class MainActivity extends Activity {
private ListView citylist_lft;
private ListView citylist_right;
List<String> list = new ArrayList<>();
String[] letter = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z" };
String[] city = { "C", "B", "H", "G", "W", "C", "S", "S", "C", "W", "H",
"Z", "C", "D", "N", "L", "Q", "S", "S", "L", "N", "H", "X", "H",
"S", "Z", "G", "N", "J", "T", "S", "X", "A" };
String[] cityName = { "长沙", "北京", "杭州", "广州", "武汉", "重庆", "上海", "深圳", "长春",
"乌鲁木齐", "哈尔滨", "郑州", "成都", "大连", "南昌", "兰州", "齐齐哈尔", "汕头", "苏州",
"拉萨", "南京", "呼和浩特", "厦门", "合肥", "沈阳", "张家界", "贵州", "宁夏", "济南",
"天津", "石家庄", "西安", "澳门" };
private void initData() {
for (int i = 0; i < letter.length; i++) {
boolean flag = false;
for (int j = 0; j < city.length; j++) {
if (letter[i].equals(city[j])) {
if (!flag) {
flag = true;
list.add(letter[i]);
}
list.add(cityName[j]);
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
initData();
citylist_lft.setAdapter(new MyBaseAdapter());
citylist_right.setAdapter(new MyBaseAdapter1());
citylist_lft.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
boolean flag = true;
for (int i = 0; i < letter.length; i++) {
if (letter[i].equals(list.get(position))) {
flag = false;
break;
}
}
if (flag) {
Toast.makeText(MainActivity.this, list.get(position), 0)
.show();
}
}
});
citylist_right.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
for (int i = 0; i < list.size(); i++) {
if (letter[position].equals(list.get(i))) {
// 根据索引跳到指定的位置
citylist_lft.setSelection(i);
}
}
}
});
}
/**
* 初始化控件
*/
private void init() {
citylist_lft = (ListView) findViewById(R.id.citylist_lft);
citylist_right = (ListView) findViewById(R.id.citylist_right);
}
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getItemViewType(int position) {
for (int i = 0; i < letter.length; i++) {
if (letter[i].equals(list.get(position))) {
return 1;
}
}
return 2;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String str = list.get(position);
int type = getItemViewType(position);
System.out.println(type + "");
ViewHolder1 vh1 = null;
ViewHolder2 vh2 = null;
if (convertView == null) {
switch (type) {
case 1:
vh1 = new ViewHolder1();
convertView = View.inflate(MainActivity.this,
R.layout.item1, null);
vh1.tv1 = (TextView) convertView
.findViewById(R.id.item1_textview);
convertView.setTag(vh1);
break;
case 2:
vh2 = new ViewHolder2();
convertView = View.inflate(MainActivity.this,
R.layout.item2, null);
vh2.tv2 = (TextView) convertView
.findViewById(R.id.item2_textview);
convertView.setTag(vh2);
break;
}
} else {
switch (type) {
case 1:
vh1 = (ViewHolder1) convertView.getTag();
break;
case 2:
vh2 = (ViewHolder2) convertView.getTag();
break;
}
}
switch (type) {
case 1:
System.out.println(vh1 + "vh1");
vh1.tv1.setText(str);
break;
case 2:
vh2.tv2.setText(str);
break;
default:
break;
}
System.out.println(convertView + "convertView");
return convertView;
}
class ViewHolder1 {
TextView tv1;
}
class ViewHolder2 {
TextView tv2;
}
}
class MyBaseAdapter1 extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return letter.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String str = letter[position];
ViewHolder vh = null;
if (convertView == null) {
vh = new ViewHolder();
convertView = View.inflate(MainActivity.this, R.layout.item3,
null);
vh.tv = (TextView) convertView
.findViewById(R.id.item3_textview);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.tv.setText(str);
return convertView;
}
class ViewHolder {
TextView tv;
}
}
}
布局文件
item1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="40dp"
android:background="#cccccc"
android:orientation="vertical" >
<TextView
android:id="@+id/item1_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"
/>
</LinearLayout>
item2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="40dp"
android:orientation="vertical" >
<TextView
android:id="@+id/item2_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"
/>
</LinearLayout>
item3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="40dp"
android:orientation="vertical" >
<TextView
android:layout_gravity="center_horizontal"
android:id="@+id/item3_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
/>
</LinearLayout>
效果图