底部dialog弹窗 [泛型适配同种布局不同实体类][6]
先看效果 分割线可以自己在dialog中自己布局
直接上代码
实体类必须实现AInterface 接口
recyclerview
compile 'com.android.support:recyclerview-v7:25.0.1'
如何使用 一句代码
LinkedList<T1> list = new LinkedList<>();
for(int i =0;i<20;i++){
T1 t1 = new T1();
t1.setcName("sfsss");
t1.setAge(i);
list.add(t1);
}
BottomRecycleAdapter<T1> bottomRecycleAdapter = new BottomRecycleAdapter<>(list, new AdapterTCallBack() {
@Override
public void onClickListener(Object o) {
bottomDiaLog.dismiss();
Toast.makeText(BottomActivity.this,((T1)o).getAge()+"",Toast.LENGTH_SHORT).show();
}
});
BottomDiaLog bottomDiaLog = new BottomDiaLog(this,R.style.transparentFrameWindowStyle,"1234",bottomRecycleAdapter);
//显示隐藏
bottomDialog.show();
bottomDialog.dismiss();
主要的adapter 代码
public class BottomRecycleAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private LinkedList<T> list;
private AdapterTCallBack adapterTCallBack;
public BottomRecycleAdapter( LinkedList<T> list, AdapterTCallBack adapterTCallBack){
this.list = list;
this.adapterTCallBack = adapterTCallBack;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.bottom_dialog_item,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
T t = list.get(position);
((ViewHolder) holder).bottom_ll.setOnClickListener(new MyOnclick<>(adapterTCallBack,t));
if(t instanceof AInterface) {
String name = ((AInterface) t).getComName();
if(!TextUtils.isEmpty(name)){
((ViewHolder) holder).tv.setText(name);
}
}
}
@Override
public int getItemCount() {
return list.size();
}
final class ViewHolder extends RecyclerView.ViewHolder {
public TextView tv;
public LinearLayout bottom_ll;
public ViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.bottom_dialog_text);
bottom_ll = (LinearLayout) itemView.findViewById(R.id.bottom_dialog_ll);
}
}
final class MyOnclick<T> implements View.OnClickListener{
private AdapterTCallBack adapterTCallBack;
private T t;
public MyOnclick(AdapterTCallBack adapterTCallBack,T t){
this.adapterTCallBack = adapterTCallBack;
this.t = t;
}
@Override
public void onClick(View v) {
adapterTCallBack.onClickListener(t);
}
}
}
布局代码 [bottom_dialog_item][6]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/bottom_dialog_ll"
android:layout_height="wrap_content">
<TextView
android:id="@+id/bottom_dialog_text"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12345"/>
</LinearLayout>
两个接口
//获取界面公共部分的内容
public interface AInterface {
String getComName();
}
//点击事件
public interface AdapterTCallBack<T> {
void onClickListener(T t);
}
dialog代码
public class BottomDiaLog extends Dialog {
private Context context;
public BottomDiaLog(@NonNull Context context) {
super(context);
this.context = context;
}
public BottomDiaLog(@NonNull Context context, int themeResId) {
super(context, themeResId);
this.context = context;
}
public BottomDiaLog(@NonNull Context context, int themeResId,String title,BottomRecycleAdapter bottomRecycleAdapter) {
super(context, themeResId);
this.context = context;
initView(title,bottomRecycleAdapter);
}
public void initView(String title,BottomRecycleAdapter bottomRecycleAdapter){
View v = LayoutInflater.from(context).inflate(R.layout.bottom_action_layout,null);
TextView textView = (TextView) v.findViewById(R.id.action_list_title);
textView.setText(title);
v.findViewById(R.id.action_list_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.action_list_recycelerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation( LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(bottomRecycleAdapter);
setContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
Window window = getWindow();
//window.setWindowAnimations(R.style.dialogWindowAnim);//设置动画 -----百度一下很多 ~
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;
wl.y =((Activity)context).getWindowManager().getDefaultDisplay().getHeight();
// 以下这两句是为了保证按钮可以水平满屏
wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
// 设置显示位置
onWindowAttributesChanged(wl);
// 设置点击外围解散
setCanceledOnTouchOutside(true);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
样式需要的部分
<style name="transparentFrameWindowStyle" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/photo_choose_bg</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00000000" />
<corners android:radius="20dip" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
public class T1 implements AInterface {
private String cName;
private int age;
@Override
public String getComName() {
return cName;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}