自定义popuwindow+recyclerView下拉选择框

需要说明:1.可自由显示在控件下方,并根据控件宽度显示对应的宽度。

2.recycleView可自定义背景,并且可带自定义颜色侧滑栏

3.点击外部消失,选中条目后消失

 

方法简单抽取

private PopuWindow popupWindow;
public void showSelectWindows(TextView view,List mDataList)
{
//获取布局中的recyclerView
RecyclerView recyclerView = (RecyclerView)LayoutInflater.from(ConferenceReservationActivity.this).inflate(R.layout.ry,null);

recyclerView.setBackgroundResource(R.drawable.shape_white_bg);
recyclerView.setScrollbarFadingEnabled(false);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

//设置布局管理器
recyclerView.setLayoutManager(layoutManager);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

//设置Adapter
ConferenceAdapter adapter = new ConferenceAdapter(mDataList,Activity.this);

recyclerView.setAdapter(adapter);

adapter.setOnItemClickListener(new ConferenceAdapter.OnItemClickListener(){

 view.setText(content);
 popupWindow.dismiss();

});

    popupWindow = new PopupWindow(recyclerView,view.getWidth(),300);
//设置点击外部消失
    popupWindow.setBackgroundDrawable(new ColorDrawable());
    popupWindow.setFocusable(true)
    //设定在指定view下显示
    popupWindow.showAsDropDown(view,0,10);



}

 

带阴影圆角背景shape_white_bg代码


   
       
           
           
           
       
   
   
       
           
           
           
       
   
   
       
           
           
           
       
   
   
       
           
           
           
       
   
   
       
           
           
           
       
   
   
       
           
           
           
       
   
   
       
           
           
           
       
   

参考链接:https://www.jianshu.com/p/c72d19dafa75

rceycleView的布局文件


    

设置侧拉条背景drawable

scorllview_button1.xml


    


scrollview_bg.xml


    






adapter

public class ConferenceAdapter extends RecyclerView.Adapter {

private List mList;
private Context mContext;
    public ConferenceAdapter(List list, Context context) {
        
        mList = list;
        mContext = context;
        
        
    }

    @NonNull
    @Override
    public ConferenceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.conference_item,parent,false);
        return new ViewHolder(v);
    }


    @Override
    public void onBindViewHolder(@NonNull final ConferenceAdapter.ViewHolder holder, final int position) {

        holder.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null)
                onItemClickListener.onItemClick(mList.get(position),position);
            }
        });
        holder.tvItem.setText(mList.get(position));
    }

    /**
     * Returns the total number of items in the data set held by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        if (mList!=null&&mList.size()>0){
            return mList.size();
        }
       return 0;
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder{

        private final RelativeLayout rlItem;
        private final TextView tvItem;
        public ViewHolder(View itemView) {
            super(itemView);
            rlItem = itemView.findViewById(R.id.rl_item);
            tvItem = itemView.findViewById(R.id.tv_item);
        }
    }
    public interface OnItemClickListener{
        
        void onItemClick(String content,int position);
    }
    OnItemClickListener onItemClickListener;
    
    public void setOnItemClickListener(OnItemClickListener mListener){
        onItemClickListener = mListener;
    }
}

item




    

 

你可能感兴趣的:(整理)