自定义弹出Dialog框

                              自定义弹出Dialog框

效果图:


main.xml文件:

<Button
    android:id="@+id/btn_4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="自定义dialog" />
自定义dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="店铺列表"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="34dp" />
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

listview子item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
    android:layout_weight="1"
    android:layout_marginTop="20dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_name"
        android:text="王府井 - 小尕子店"
        android:textSize="20sp"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_marginTop="5dp"
        android:id="@+id/tv_dizhi"
        android:text="朝阳莱锦文化园cf25"
        android:textSize="15sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
    <TextView
        android:layout_gravity="center"
        android:id="@+id/tv_gongli"
        android:text=">330km"
        android:textSize="25sp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>
main方法:
public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private List<Bean> list;
    private Button btn_4;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog4();
            }
        });

    }
    private void myDialog4() {
        // 使用layout布局文件 得到一个view对象
        //区别:View类中的inflate方法  封装了LayoutInflater的使用
        View view = View.inflate(this, R.layout.dialog_view, null);
//    View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
        //初始化控件
        lv = (ListView) view.findViewById(R.id.lv);
        //初始化数据
        initData();
        //创建一个Adapter对象
        adapter = new MyAdapter(this,list);
        //绑定
        lv.setAdapter(adapter);
        // 1、创建builder
        // 为什么要传上下文参数? 因为dialog的显示要依托于Activity
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

       // builder.setTitle("店铺列表");
        // 让builder加载一个view对象 也就是一个布局对象    自定义布局属于  内容部分
        builder.setView(view);
        final AlertDialog dialog = builder.show();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //关闭dialog对话框
                dialog.cancel();
            }
        });

    }

    private void initData() {
        list = new ArrayList<>();
        for (int i =0;i<6;i++){
            Bean bean = new Bean("朝阳莱锦文化园cf25"+i,">330km"+i,"王府井 - 小尕子店"+i);
            list.add(bean);
        }
    }

}
适配器:
public class MyAdapter extends BaseAdapter{
    Context context;
    List<Bean>list = new ArrayList<>();

    public MyAdapter(Context context, List<Bean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
        if (convertView==null){
            holder = new ViewHolder();
            convertView = View.inflate(context,R.layout.item,null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_dizhi = convertView.findViewById(R.id.tv_dizhi);
            holder.tv_gongli = convertView.findViewById(R.id.tv_gongli);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        holder.tv_name.setText(list.get(position).name);
         holder.tv_dizhi.setText(list.get(position).dizhi);
            holder.tv_gongli.setText(list.get(position).gongli);
        }
        return convertView;
    }
    public static class ViewHolder{
            TextView tv_name,tv_dizhi,tv_gongli;
    }

}
Bean类:
public class Bean {
    String name,dizhi,gongli;

    public Bean(String dizhi, String gongli, String name) {
        this.dizhi = dizhi;
        this.gongli = gongli;
        this.name = name;
    }

    public Bean() {
    }

    @Override
    public String toString() {
        return "Bean{" +
                "dizhi='" + dizhi + '\'' +
                ", name='" + name + '\'' +
                ", gongli='" + gongli + '\'' +
                '}';
    }

    public String getDizhi() {
        return dizhi;
    }

    public void setDizhi(String dizhi) {
        this.dizhi = dizhi;
    }

    public String getGongli() {
        return gongli;
    }

    public void setGongli(String gongli) {
        this.gongli = gongli;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
 
 









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值