PopupWindow

本文介绍了一种基于Android的图片选择器实现方法,通过自定义PopupWindow展示文件夹列表供用户选择,并详细展示了如何使用ListView及适配器加载文件夹数据,同时实现了点击事件监听以切换选中状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PopupWindow

R.layout.imageup_pop

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="5dp" >

    <ListView
        android:id="@id/imgupPopLv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#eee3d9"
        android:dividerHeight="1px" />

</LinearLayout>

DirPopupWindow

public class DirPopupWindow extends PopupWindow{

    private int mWidth;
    private int mHeight;
    private ListView mListView;
    private List<FolderBean> mData;
    private FolderBean preSelFolder;

    public DirPopupWindow(Context context, List<FolderBean> data) {
        super(context);
        mData = data;
        calWidthAndHeight(context);
        initViews(context);
        initListener();
    }

    private void calWidthAndHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        mWidth = metrics.widthPixels;
        mHeight = (int) (metrics.heightPixels*0.7);
    }

    private void initViews(Context context) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.imageup_pop,null);
        setContentView(contentView);
        setWidth(mWidth);
        setHeight(mHeight);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE){
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        mListView = (ListView) contentView.findViewById(R.id.imgupPopLv);
        mListView.setAdapter(new DirAdapter(context,R.layout.imageup_pop_item,mData));
    }

    private void initListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if (null != mLisetner) {
                    if (null != preSelFolder) {
                        preSelFolder.setSel(false);
                    }
                    FolderBean bean = (FolderBean) parent
                            .getItemAtPosition(position);
                    bean.setSel(true);
                    preSelFolder = bean;
                    mLisetner.onSelected(bean);
                }
            }
        });
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);

    }

    private class DirAdapter extends CommonAdapter<FolderBean>{

        public DirAdapter(Context context, int layoutId, List<FolderBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, FolderBean item, int position) {
            ImageView img = holder.getView(R.id.popIvYl);
            img.setImageResource(R.mipmap.pictures_no);
            ImageLoader.getInstance().loadImage(item.getFirstImgPath(), img);
            holder.setText(R.id.popTvDirName, item.getDirName());
            holder.setText(R.id.popTvDirImgCount, item.getImgCountStr());

            img = holder.getView(R.id.popImgSel);
            int visible = item.isSel() ? View.VISIBLE : View.GONE;
            img.setVisibility(visible);
        }
    }

    private onDirSelectedListener mLisetner;

    public interface onDirSelectedListener {
        public void onSelected(FolderBean folder);
    }

    public void setOnDirSelectedLisetner(onDirSelectedListener mLisetner) {
        this.mLisetner = mLisetner;
    }
}

R.style.popupwindow_anim

 <style name="popupwindow_anim">
        <item name="android:windowEnterAnimation">@anim/slide_up</item>
        <item name="android:windowExitAnimation">@anim/slide_down</item>
    </style>

slide_down

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="100%" />

</set>

slide_up

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:fromXDelta="0"
        android:fromYDelta="100%"
        android:toXDelta="0"
        android:toYDelta="0" />

</set>

R.layout.imageup_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/LinearLayout1"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

    <GridView
        android:id="@id/imgupGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@color/transparent"
        android:horizontalSpacing="3dp"
        android:listSelector="@color/transparent"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="3dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#a000"
        android:clipChildren="true">

        <TextView
            android:id="@id/imgupTvDirName"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:drawableRight="@drawable/tree"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:text="所有文件"
            android:textColor="@color/white"
            android:textSize="16sp"/>

        <TextView
            android:id="@id/imgupTvDirCount"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="center_vertical"
            android:paddingRight="10dp"
            android:textColor="@color/white"
            android:textSize="16sp"/>
    </RelativeLayout>

</RelativeLayout>
tvDirName.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mDirPopupWindow.setAnimationStyle(R.style.popupwindow_anim);
                // 设置显示位置
                mDirPopupWindow.showAsDropDown(tvDirName, 0, 0);

                // 设置显示内容区域变暗
                lightSwitch(0.3f);
            }
        });

private void initDirPopupWindow() {
        mDirPopupWindow = new DirPopupWindow(this, mFolderBeans);
        mDirPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                lightSwitch(1.0f);
            }
        });

        mDirPopupWindow.setOnDirSelectedLisetner(new DirPopupWindow.onDirSelectedListener() {
            @Override
            public void onSelected(FolderBean folder) {
                gridViewDatas(folder);
                data2View(folder);
                mDirPopupWindow.dismiss();
            }
        });
    }

    /**
     * 内容区域明暗度设置
     */
    private void lightSwitch(float alpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().setAttributes(lp);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值