欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【笔记】创建PopupWindow

程序员文章站 2022-05-31 15:38:39
...
public class xxx extends Fragment{

    private TextView mSortBtn, mJoinBtn, mReadBtn;
    private ImageView mUpImg, mDownImg;
    private RecyclerView mRecycler;
    private LoadingPage mLoadPageFl;

    private PopupWindow popupWindow;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.retech_fg_my_shelf, container, false);
        mSortBtn = view.findViewById(R.id.tv_sort);
        mUpImg = view.findViewById(R.id.iv_up);
        mDownImg = view.findViewById(R.id.iv_down);
        mRecycler = view.findViewById(R.id.recyclerView);
        mLoadPageFl = view.findViewById(R.id.loadPage);
        TextView mTitleTv = view.findViewById(R.id.tv_AppBar);
        ImageView mBackBtn = view.findViewById(R.id.iv_back);
        ImageView mSearchBtn = view.findViewById(R.id.iv_search);
        mTitleTv.setText("我的书橱");
        mBackBtn.setVisibility(View.GONE);
        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //to search page
            }
        });
        sort = "0";
        mSortBtn.setText("最近加入");
        mUpImg.setSelected(true);
        mDownImg.setSelected(false);
        mSortBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.showAsDropDown(mSortBtn, DpUtils.dp2px(getActivity(), 10.0f),
                        0, Gravity.LEFT);
            }
        });
        mLoadPageFl.setLoadPageListener(this);
        if (Utils.isPad(getActivity())) {
            mRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        } else {
            mRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        }
        initPoPwindows();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mLoadPageFl.show();
    }

    private void initPoPwindows() {
        View view = View.inflate(getActivity(), R.layout.retech_pop_myshelf_sort, null);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        mJoinBtn = view.findViewById(R.id.tv_join);
        mReadBtn = view.findViewById(R.id.tv_read);
        mJoinBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //点击最近加入按钮
                mSortBtn.setText("最近加入");
                mJoinBtn.setTextColor(getResources().getColor(R.color.retech_appyes));
                mReadBtn.setTextColor(getResources().getColor(R.color.retech_appnot));
                if ("0".equals(sort)) {
                    sort = "1";
                    mUpImg.setSelected(false);
                    mDownImg.setSelected(true);
                    mLoadPageFl.show();
                } else {
                    sort = "0";
                    mUpImg.setSelected(true);
                    mDownImg.setSelected(false);
                    mLoadPageFl.show();
                }
                popupWindow.dismiss();
            }
        });
        mReadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //点击最近阅读按钮
                mSortBtn.setText("最近阅读");
                mJoinBtn.setTextColor(getResources().getColor(R.color.retech_appnot));
                mReadBtn.setTextColor(getResources().getColor(R.color.retech_appyes));
                if ("2".equals(sort)) {
                    sort = "3";
                    mUpImg.setSelected(false);
                    mDownImg.setSelected(true);
                    mLoadPageFl.show();
                } else {
                    sort = "2";
                    mUpImg.setSelected(true);
                    mDownImg.setSelected(false);
                    mLoadPageFl.show();
                }
                popupWindow.dismiss();
            }
        });
    }
}

 

相关标签: PopupWindow