快速创建弹窗PopupWindow、提示框AlertDialog、列表对话框Dialog
1.创建PopupWindow:
01.创建PopupWindow的布局文件: 注意:设置了多个Item,不需要时可以选择隐藏
<?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="@color/color_tran">
<LinearLayout
android:layout_width="@dimen/dp_320"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60">
<com.hikvision.showexamplepad101.widget.TextDrawable
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_32"
android:text="IP"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_20" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_32"
android:checked="false"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/dp_32"
android:layout_marginRight="@dimen/dp_32"
android:background="@color/gray_line" />
<RelativeLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60">
<com.hikvision.showexamplepad101.widget.TextDrawable
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_32"
android:text="账号"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_20" />
<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_32"
android:checked="false"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/line2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/dp_32"
android:layout_marginRight="@dimen/dp_32"
android:background="@color/gray_line"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60"
android:visibility="gone">
<com.hikvision.showexamplepad101.widget.TextDrawable
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_32"
android:text="账号"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_20" />
<Switch
android:id="@+id/switch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_25"
android:checked="false"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/line3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/dp_32"
android:layout_marginRight="@dimen/dp_32"
android:background="@color/gray_line"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/layout4"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60"
android:visibility="gone">
<com.hikvision.showexamplepad101.widget.TextDrawable
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_32"
android:text="账号"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_20" />
<Switch
android:id="@+id/switch4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_32"
android:checked="false"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/line4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/dp_32"
android:layout_marginRight="@dimen/dp_32"
android:background="@color/gray_line"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/layout5"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60"
android:visibility="gone">
<com.hikvision.showexamplepad101.widget.TextDrawable
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_32"
android:text="账号"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_20" />
<Switch
android:id="@+id/switch5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_32"
android:checked="false"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
02.创建PopupWindow:
if (null == cameraControlPopWindow)
{
val view = layoutInflater.inflate(R.layout.base_dialog_item_large, null)
view.line1.visibility = if (confType == ConfType.LOCAL_PTOP) View.GONE else View.VISIBLE
view.layout2.visibility = if (confType == ConfType.LOCAL_PTOP) View.GONE else View.VISIBLE
view.tv1.text = "本地会场"
view.tv2.text = "对方会场"
view.layout1.setOnClickListener {
}
view.layout2.setOnClickListener {
}
cameraControlPopWindow = PopupWindow(
view,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
true
)
cameraControlPopWindow!!.setBackgroundDrawable(getDrawable())
cameraControlPopWindow!!.isOutsideTouchable = true
cameraControlPopWindow!!.isFocusable = true
}
if (cameraControlPopWindow!!.isShowing)
{
cameraControlPopWindow!!.dismiss()
} else
{
cameraControlPopWindow!!.showAsDropDown(cameraControlIcon, 0, 10, Gravity.BOTTOM)
}
2.创建AlertDialog:
01.创建AlertDialog的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
android:gravity="center_horizontal"
>
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示带取消、中立和确定按钮的对话框"/>
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示列表的对话框"/>
<Button android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示带单选列表对话框"/>
<Button android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示带多选列表对话框"/>
</LinearLayout>
02.创建AlertDialog
class MainActivity : AppCompatActivity(), OnRefreshListener, OnRefreshLoadMoreListener,
MainContract.View {
private var currentPage = 1
private var mList = ArrayList<Data>()
private lateinit var mJokeAdapter: NewsAdapter
private var refreshLayout: SmartRefreshLayout? = null
private var mJokeListView: RecyclerView? = null
private lateinit var mPresenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
refreshLayout = findViewById(R.id.refreshLayout)
mJokeListView = findViewById(R.id.mJokeListView)
mJokeAdapter = NewsAdapter()
mPresenter = MainPresenter()
initList()
mPresenter.getData(0, 0)
}
override fun onStart() {
super.onStart()
mPresenter.takeView(this)
}
override fun refreshView(orientation: Int, data: List<Data>) {
if (orientation == 0) {
refreshLayout?.finishRefresh()
mList.clear()
mList.addAll(data)
mJokeAdapter.submitList(mList)
mJokeAdapter.notifyDataSetChanged()
} else if (orientation == 1) {
refreshLayout?.finishLoadMore()
val positionStart = mList.size
mList.addAll(data)
mJokeAdapter.submitList(mList)
mJokeAdapter.notifyItemRangeInserted(positionStart, data.size)
}
}
private fun initList() {
refreshLayout?.setRefreshHeader(ClassicsHeader(this))
refreshLayout?.setRefreshFooter(ClassicsFooter(this))
refreshLayout?.setOnRefreshListener(this)
refreshLayout?.setOnRefreshLoadMoreListener(this)
mJokeListView?.layoutManager = LinearLayoutManager(this)
mJokeListView?.adapter = mJokeAdapter
}
override fun onRefresh(refreshLayout: RefreshLayout) {
currentPage = 1
mPresenter.getData(currentPage, 0)
}
override fun onLoadMore(refreshLayout: RefreshLayout) {
currentPage += 1
mPresenter.getData(currentPage, 1)
}
}
3.创建列表对话框Dialog:
01.创建列表的适配器Adapter:
public class DialogListAdapter extends BaseAdapter {
private List<HashMap<String,String>> mapList;
private LayoutInflater inflater;
private Context mContext;
public DialogListAdapter(Context context, List<HashMap<String,String>>list){
this.mContext = context;
this.mapList = list;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mapList.size();
}
@Override
public Object getItem(int position) {
return mapList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
myViewHolder hv;
if (null==convertView){
hv = new myViewHolder();
convertView = inflater.inflate(R.layout.adapter_dialog_view,null,false);
hv.tvName = convertView.findViewById(R.id.tvName);
hv.tvCountry = convertView.findViewById(R.id.tvCountry);
hv.agree = convertView.findViewById(R.id.agree);
hv.disAgree = convertView.findViewById(R.id.disAgree);
convertView.setTag(hv);
}else {
hv = (myViewHolder)convertView.getTag();
}
if (mapList!=null && !mapList.isEmpty()){
hv.tvName.setText(mapList.get(position).get("name"));
hv.tvCountry.setText(mapList.get(position).get("country"));
hv.agree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, mapList.get(position).get("name"), Toast.LENGTH_SHORT).show();
}
});
hv.disAgree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, mapList.get(position).get("country"), Toast.LENGTH_SHORT).show();
}
});
}
return convertView;
}
liebiao
class myViewHolder{
TextView tvName;
TextView tvCountry;
Button agree;
Button disAgree;
}
}
02.创建列表Dialog:
public class MainActivity extends AppCompatActivity {
private DialogListAdapter listAdapter;
private List<HashMap<String, String>> lists = new ArrayList<>();
String[] names = {"Tom", "jane", "kangkang", "mike"};
String[] countries = {"china", "japan", "germany", "usa"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViewDatas();
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_adapter(v);
}
});
}
void initViewDatas() {
listAdapter = new DialogListAdapter(this, lists);
for (int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<>();
map.put("name", names[i % 4]);
map.put("country", countries[i % 4]);
lists.add(map);
}
listAdapter.notifyDataSetChanged();
}
public void show_adapter(View view) {
AlertDialog.Builder adapterBuilder = new AlertDialog.Builder(this);
adapterBuilder.setIcon(R.mipmap.ic_launcher_round);
adapterBuilder.setTitle("This is Adapter Dialog");
ListView listView = new ListView(this);
adapterBuilder.setView(listView);
listView.setAdapter(listAdapter);
adapterBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
adapterBuilder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
adapterBuilder.show();
}
}
|