Android 通用Dialog中设置RecyclerView
通用Dialog
public class IOSRecyclerViewDialog{
private Context context;
private Dialog dialog;
private LinearLayout lLayout_bg;
private TextView txt_title;
private TextView txt_msg;
private Button btn_neg;
private Button btn_pos;
//private ImageView img_line;
private Display display;
private boolean showTitle = false;
private boolean showMsg = false;
private boolean showPosBtn = false;
private boolean showNegBtn = false;
private View view;
private FrameLayout frameLayout;
public IOSRecyclerViewDialog(Context context) {
this.context = context;
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
}
public IOSRecyclerViewDialog builder() {
view = LayoutInflater.from(context).inflate(R.layout.dialog_recycler_view, null);
lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_title.setVisibility(View.GONE);
txt_msg = (TextView) view.findViewById(R.id.txt_msg);
txt_msg.setVisibility(View.GONE);
btn_neg = (Button) view.findViewById(R.id.btn_neg);
btn_neg.setVisibility(View.GONE);
btn_pos = (Button) view.findViewById(R.id.btn_pos);
btn_pos.setVisibility(View.GONE);
frameLayout = (FrameLayout) view.findViewById(R.id.customPanel);
// ????Dialog????????
dialog = new Dialog(context, R.style.AlertDialogStyle);
dialog.setContentView(view);
dialog.setCancelable(false);
// ????dialog????????
lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));
return this;
}
public IOSRecyclerViewDialog setCustomView(View v, LayoutParams params){
if (frameLayout.getChildCount() > )
frameLayout.removeAllViews();
txt_msg.setVisibility(View.GONE);
frameLayout.addView(v, params);
return this;
}
public IOSRecyclerViewDialog setTitle(String title) {
showTitle = true;
if ("".equals(title)) {
txt_title.setText("????");
} else {
txt_title.setText(title);
}
return this;
}
public IOSRecyclerViewDialog setMsg(String msg) {
frameLayout.setVisibility(View.GONE);
showMsg = true;
if ("".equals(msg)) {
txt_msg.setText("????");
} else {
txt_msg.setText(msg);
}
return this;
}
public IOSRecyclerViewDialog setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
public IOSRecyclerViewDialog setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}
public IOSRecyclerViewDialog setPositiveButton(String text,
final View.OnClickListener listener) {
showPosBtn = true;
if ("".equals(text)) {
btn_pos.setText("???");
} else {
btn_pos.setText(text);
}
btn_pos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null)
listener.onClick(v);
dialog.dismiss();
}
});
return this;
}
public IOSRecyclerViewDialog setNegativeButton(String text,
final View.OnClickListener listener) {
showNegBtn = true;
if ("".equals(text)) {
btn_neg.setText("???");
} else {
btn_neg.setText(text);
}
btn_neg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null)
listener.onClick(v);
dialog.dismiss();
}
});
return this;
}
private void setLayout() {
if (!showTitle && !showMsg) {
txt_title.setText("???");
txt_title.setVisibility(View.VISIBLE);
}
if (showTitle) {
txt_title.setVisibility(View.VISIBLE);
}
if (!showPosBtn && !showNegBtn) {
btn_pos.setText("???");
btn_pos.setVisibility(View.VISIBLE);
//btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
btn_pos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if (showPosBtn && showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
//btn_pos.setBackgroundResource(R.drawable.alertdialog_right_selector);
btn_neg.setVisibility(View.VISIBLE);
//btn_neg.setBackgroundResource(R.drawable.alertdialog_left_selector);
//img_line.setVisibility(View.VISIBLE);
}
if (showPosBtn && !showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
//btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
}
if (!showPosBtn && showNegBtn) {
btn_neg.setVisibility(View.VISIBLE);
//btn_neg.setBackgroundResource(R.drawable.alertdialog_single_selector);
}
}
public void show() {
setLayout();
dialog.show();
}
}
其中用到的资源文件 dialog_recycler_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lLayout_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/alert_bg"
android:orientation="vertical" > <TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30px"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="50px"
android:textStyle="bold" /> <TextView
android:id="@+id/txt_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="16sp" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/customPanel"/> <ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/alertdialog_line" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="20px"> <!--<Button-->
<!--android:id="@+id/btn_neg"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="43dp"-->
<!--android:layout_weight=""-->
<!--android:background="@drawable/alertdialog_left_selector"-->
<!--android:gravity="center"-->
<!--android:textColor="@color/actionsheet_blue"-->
<!--android:textSize="16sp" />--> <!--<ImageView-->
<!--android:id="@+id/img_line"-->
<!--android:layout_width="0.5dp"-->
<!--android:layout_height="43dp"-->
<!--android:background="@color/alertdialog_line" />--> <!--<Button-->
<!--android:id="@+id/btn_pos"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="43dp"-->
<!--android:layout_weight=""-->
<!--android:background="@drawable/alertdialog_right_selector"-->
<!--android:gravity="center"-->
<!--android:textColor="@color/actionsheet_blue"-->
<!--android:textSize="16sp"-->
<!--/>--> <Button
android:layout_width="wrap_content"
android:layout_height="120px"
android:layout_marginTop="90px"
android:layout_weight=""
android:gravity="center"
android:layout_marginLeft="60px"
android:layout_marginRight="60px"
style="@style/text_white.58"
android:textColor="@color/colorPrimaryDark"
android:background="@drawable/regist_selector_bg"
android:text="@string/logingbutton"
android:id="@+id/btn_neg"
android:layout_below="@+id/forget_password"/>
<Button
android:layout_width="wrap_content"
android:layout_height="120px"
android:layout_marginTop="90px"
android:layout_weight=""
android:gravity="center"
style="@style/text_white.58"
android:layout_alignParentRight="true"
android:layout_marginRight="60px"
android:background="@drawable/btn_circle_seletor"
android:text="注册"
android:id="@+id/btn_pos"
android:layout_below="@+id/forget_password"/>
</LinearLayout> </LinearLayout> <style name="AlertDialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>
使用
RecyclerView recyclerView = new RecyclerView(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(homeAdapter);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
homeAdapter.getItemCount() * Utils.getHeightInPx(this)/); new IOSRecyclerViewDialog(this).builder()
.setCancelable(false)
.setCanceledOnTouchOutside(false)
.setCustomView(recyclerView, lp)
.setTitle(selectedBrand.getName())
.setPositiveButton(getString(R.string.add_attention), v ->
presenter.saveData()
).setNegativeButton(getString(R.string.cancle), v -> {
presenter.getSelectedData().clear();
presenter.getSelectedData().addAll(originalAllSelected);
presenter.saveData();
})
.show();
Android 通用Dialog中设置RecyclerView的更多相关文章
- Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。
根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性. 我们知道在xml中设置的方法为:android:d ...
- Android 一个TextView中设置多种不同大小的字体,设置超链接
以前项目中要是遇到这样的UI设计,都是傻不拉唧的分为三个TextView来实现,今天在微信中无意中看了一篇公众号文章,发现原来只要一个TextView就可以搞定啦,人生最悲哀的事情莫过于工作了这么久啦 ...
- Android 在onActivityResult()中设置图片setImageResource(resId) 或者改变view属性,不成功的解决办法
如果试验过的朋友就会发现,在onActivityResult()中设置这些属性,好像都不工作,虽然我死磕一番还是不知道具体原因,我直接默认它可能就是不能在里面设置,所以就只能在其他地方设置,幸好发现A ...
- Android:Dialog中隐藏键盘的注意事项
场景:弹出一个Dialog.里面有一个EditText.用来输入内容.由于输入时.须要弹出键盘.所以当Dialog消失时.键盘要一起隐藏. 如今我们做一个自己定义的Dialog MyDialog ex ...
- android 在代码中设置字体颜色 问题
项目中需要在代码中控制字体颜色 之前是直接引用资源文件 但是不行 tv.setTextColor(R.color.textColor_black); 无效果 后来在网上找了资料发现 要从reso ...
- Android 自定义Dialog中加EditText弹不出键盘跟Dialog遮挡键盘的问题
先上两张图 第一张问题很明显,第二张是成功的图, 其实第一张是加了 //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INP ...
- 【Android 界面效果47】RecyclerView详解
RecylerView作为 support-library发布出来,这对开发者来说绝对是个好消息.因为可以在更低的Android版本上使用这个新视图.下面我们看如何获取 RecylerView.首先打 ...
- Android studio 安装中遇到一些问题的解决办法,分享一下
从eclipse转到android studio也是很无耐,刚开始总是会遇到很多难题,但是都不要轻言放弃. 以下是我遇到的问题,并通过搜索引擎找到的解决办法,善用工具,善用头脑,勿为伸手之人. And ...
- 浅析Android Dialog中setContentView()方法
2017-05-15 概述 Dialog在Android中是一个很优秀的工具.在使用Dialog时,我们一般都会自定义要显示的内容布局.Dialog自带了三个方法来支持自定义内容布局. public ...
随机推荐
- 配置Jupyter
前几天见同学有用Jupyter notebook的,有点喜欢,于是今天自己配了一下. Jupyter是一个非常好用编辑器,因为Jupyter notebook 不仅可以编写代码运行,并且可以直接在代码 ...
- BZOJ 2085 luogu P3502 [POI2010]Hamsters (KMP、Floyd、倍增)
数组开小毁一生-- 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2085 这题在洛谷上有个条件是"互不包含",其实 ...
- ACDream - Crayon
题目: Description There are only one case in each input file, the first line is a integer N (N ≤ 1,000 ...
- RedisTemplate Api总结
RedisTemplate 使用总结 最近在做一个项目,考虑到有累计,排行,缓存等功能:而Redis是一个基于内存的数据库,而且提供了 字符串(String), 哈希(Map), 列表(list), ...
- Spring 学习二-----AOP的原理与简单实践
一.Spring AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...
- 实践一些js中的prototype, __proto__, constructor
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- 洛谷——P1616 疯狂的采药
https://www.luogu.org/problem/show?pid=1616#sub 题目背景 此题为NOIP2005普及组第三题的疯狂版. 题目描述 LiYuxiang是个天资聪颖的孩子, ...
- POJ 3207
还是那句话,做2SAT题时,找出矛盾点基本上可解了.这道题也是这样 题意是说给出一个圆上的 n 个点(0~n-1编号),然后在指定的 m 对点之间各连一条线(可以在圆内,也可以在圆外,可以是曲线,这点 ...
- RubyMine快捷键
RubyMine快捷键 ctrl+shift+up/down 向上/向下移动代码段 alt+shift+up/down 向上/向下移动代码行 ctrl+y 删除一行 ctrl+shift+f10 运行 ...
- Hibernate单表操作(一)——单一主键
assigned由java应用程序负责生成.(手工赋值) native由底层数据库自己主动生成标识符,假设是MySQL就是increment,假设是oracle就是sequence.等等.