junixapp / XPopup

🔥XPopup2.0版本重磅来袭,2倍以上性能提升,带来可观的动画性能优化和交互细节的提升!!!功能强大,交互优雅,动画丝滑的通用弹窗!可以替代Dialog,PopupWindow,PopupMenu,BottomSheet,DrawerLayout,Spinner等组件,自带十几种效果良好的动画, 支持完全的UI和动画自定义!(Powerful and Beautiful Popup for Android,can absolutely replace Dialog,PopupWindow,PopupMenu,BottomSheet,DrawerLayout,Spinner. With built-in animators , very easy to custom popup view.)
Apache License 2.0
7.76k stars 1.17k forks source link

弹出的框会超出屏幕,怎么限制不让超出屏幕? #591

Closed roybill closed 4 years ago

roybill commented 4 years ago

用的2.1.16版本的AttachPopupView,用于显示屏蔽弹框,有些显示会超出界面该怎么解决? 1.代码一: private void shieldDialog(View view, int position) { if (!mAdapter.getData().isEmpty()) { HouseRecommendBean.ResDataBean bean = mAdapter.getData().get(position); new XPopup.Builder(getContext()) .atView(view) .hasShadowBg(true) // 是否需要黑色背景 .asCustom(new RecommendShieldWindow(getActivity(), mAdapter, position, bean)) .show(); } else { ToastUtils.showShort("获取服务数据异常"); } } 2.代码二: public class RecommendShieldWindow extends AttachPopupView implements View.OnClickListener {

private ImageView mTopClose;
private ImageView mBottomClose;

private BaseProviderMultiAdapter mAdapter;
private int mPosition;
private HouseRecommendBean.ResDataBean mBean;
private int mTypeId;
private String mUserType;
private int mUserId;

public RecommendShieldWindow(@NonNull Context context, BaseProviderMultiAdapter adapter, int position, HouseRecommendBean.ResDataBean bean) {
    super(context);
    mAdapter = adapter;
    mPosition = position;
    this.mBean = bean;
}

@Override
protected int getImplLayoutId() {
    return R.layout.popup_shield_recommend;
}

@Override
protected void onCreate() {
    super.onCreate();
    TextView notInterested = findViewById(R.id.tv_shield_not_interested_recommend);
    TextView repeat = findViewById(R.id.tv_shield_repeat_recommend);
    TextView ad = findViewById(R.id.tv_shield_ad_recommend);
    TextView author = findViewById(R.id.tv_shield_author_recommend);
    TextView shield = findViewById(R.id.tv_shield_recommend);
    mTopClose = findViewById(R.id.iv_top);
    mBottomClose = findViewById(R.id.iv_bottom);

    notInterested.setOnClickListener(this);
    repeat.setOnClickListener(this);
    ad.setOnClickListener(this);
    author.setOnClickListener(this);
    shield.setOnClickListener(this);

    List<HouseRecommendBean.ResDataBean.TopicListBean> topicList = mBean.getTopicList();
    if (!topicList.isEmpty()) {
        String topic = topicList.get(0).getTitle();//话题类型
        if (topic != null && !TextUtils.isEmpty(topic)) {
            shield.setVisibility(View.VISIBLE);
            shield.setText("不想看  " + topic);
        } else {
            shield.setVisibility(View.GONE);
        }
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.tv_shield_author_recommend: //作者
            mUserType = mBean.getUser_type();
            mUserId = mBean.getUser_id();
            break;
        case R.id.tv_shield_recommend:        //屏蔽话题
        case R.id.tv_shield_ad_recommend:     //广告
        case R.id.tv_shield_repeat_recommend: //重复
        case R.id.tv_shield_not_interested_recommend: //不感兴趣
            mUserType = "";
            mUserId = 0;
            break;
    }
    shield();//屏蔽
}

private void shield() {
    String type = mBean.getType();
    switch (type) {
        case "essay":
        case "post":
            mTypeId = mBean.getEssay_id();
            break;
        case "question":
            mTypeId = mBean.getQuestion_id();
            break;
    }
    RxManager.getMethod().shield(new ShieldBody(type, mTypeId, mUserType, mUserId))
            .compose(RxUtil.schedulers((Activity) getContext()))
            .subscribe(new RxCallback<Object>((Activity) getContext()) {
                @Override
                public void onSuccess(Object o, String msg) {
                    LogUtils.d("1451  屏蔽数据成功");
                    mAdapter.remove(mPosition);

// mAdapter.notifyItemRemoved(mPosition); ToastUtils.showLong("已收到反馈,将减少推荐类似内容"); dismiss(); } }); }

/**
 * 根据弹框位置控制箭头的显示和隐藏
 */
@Override
protected boolean isShowUpToTarget() {
    //当前弹出框true为在view的上面,   false在下面
    boolean showUpToTarget = super.isShowUpToTarget();

// LogUtils.d("1351 "+showUpToTarget); if (showUpToTarget) { mTopClose.setVisibility(View.GONE);//弹框在view的上面,隐藏上面的箭头,显示下面的箭头 mBottomClose.setVisibility(View.VISIBLE); } else { mTopClose.setVisibility(View.VISIBLE);//弹框在view的下面面,显示上面的箭头,隐藏下面的箭头 mBottomClose.setVisibility(View.GONE); } return showUpToTarget; }

}

junixapp commented 4 years ago

对于超出屏幕,要么缩短宽度,这样会导致部分内容不可见;如果宽度不变,那就只能往反方向偏移,效果也不是很好。 所以最好的办法是你给布局的宽度写一个固定值,不用wrap,这样最坏的情况就是内容换行