listView三级联动

先看例子演示:



部分代码:

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,源码例子文档一网打尽。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值