美文网首页
AndroidStudio下实现包含ListView的弹出窗口

AndroidStudio下实现包含ListView的弹出窗口

作者: 牛萌姐 | 来源:发表于2017-06-08 15:49 被阅读0次

    工程中需要选择文件夹中的多个文件,这个功能可以通过PopupWindow结合ListView实现。
    编写工程中需要的布局文件。
    主窗口布局activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="dell.popwindow.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
        <Button
            android:id="@+id/main_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/显示"/>
    </LinearLayout>
    

    弹出窗口布局poptest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ListView
            android:id="@+id/pop_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/pop_ok"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/确定"/>
            <Button
                android:id="@+id/pop_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/取消"/>
        </LinearLayout>
    
    </LinearLayout>
    

    列表子项布局listview_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/item_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/item_name"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/item_cb"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    编写列表适配器代码

    public class PopAdapter extends BaseAdapter {
        private Context context;
        private List<String> list;
        public static Map<Integer,Boolean> isSelected;
        public PopAdapter(Context context){
            this.context = context;
            list = new ArrayList<>();
            list.add("appale");
            list.add("banana");
            list.add("orange");
            list.add("pear");
            list.add("cherry");
            isSelected = new HashMap<Integer, Boolean>();
            for(int i=0;i<list.size();i++){
                isSelected.put(i,false);
            }
        }
    
    
        @Override
        public int getCount(){
            return list.size();
        }
        @Override
        public Object getItem(int pos){
            return list.get(pos);
        }
        @Override
        public long getItemId(int pos){
            return pos;
        }
        public final class ViewHolder{
            public ImageView img;
            public TextView name;
            public CheckBox checkBox;
        }
        @Override
        public View getView(int pos, View converView, ViewGroup parent){
            ViewHolder holder;
            View view;
            if(converView == null){
                holder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.listview_item,null);
                holder.img = (ImageView)view.findViewById(R.id.item_img);
                holder.name = (TextView)view.findViewById(R.id.item_name);
                holder.checkBox = (CheckBox)view.findViewById(R.id.item_cb);
                view.setTag(holder);
            }else{
                
                view = converView;
                holder = (ViewHolder)converView.getTag();
            }
            holder.img.setImageResource(R.drawable.document);
            holder.name.setText(list.get(pos));
            holder.checkBox.setChecked(isSelected.get(pos));
            return view;
        }
    
    }
    

    由于是简单的实现所以列表中的项目为事先编辑好的,实际应用中可以根据需要改为动态添加。
    最后是弹出窗口的实现

    public class MainActivity extends AppCompatActivity {
        private PopupWindow window;
        private List<String> file_list;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button show = (Button)findViewById(R.id.main_show);
    
            show.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    popWindow(v);
                }
            });
        }
    
        private void closeWindow(boolean submit){
            if(submit){
                for(int i=0;i<file_list.size();i++){
                    Log.i("名称",file_list.get(i));
                }
            }
            if(window!=null){
                window.dismiss();
            }
        }
    
        private void popWindow(View parent){
            file_list = new ArrayList<>();
            LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = layoutInflater.inflate(R.layout.poptest,null);
            ListView listView = (ListView)view.findViewById(R.id.pop_list);
            Button ok = (Button)view.findViewById(R.id.pop_ok);
            Button cancel = (Button)view.findViewById(R.id.pop_cancel);
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    closeWindow(true);
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    closeWindow(false);
                }
            });
            final PopAdapter popadpter = new PopAdapter(this);
            listView.setAdapter(popadpter);
            listView.setItemsCanFocus(false);
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    PopAdapter.ViewHolder holder = (PopAdapter.ViewHolder)view.getTag();
                    holder.checkBox.toggle();
                    popadpter.isSelected.put(i,holder.checkBox.isChecked());
                    if(holder.checkBox.isChecked()){
                        file_list.add(holder.name.getText().toString());
                    }else{
                        file_list.remove(holder.name.getText().toString());
                    }
                }
            });
    
            window = new PopupWindow(view,500,300);
            window.setFocusable(true);
            window.update();
            window.showAtLocation(parent, Gravity.CENTER_VERTICAL,0,0);
        }
    
    }
    

    其中因为考虑到文件夹中的文件可能发生变化,所以在每次执行popWindow函数时都重新创建了适配器对象和其他相关对象,如果适配器内容是固定的,则更好的写法是

        private void popWindow(View parent){
            file_list = new ArrayList<>();
            if(window == null){
                LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = layoutInflater.inflate(R.layout.poptest,null);
                ListView listView = (ListView)view.findViewById(R.id.pop_list);
                Button ok = (Button)view.findViewById(R.id.pop_ok);
                Button cancel = (Button)view.findViewById(R.id.pop_cancel);
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        closeWindow(true);
                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        closeWindow(false);
                    }
                });
                final PopAdapter popadpter = new PopAdapter(this);
                listView.setAdapter(popadpter);
                listView.setItemsCanFocus(false);
                listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        PopAdapter.ViewHolder holder = (PopAdapter.ViewHolder)view.getTag();
                        holder.checkBox.toggle();
                        popadpter.isSelected.put(i,holder.checkBox.isChecked());
                        if(holder.checkBox.isChecked()){
                            file_list.add(holder.name.getText().toString());
                        }else{
                            file_list.remove(holder.name.getText().toString());
                        }
                    }
                });
        
                window = new PopupWindow(view,500,300);
            }
            window.setFocusable(true);
            window.update();
            window.showAtLocation(parent, Gravity.CENTER_VERTICAL,0,0);
        }
    

    此时需要重新构建关闭弹出窗口时的代码,否则再次弹出的窗口中的checkbox的状态为上一次的状态。
    总结一下完成此项目主要包含两个部分,第一部分为ListView,此部分的基本组成为listview布局、列表子项布局、适配器和在活动中的实现部分。第二部分为弹出窗口,这部分较为简单主要是PopupWindow的相应配置。

    参考文章
    http://gqdy365.iteye.com/blog/994746
    http://blog.csdn.net/wangkuifeng0118/article/details/7368157

    相关文章

      网友评论

          本文标题:AndroidStudio下实现包含ListView的弹出窗口

          本文链接:https://www.haomeiwen.com/subject/lrbzfxtx.html