cathome 猫家 开发日记- 选择弹窗

概述

完成了自定义弹窗和gridview 的选择功能。那么把2个组合起来。就成为了一个自定义的弹窗选择器。

1.建立继承与popupwindow的dialog.

2.建立layout,放入一个gridview

3.建立一个item

4.完善custom dialog

1.LSUIPopupWindow

   1.1最主要是接收参数,并传递给adapter。

    1.2 并 给gridview加入单击事件,并完成下面2个函数 

   tempAdapter.ChangeSelector(position);
   tempAdapter.notifyDataSetChanged();
1.3 .实现虚基类定义的虚函数 onclicksumbit.用虚函数来确保必须覆写,这样更严谨。
package com.utils.widgets;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.linson.catshome.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
//1.click 2.custom popup 3.layout:only grid view.4.popupwindow load layout and add to content of popupwindow.
//5.init adapter . set it to gridview 's adapter.
//6.add event for gridview and call adapter change fun and notify.
//7. popupwindow'OnClickSubmit return select and dismiss.
//8.may be ,need custom popupwindow for use name popupSeletor.
public class LSUIPopupSelector extends LSUIPopupWindow
{
    private Context mContext;
    private List<LSUIStructDisplayValue> mData;
    private List<LSUIStructDisplayValue> mSelected;
    private List<LSUIStructDisplayValue> mTempSelected;
    private LSUISelectorAdapter.SingleOrMultiple mtype;
    private GridView mgridview;
    private IPopupSelector mHandle;

    public LSUIPopupSelector(Context context, int heightdp, String title,List<LSUIStructDisplayValue> data,List<LSUIStructDisplayValue> selected,LSUISelectorAdapter.SingleOrMultiple type,IPopupSelector handle)
    {
        super(context, heightdp, title);
        mContext=context;
        mData=data;
        mSelected=selected;
        mtype=type;
        mTempSelected=new ArrayList<>(Arrays.asList(new LSUIStructDisplayValue[mSelected.size()]));//初始化一个数组,并直接给左边变量.方便后面的clone.为什么要这样?
        Collections.copy(mTempSelected, mSelected);
        mHandle=handle;
    }

    @Override
    public void OnClickSubmit(View v)
    {
        //process click submit.
        if(v.getId()==mBtn_submit.getId())
        {
            if (mHandle != null)
            {
                mHandle.OnClickSubmit(mTempSelected);
            }
            dismiss();
        }
    }

    @Override
    public void OnBaseCreated(Bundle savedInstanceState)
    {
        //1.add view to mcontenct 2.new adapter .add event
        View view=View.inflate(mContext, R.layout.lsuipopupselector,null);
        mContentLayout.addView(view);
        mgridview=view.findViewById(R.id.gv_selecotr);

        final LSUISelectorAdapter tempAdapter=new LSUISelectorAdapter(mContext,mData,mTempSelected,mtype);
        mgridview.setAdapter(tempAdapter);

        mgridview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                tempAdapter.ChangeSelector(position);
                tempAdapter.notifyDataSetChanged();
            }
        });
    }

    public interface IPopupSelector
    {
        public void OnClickSubmit(List<LSUIStructDisplayValue> selected);
    }
}

2.

<?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">
    <GridView android:id="@+id/gv_selecotr" android:numColumns="3"
        android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="0dp" android:layout_marginTop="0dp" />
</RelativeLayout>

3. margin top .不知道为什么没效果。干脆放入一个textview来代替margin.

<?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">
    <TextView android:layout_width="match_parent" android:layout_height="10dp"  android:id="@+id/space" android:text=""/>
    <TextView android:layout_width="100dp" android:gravity="center"  android:layout_height="30dp" android:id="@+id/selectorItem"  android:layout_centerHorizontal="true" android:layout_below="@id/space"  android:text="TextView"  />
</RelativeLayout>

4.调用。

int heightdp=(int) (GlobalVariable.screenHeight-(GlobalVariable.popupwindowMargintop)*GlobalVariable.screendensity);
LSUIPopupSelector popupSelector=new LSUIPopupSelector(this,heightdp , "猫亚科", mdataSubClass, mSelectedSubClass, LSUISelectorAdapter.SingleOrMultiple.singler,this);
popupSelector.show();
break;

原文地址:https://www.cnblogs.com/lsfv/p/9856116.html