先看例子演示:
部分代码:
package mm.shandong.com.testthreeregion;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import mm.shandong.com.testthreeregion.adapter.FirstSearchAreaAdapter;
import mm.shandong.com.testthreeregion.adapter.SecondSearchAreaAdapter;
import mm.shandong.com.testthreeregion.adapter.ThirdSearchAreaAdapter;
public class TestThreeRegionActivity extends AppCompatActivity {
ListView listView1;
ListView listView2;
ListView listView3;
FirstSearchAreaAdapter firstSearchAreaAdapter;
SecondSearchAreaAdapter secondSearchAreaAdapter;
ThirdSearchAreaAdapter thirdSearchAreaAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_three_region);
listView1 = (ListView) findViewById(R.id.listView1);
listView2 = (ListView) findViewById(R.id.listView2);
listView3 = (ListView) findViewById(R.id.listView3);
initFirstListView();
}
//初始化第一个ListView
public void initFirstListView() {
firstSearchAreaAdapter = new FirstSearchAreaAdapter(this, Constant.firstNameArray);
firstSearchAreaAdapter.selectPosition = 0;
listView1.setAdapter(firstSearchAreaAdapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long l) {
if (firstSearchAreaAdapter.selectPosition != position) {
updateFirstListView(position);
}
}
});
initSecondListView(0);
}
//初始化第二个ListView
public void initSecondListView(int position) {
secondSearchAreaAdapter = new SecondSearchAreaAdapter(this,
Constant.secondNameArray[position]);
secondSearchAreaAdapter.selectPosition = 0;
listView2.setAdapter(secondSearchAreaAdapter);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view, int position, long l) {
if (secondSearchAreaAdapter.selectPosition != position) {
updateSecondListView(position);
}
}
});
initThirdListView(0);
}
//初始化第三个ListView
public void initThirdListView(int position) {
///如果第一个listView选择的是地铁,那么第三个listView就显示出来,否则就隐藏
if ("地铁".equals(firstSearchAreaAdapter.
getItem(firstSearchAreaAdapter.selectPosition))) {
thirdSearchAreaAdapter =
new ThirdSearchAreaAdapter(this, Constant.thirdNameArray[position]);
thirdSearchAreaAdapter.selectPosition = 0;
listView3.setAdapter(thirdSearchAreaAdapter);
} else {
listView3.setVisibility(View.GONE);
}
}
public void updateFirstListView(int position) {
firstSearchAreaAdapter.selectPosition = position;
firstSearchAreaAdapter.notifyDataSetChanged();
secondSearchAreaAdapter.setDatas(Constant.secondNameArray[position]);
updateSecondListView(0);
}
public void updateSecondListView(int position) {
secondSearchAreaAdapter.selectPosition = position;
secondSearchAreaAdapter.notifyDataSetChanged();
if ("地铁".equals(firstSearchAreaAdapter.
getItem(firstSearchAreaAdapter.selectPosition))) {
if (thirdSearchAreaAdapter == null) {
thirdSearchAreaAdapter =
new ThirdSearchAreaAdapter(this, Constant.thirdNameArray[position]);
// thirdSearchAreaAdapter.selectPosition=0;
listView3.setAdapter(thirdSearchAreaAdapter);
} else {
thirdSearchAreaAdapter.setDatas(Constant.thirdNameArray[position]);
updateThirdListView(0);
}
listView3.setVisibility(View.VISIBLE);
} else {
listView3.setVisibility(View.GONE);
}
}
public void updateThirdListView(int position) {
// thirdSearchAreaAdapter.selectPosition=position;
thirdSearchAreaAdapter.notifyDataSetChanged();
}
}
Demo下载最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:http://android.myapp.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy,源码例子文档一网打尽。