package com.example.myfirstapp.web.fragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myfirstapp.R;
import com.example.myfirstapp.web.adapter.DialogAdapter;
/**
* 底部弹框Fragment
*/
public class DialogFragment extends androidx.fragment.app.DialogFragment {
private String[] arrs = {"看电影", "去唱歌", "吃吃喝喝", "旅游", "夜蒲聚会", "玩游戏", "运动", "其他",};
private DialogAdapter dialogAdapter;
private ListView listView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//去掉dialog的标题,需要在setContentView()之前
this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = this.getDialog().getWindow();
//去掉dialog默认的padding
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//设置dialog的位置在底部
lp.gravity = Gravity.BOTTOM;
//设置dialog的动画
lp.windowAnimations = R.style.BottomDialogAnimation;
window.setAttributes(lp);
window.setBackgroundDrawable(new ColorDrawable());
View view = inflater.inflate(R.layout.dialog_fragment_bottom, null);
dialogAdapter = new DialogAdapter(getContext(), arrs);
listView = view.findViewById(R.id.listview);
listView.setAdapter(dialogAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//点击监听
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
int size = checkedItemPositions.size();
String result = "";
for (int i1 = 0; i1 < size; i1++) {
if (checkedItemPositions.valueAt(i1)) {
result += checkedItemPositions.keyAt(i1) + "*";
}
}
System.out.println("点击:" + result);
}
});
//取消消除底部弹框
View viewById = view.findViewById(R.id.quxiao);
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
return view;
}
}
– DialogAdapter.java
package com.example.myfirstapp.web.adapter;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import com.example.myfirstapp.R;
/**
* 动态新增数据
*/
public class DialogAdapter extends BaseAdapter {
private Context context;
private String[] text_list;
int tvSelect = 0; //选中项
public DialogAdapter(Context context, String[] text_list) {
this.context = context;
this.text_list = text_list;
}
@Override
public int getCount() {
return text_list.length;//添加条数
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
// System.out.println("**********"+tvSelect);
// //获取对应数据
String text = text_list[i];
View view = View.inflate(context, R.layout.dialog_item, null);
TextView textView = view.findViewById(R.id.tv_1);
textView.setText(text);
return view;
}
}
– dialog_fragment_bottom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="约会项目 (可多选)"
android:textColor="@color/font_color"
android:textSize="15sp"
android:background="@drawable/dialog_item_bg"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scrollbars="none"/>
<TextView
android:id="@+id/quxiao"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="取消"
android:gravity="center"
android:textColor="#FF4E6D"
android:textSize="15sp"
android:background="@color/white"/>
LinearLayout>
– dialog_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:background="#E9E9E9">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="@drawable/dialog_tv_text_color"
android:textSize="14sp"
android:background="@drawable/dialog_tv_bg"/>
LinearLayout>
– dialog_tv_text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/dialog_red" android:state_activated="true"/>
<item android:color="@color/font_color"/>
selector>
– dialog_tv_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/white"/>
selector>
– dialog_item_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topLeftRadius="18dp"
android:topRightRadius="18dp"/>
<solid android:color="@color/white"/>
shape>
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn:
DialogFragment dialogFragment = new DialogFragment();
dialogFragment.show(getSupportFragmentManager(),"age");
break;
}
}