ListView联动(二级联动)简单易懂

前段时间项目需求,需要做二级联动(左边列表点击联动右边列表,右边列表滑动联动左边列表)的需求,现在已经发版,抽时间总结,记录下来


这里代码是新写的一个Demo,项目中的代码太多,逻辑太多,不好贴代码

布局



    

    

左边ListviewAdapter

public class LefteAdapter extends BaseAdapter {
    private ArrayList list;
    private Context context;
    private int item;
    private String url;

    public LefteAdapter(Context applicationContext, ArrayList list) {
        this.list = list;
        this.context = applicationContext;
    }

    public void setTpyeData(int i) {
        this.item = i;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View contenView = View.inflate(context, R.layout.item_image_deme, null);
        if (i == item) {
            url = "https://img-blog.csdn.net/20140915162641093?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWxsZW4zMTU0MTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center";
        } else {
            url = "https://img-blog.csdn.net/20170312221304074?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VvbGluX2Jsb2c=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast";
        }
        ImageView imageView = (ImageView) contenView.findViewById(R.id.image);
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .into(imageView);


        return contenView;
    }
}
PS:主要代码是
setTpyeData()
由于这是Demo没有使用复用item,还有这里Glide(不知道的话百度)是为了见识一下它的强大,可用其他代替

点击左边联动右边

 //左边列表点击监听
        lefte_image.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                right_image.setSelection(i);//选中item(联动了右边)
            }
        });
右边滑动联动左边

        //右边列表滑动监听
        right_image.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int i1, int i2) {
                lefte.setTpyeData(firstVisibleItem);//主要是这行代码
                lefte.notifyDataSetChanged();
            }
        });
完成


你可能感兴趣的:(小知识)