根据产品需求,需要实现自定义dialog,里面包含多个布局切换:类似效果如下图所示(子布局样式根据需求来自定义):
第一方案:popWindows+TabLayout+viewapge
结果:一直报viewpage对应的id一直找不到,无论我怎么修改还是删除id,还是这样子报错,捣鼓了一天,感谢sakura_L写的简书:https://www.jianshu.com/p/8b76de9969b4让我知道错在哪了,里面有实现,但是效果不是想要的,后面还是放弃了。
第二方案:HorizontalScrollView、fragment、Viewpager结合使用,参考:https://blog.youkuaiyun.com/qq_41673194/article/details/79357629,但是代码量增加了很多,报的错还是前面那个(因为当时还没有找到方案一错误的原因),还是放弃了。
第三方案:HorizontalScrollView、自定义PopupWindow(位置可以随意指定)具体实现步骤如下:
步骤一:activity_main布局就一个button控件,代码不贴。
item_main(自定义PopupWindow)布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/www">
<!--若子布局有滑动事件操作,可参考https://blog.youkuaiyun.com/wangyongyao1989/article/details/76615827博客,把HorizontalScrollView换成博客里的MyHorizontalScrollView 即可-->
<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="394dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<!--HorizontalScrollView只允许有一个子布局-->
<LinearLayout
android:id="@+id/contentLinearLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#E91E63"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="horizontal" >
<!--在这里定义布局一-->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是布局一"
android:textSize="18sp"
android:textColor="@color/www"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E91E63"
android:orientation="horizontal">
<!--在这里定义布局一-->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是布局二"
android:textSize="18sp"
android:textColor="@color/www"/>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示布局一"
app:layout_constraintBottom_toTopOf="@+id/scrollView"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示布局二"
app:layout_constraintBottom_toTopOf="@+id/scrollView"
app:layout_constraintLeft_toRightOf="@id/button1" />
</android.support.constraint.ConstraintLayout>
步骤二:MainActivity里实现button点击事件,调用showPopupWindow()方法,代码如下:
private void showPopupWindow() {
View wh = LayoutInflater.from(this).inflate(R.layout.item_main, null);
scrollView = wh.findViewById(R.id.scrollView);
layout1 = wh.findViewById(R.id.layout1);
layout2 = wh.findViewById(R.id.layout2);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
final int width = getWidthInPx(this);
//重点:代码设置布局的宽度,不然所有布局全部缩小版显示在一起
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup.LayoutParams params1 = layout1.getLayoutParams();
ViewGroup.LayoutParams params2 = layout2.getLayoutParams();
params1.width = width;
params2.width = width;
layout1.setLayoutParams(layoutParams);
layout2.setLayoutParams(layoutParams);
final WPopupWindow popupWindow = new WPopupWindow(wh);
popupWindow.showAtLocation(getContentView(MainActivity.this), Gravity.BOTTOM, 0, 0);
scrollView.setOnTouchListener(new View.OnTouchListener() {//scrollView滑动事件静止
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
wh.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.scrollTo(0, 0);
}
});
wh.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.scrollTo(width, 0);
}
});
}
//获取屏幕宽度
public int getWidthInPx(Context context) {
final int width = context.getResources().getDisplayMetrics().widthPixels;
return width;
}
private static View getContentView(Activity ac) {
ViewGroup view = (ViewGroup) ac.getWindow().getDecorView();
FrameLayout content = view.findViewById(android.R.id.content);
return content.getChildAt(0);
}
总结:若子布局中有滑动事件,就不能滑动切换布局,只能点击按钮进行切换
Demo地址:https://github.com/mcl22222/HorizontalScrollView