1. 概述
在開發(fā)過程中,對于一些dialog彈窗的效果友驮,如果簡單的漂羊、可以直接實(shí)現(xiàn)的、不需要太多復(fù)雜交互邏輯的卸留,可以直接使用Dialog走越、PopupWindow、DialogFragment實(shí)現(xiàn)耻瑟,對于一些比較復(fù)雜的旨指、需要比較多交互的效果,可以使用Dialog樣式Activity來實(shí)現(xiàn)與Dialog等一樣的效果匆赃,比如下邊自己項(xiàng)目中的:需要點(diǎn)擊對應(yīng)銀行卡后淤毛,再次進(jìn)入時(shí)顯示選中的銀行卡打上對號
自己一開始使用DialogFragment做的,但是由于個(gè)人水平有限算柳,在處理選中銀行卡后再次進(jìn)入時(shí)顯示對應(yīng)銀行卡然后打上對號有點(diǎn)問題低淡,然后采用Dialog樣式Activity來實(shí)現(xiàn)的,具體實(shí)現(xiàn)方式如下
2. 代碼如下
1>:Dialog樣式的Activity如下:
/**
* Email: 2185134304@qq.com
* Created by Novate 2018/7/28 11:37
* Version 1.0
* Params:
* Description: Dialog樣式的Activity
*/
public class DialogActivity extends Activity{
private List<String> mData = new ArrayList<>();
private ListView mListView;
private MyAdapter mAdapter;
@Override
protected void onResume() {
super.onResume();
String mKey = PrefUtils.getString(DialogActivity.this , "key" , "") ;
String position = PrefUtils.getString(DialogActivity.this , "position" , "") ;
Log.e("TAG" , "mKey: " + mKey + ", position: " + position) ;
if (!TextUtils.isEmpty(mKey) && "Y".equals(mKey)){
if (!TextUtils.isEmpty(position)) {
mAdapter.setTag(2, Integer.parseInt(position)); //給adapter設(shè)置綁定 , 表示你點(diǎn)擊某個(gè)具體的item
mAdapter.notifyDataSetChanged();//刷新數(shù)據(jù)
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//窗口對齊屏幕寬度
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.TOP;//設(shè)置對話框置頂顯示
win.setAttributes(lp);
getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mListView = (ListView) findViewById(R.id.listView);
//添加數(shù)據(jù)
for (int i = 0; i < 10; i++) {
mData.add("" + i);
}
//適配MyAdapter數(shù)據(jù)
mAdapter = new MyAdapter(this, mData);
mListView.setAdapter(mAdapter);
//點(diǎn)擊 listview
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mAdapter.setTag(2, position); //給adapter設(shè)置綁定 , 表示你點(diǎn)擊某個(gè)具體的item
mAdapter.notifyDataSetChanged(); //刷新數(shù)據(jù)
// lastPostion = position; //將當(dāng)前位置 置為最后一個(gè)位置
finish();
}
});
}
}
2>:MyAdapter代碼如下:
/**
* Email: 2185134304@qq.com
* Created by Novate 2018/7/28 11:39
* Version 1.0
* Params:
* Description:
*/
public class MyAdapter extends BaseAdapter {
private List<String> mData;
private Context mContext;
private LayoutInflater mInflater;
private int tag;
private int NowDay;
public MyAdapter(Context context, List<String> data) {
this.mContext = context;
this.mData = data;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public void setTag(int s,int NaDayPostion){
tag=s;
NowDay=NaDayPostion;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, null);
holder.mImageView = (ImageView) convertView.findViewById(R.id.image);
holder.iv_check = (ImageView) convertView.findViewById(R.id.iv_check);
holder.mTextView = (TextView) convertView.findViewById(R.id.tvContent);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText("我是:" + mData.get(position));
if(tag==2&&NowDay==position){
holder.iv_check.setVisibility(View.VISIBLE);
holder.iv_check
.setImageResource(R.mipmap.checkedaddress); //選中地址的圖片
// holder.mTextView
// .setTextColor(mContext.getResources().getColor(R.color.colorAccent));
PrefUtils.putString(mContext , "key" , "Y");
PrefUtils.putString(mContext , "position" , position+"");
}else{
holder.iv_check.setVisibility(View.GONE);
holder.iv_check
.setImageResource(R.mipmap.nocheckedaddress);//沒有選中的圖片
}
return convertView;
}
class ViewHolder {
ImageView mImageView , iv_check;
TextView mTextView;
}
}
3. 總結(jié)
個(gè)人覺得對于Dialog樣式的彈窗,如果涉及到的功能蔗蹋、需求何荚、及交互效果比較多的時(shí)候,可以使用 Dialog樣式的Activity實(shí)現(xiàn)即可猪杭,Dialog樣式Activity寫法注意以下幾點(diǎn):
注意:
1>:把布局文件中的根布局background設(shè)置為全透明餐塘,把要顯示數(shù)據(jù)的布局設(shè)置為白色:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ffffff"
android:layout_alignParentBottom="true"
>
</ListView>
</RelativeLayout>
2>:在DialogActivity的onCreate()方法中設(shè)置對話框顯示的大小和位置:
//窗口對齊屏幕寬度
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.TOP;//設(shè)置對話框置頂顯示
win.setAttributes(lp);
getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
4. 記住
如果用dialog能實(shí)現(xiàn),就用dialog實(shí)現(xiàn)皂吮,如果dialog里邊的控件交互比較多戒傻,覺得比較用dialog的話比較復(fù)雜,直接使用Activity樣式的Dialog蜂筹,這個(gè)一定是可以實(shí)現(xiàn)所有dialog樣式的所有需求的需纳。
5. 需要注意的地方
1>:在MainActivity的onCreate中設(shè)置:
//窗口對齊屏幕寬度
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.TOP;//設(shè)置對話框置頂顯示
win.setAttributes(lp);
getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2>:在listview的布局文件中的根布局添加透明屬性:
android:background="#00000000"
3>:在所在的Activity的清單文件中配置:
<activity android:name=".activity.classification.CancelDialogActivity"
android:screenOrientation="portrait"
android:theme="@style/dialogstyle"
/>
<style name="dialogstyle">
<!--設(shè)置dialog的背景-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--設(shè)置Dialog的windowFrame框?yàn)闊o-->
<item name="android:windowFrame">@null</item>
<!--設(shè)置無標(biāo)題-->
<item name="android:windowNoTitle">true</item>
<!--是否浮現(xiàn)在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否半透明-->
<item name="android:windowIsTranslucent">true</item>
<!--設(shè)置窗口內(nèi)容不覆蓋-->
<item name="android:windowContentOverlay">@null</item>
<!--設(shè)置動畫,在這里使用讓它繼承系統(tǒng)的Animation.Dialog-->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<!--背景是否模糊顯示-->
<item name="android:backgroundDimEnabled">true</item>
</style>
4>:MainActivity要么繼承Activity艺挪,然后給該MainActivity設(shè)置這個(gè)主題android:theme="@style/dialogstyle"不翩,或者讓給MainActivity設(shè)置下邊主題,可以繼承其他Activity麻裳,比如AppCompatActivity
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
5>:ListView的背景必須是白色口蝠,否則運(yùn)行結(jié)果將是透明的!
上邊的5個(gè)點(diǎn)必須都要寫津坑,少一個(gè)都會有問題妙蔗!
代碼已上傳至github:
https://github.com/shuai999/DialogActivity.git