Android 自定义侧边栏列表

封装成Demo供日后复用,先上效果动图:

                                                     Android 自定义侧边栏列表_第1张图片

                                                                          效果图

 

        由popWindow和MultiAdapter实现, 图片加载引擎用的是Glide和ImageLoader2选其1,哪个都能完美加载图片,具体源码因为类文件和界面资源文件有些琐碎,只贴出核心代码PopWindow的实现:

package p.com.sidepoplist;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;


/**
 * Created by ThinkPad on 2017/12/7.
 */

public class AsidePopupWindow extends PopupWindow implements View.OnClickListener {

    private View contentView;
    private Context context;
    private Activity activity;
    private MultiTypeAdapter faceWarnMultiTypeAdapter;

    private Items warnItems;
    private PopupWindow popupWindow;
    private RecyclerView mRecyclerView;
    private ImageView img_close;
    private RelativeLayout rel_close;

    public AsidePopupWindow(Context _context, Activity activity, List list)  {

        this.context = _context;
        this.activity = activity;
        popupWindow = this;
        contentView = LayoutInflater.from(context).inflate(R.layout.aside_popwindow, null);
        this.setContentView(contentView);
        //popWindow 高度
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //popWindow  宽度
        this.setWidth(350);
        //    popupWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setFocusable(false);// 取得焦点
        //点击外部消失
        this.setOutsideTouchable(true);
        //设置可以点击
        this.setTouchable(true);
        //动画效果
        this.setAnimationStyle(R.style.AnimationRightFade);
        img_close = contentView.findViewById(R.id.img_close);
        mRecyclerView = contentView.findViewById(R.id.rcv_warning);
        rel_close = contentView.findViewById(R.id.rel_close);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        //MultitypeAdapter
        setData(list);
        faceWarnMultiTypeAdapter = new MultiTypeAdapter(warnItems);
        faceWarnMultiTypeAdapter.register(warnningBean.class, new WarnBinder(activity));
        mRecyclerView.setAdapter(faceWarnMultiTypeAdapter);

        rel_close.setOnClickListener(this);

    }

    public void showWindow(View view) {
        //屏幕宽度
        WindowManager wm1 = activity.getWindowManager();
        int screenWidth = wm1.getDefaultDisplay().getWidth();
        if (!this.isShowing()) {

            // 获取控件的位置,安卓系统>7.0
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            this.showAtLocation(view, Gravity.NO_GRAVITY, screenWidth, location[1] + view.getHeight());

        }

    }

    public void setData(List list) {

        if (list == null || list.size() == 0) {
            //
        } else {
            //预警项
            warnItems = new Items();
            warnItems.addAll(list);
        }
    }


    @Override
    public void onClick(View view) {

        switch(view.getId()){
            case R.id.rel_close:{
                popupWindow.dismiss();
                break;
            }
        }
    }
}

     Demo已上传:https://download.csdn.net/download/crystal_xing/10871987

 

你可能感兴趣的:(android)