在嵌套列表视图中使用弹出窗口

时间:2015-10-14 08:55:24

标签: java android android-listview baseadapter

我是Android新手,在我的应用中,我正在尝试添加一个功能,当用户点击格式化为按钮的布局时会出现弹出窗口。

问题在于,在我希望实现这一目标的活动中,我列出了每个都适应自己独特的定制基础适配器的列表视图。

我觉得这对我的应用来说是一个非常低效和繁重的设计,但到目前为止我想不出更好的方法来实现我的目标。我所有的其他功能都运行良好(就他们对“点击”的反应非常好而言)。我还通过记录popup.isShown()检查了弹出窗口的可见性,它告诉我单击layout后确实如此。

我现在可以猜到的一件事是将根视图(活动的视图)传递到我正在制作弹出窗口的位置,但我认为这不是一个好主意,因为有几个原因,我相信一个因为我已经通过参数将大量信息传递给我的“内部”适配器,所以可能会损坏/操纵数据。

那么,是否可以让我深入了解我的应用程序更好更轻的结构设计以及解决弹出窗口问题的解决方案?谢谢你。

简化为:

- 我的应用程序有一个名为“CoffeeSelectActivity”的活动,其中显示所选咖啡馆提供的饮品列表(在列表视图中显示,ID为“drinks_list”

- 这个列表视图被item_cafe_menu夸大了。为此,我创建了一个名为DrinksItem

的自定义baseadatper

-In item_cafe_menu,我有一个列表视图,显示正在选择的杯子以及客户希望添加的特定选项(额外拍摄等),当用户点击列表项{{1时}}。为此,我使用名为drinks_list

DrinksItem内的私有类

- 在Dropdown课程中,我为Dropdown中的dropdown_options_list列表视图实施了另一个自定义的baseadapter,名为item_cafe_menu_dropdown.xml。这将使用选项名称后跟选项填充所选饮品的可用选项,例如,选择大而不是常规尺寸,DropdownOptionsItem文本视图将显示dropdown_option_name和{{1} } textview将显示Size

- 要实现此功能,我想在用户点击dropdown_option_selected_option时显示一个弹出窗口。所以我在Large中为此布局添加了一个onclick事件,当前未在我的设备上显示任何内容,但dropdown_option_selection_layout显示DropdownOptionsItem Log

- 我知道这是一个非常讨厌的代码,所以我想深入了解改进代码并让这个弹出窗口显示在屏幕上。

代码:

true

isShowing()

CoffeeSelectActivity.java

//code snipplet that sets adapter to my list
drinksListAdapter = new DrinksItem(this, menuList);
drinksList.setAdapter(drinksListAdapter);

activity_coffee_select.xml

<ListView
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@id/drinks_list">
</ListView>

DrinksItem.java

package com.morning_coffee.morningmorning;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by user on 15. 10. 11..
 */
public class DrinksItem extends BaseAdapter {

    private Context context;
    private ArrayList<DrinkInfo> drinkInfos;

    private static class ViewHolder {
        public LinearLayout layout, dropdown;
        public TextView name, price;
    }

    public DrinksItem(Context context, ArrayList<DrinkInfo> drinkInfos) {
        super();
        this.context = context;
        this.drinkInfos = drinkInfos;
    }

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

    @Override
    public Object getItem(int position) {
    return drinkInfos.get(position).getDrinkName();
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    //reuse views
    if(convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_cafe_menu, null);

        holder = new ViewHolder();
        holder.layout = (LinearLayout) convertView.findViewById(R.id.menu_layout);
        holder.name = (TextView) convertView.findViewById(R.id.menu_name);
        holder.price = (TextView) convertView.findViewById(R.id.menu_price);

        setOnClickEvents(convertView, holder, position);

        convertView.setTag(holder);
    }

    //fill data
    holder = (ViewHolder) convertView.getTag();

    holder.name.setText(drinkInfos.get(position).getDrinkName());
    holder.price.setText(drinkInfos.get(position).getDrinkPrice());

    return convertView;
}

private void setOnClickEvents(final View view, final ViewHolder viewHolder, final int position) {
    viewHolder.layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewGroup group = (ViewGroup)view.findViewById(R.id.menu_dropdown_layout);

            if(viewHolder.dropdown == null) {
                //show dropdown menu
                viewHolder.dropdown = new Dropdown(context, drinkInfos, group, viewHolder.dropdown, position);

                //set bottom margin
                ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) viewHolder.layout.getLayoutParams();
                p.bottomMargin = 13;
            }
            else {
                //remove dropdown menu and clear child views from viewgroup
                viewHolder.dropdown = null;
                group.removeAllViews();

                //set bottom margin
                ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) viewHolder.layout.getLayoutParams();
                p.bottomMargin = 0;
            }
        }
    });
}

private class Dropdown extends LinearLayout {
    private ListView options;
    private ImageButton dec, inc;
    private Button basket;
    private TextView count;
    private ArrayList<DrinkInfo> info;

    private class ViewHolder {
        public ListView op;
        private ImageButton d, i;
        private Button b;
        private TextView c;
    }

    public Dropdown(Context context, ArrayList<DrinkInfo> info, ViewGroup group, View view, int position) {
        super(context);
        ViewHolder holder;

        if(group.getChildCount() == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.item_cafe_menu_dropdown, group);
            holder = new ViewHolder();

            holder.d = (ImageButton) view.findViewById(R.id.dropdown_decrement);
            holder.c = (TextView) view.findViewById(R.id.dropdown_count);
            holder.i = (ImageButton) view.findViewById(R.id.dropdown_increment);
            holder.op = (ListView) view.findViewById(R.id.dropdown_options_list);
            holder.b = (Button) view.findViewById(R.id.dropdown_basket_button);

            String[] splitOptions = info.get(position).getDrinkOptions().split(":");
            DropdownOptionsItem adapter = new DropdownOptionsItem(context, Arrays.asList(splitOptions));
            holder.op.setAdapter(adapter);

            setOnClickEvents(holder, position);

            view.setTag(holder);
        }

        this.info = info;

        holder = (ViewHolder) view.getTag();
        holder.c.setText(info.get(position).getDrinkCount());
    }

    private void setOnClickEvents(final ViewHolder viewHolder, final int position) {
        viewHolder.d.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int t = Integer.parseInt(info.get(position).getDrinkCount());
                if(t > 1) {
                    info.get(position).setDrinkCount((t - 1) + "");
                    viewHolder.c.setText(info.get(position).getDrinkCount());
                }
            }
        });

        viewHolder.i.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    int t =     Integer.parseInt(info.get(position).getDrinkCount());

                    info.get(position).setDrinkCount((t + 1) + "");
                    viewHolder.c.setText(info.get(position).getDrinkCount());
                }
            });

            viewHolder.b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("dropdown vh", "basket");
                }
            });
        }
    }
}

item_cafe_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@id/menu_layout">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="40dp"
            android:id="@id/menu_name"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20dp"
            android:id="@id/menu_price" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@id/menu_dropdown_layout" >
    </LinearLayout>

    <View
        android:background="#ffc8c8c8"
        android:layout_width="fill_parent"
        android:layout_height="1.0dip" />
</LinearLayout>

item_cafe_menu_dropdown.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="22.0dip"
                android:text="count"
                android:textSize="15.0dip"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@id/dropdown_decrement"
                android:src="@mipmap/btn_control_circlebtn_nor_minus"
                android:background="@null" />

            <TextView
                android:id="@id/dropdown_count"
                android:layout_width="54.0dip"
                android:layout_height="22.0dip"
                android:textSize="15.0dip"
                android:gravity="center" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@id/dropdown_increment"
                android:src="@mipmap/btn_control_circlebtn_nor_plus"
                android:background="@null" />
        </LinearLayout>
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@id/dropdown_options_list" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@id/dropdown_basket_button"
        android:text="Basket" />
</LinearLayout>

DropdownOptionsItem.java

package com.morning_coffee.morningmorning;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by user on 15. 10. 12..
 */
public class DropdownOptionsItem extends BaseAdapter {

    private Context context;
    private List<String> options;

    private static class ViewHolder {
        public TextView name, option;
        public LinearLayout layout;
    }

    public DropdownOptionsItem(Context context, List<String> options) {
        super();
        this.context = context;
        this.options = options;
    }

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

    @Override
    public Object getItem(int position) {
        return options.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        //initialize if convertView is null
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cafe_menu_dropdown_option, null);
            holder = new ViewHolder();

            holder.name = (TextView) convertView.findViewById(R.id.dropdown_option_name);
            holder.option = (TextView) convertView.findViewById(R.id.dropdown_option_selected_option);
            holder.layout = (LinearLayout) convertView.findViewById(R.id.dropdown_option_selection_layout);

            setOnClickEvnets(holder, position);

            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();

        //fill data
        holder.name.setText(options.get(position));
        holder.option.setText(options.get(position+1));

        notifyDataSetChanged();

        return convertView;
    }

    private void setOnClickEvnets(final ViewHolder viewHolder, final int position) {
        viewHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout l = new LinearLayout(context);
                TextView tv = new TextView(context);
                tv.setText("popup");
                l.addView(tv);

                PopupWindow popup = new PopupWindow();
                popup.setContentView(l);

                popup.getContentView().bringToFront();
                popup.showAsDropDown(viewHolder.layout);
                popup.setWidth(300);
                popup.setHeight(300);
                Log.d("popup", popup.isShowing() + "");
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我已经通过使用startActivityForResult

解决了这个问题