Java dialogfragment中的Android ListView

Java dialogfragment中的Android ListView,java,android,android-fragments,Java,Android,Android Fragments,我需要在DialogFragment中放置两个ListView并使其可滚动。第一个listview位于红线上方,第二个位于红线下方。对于普通片段,我通常使用代码中的setDynamicHeight方法,它工作得非常好。我重写了使全屏显示为我的DialogFragment的OnStart()方法。出了问题,产生了一种影响: 第二个列表视图(proposalProductsList在我的代码中)仅位于黄色区域,但可滚动。看起来剩余的区域从未使用过。我知道在片段生命周期中OnStart()方法比On

我需要在DialogFragment中放置两个ListView并使其可滚动。第一个listview位于红线上方,第二个位于红线下方。对于普通片段,我通常使用代码中的
setDynamicHeight
方法,它工作得非常好。我重写了使全屏显示为我的DialogFragment的
OnStart()
方法。出了问题,产生了一种影响:

第二个列表视图(
proposalProductsList
在我的代码中)仅位于黄色区域,但可滚动。看起来剩余的区域从未使用过。我知道在片段生命周期中
OnStart()
方法比
OnCreateView()
晚,但当我试图将代码从
OnStart()
全屏放到
OnCreateView()
OnAttach()
时,全屏不起作用(DialogFragment的大小与此类似,在黄色框下没有区域)

我的代码是:

public class MealCreator extends DialogFragment {

    String dayType,mealNumber;
    ListView mealProductsList, proposalProductsList;
    ImageView proteinButton, carbButton, fatButton, vegetableButton;
    View view;
    ViewGroup header;
    ProposalProductsForMealAdapter proposalProductsForMealAdapter;
    ProposalProductsForMealAdapter proposalProductsForMealAdapter2;

    public MealCreator(String dayType, String mealNumber) {
        this.dayType = dayType;
        this.mealNumber = mealNumber;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null)
        {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_meal_creator, container, false);
        header = (ViewGroup)inflater.inflate(R.layout.fragment_product_headerview, proposalProductsList, false);

        initialComponents();

        ListUtils.setDynamicHeight(mealProductsList);
        ListUtils.setDynamicHeight(proposalProductsList);

        onClicks();

        return view;
    }


    public void initialComponents(){

        proposalProductsForMealAdapter = new ProposalProductsForMealAdapter(getActivity(),R.layout.proposal_products_for_meal_item);
        proposalProductsForMealAdapter2 = new ProposalProductsForMealAdapter(getActivity(),R.layout.proposal_products_for_meal_item);
        mealProductsList = (ListView)view.findViewById(R.id.productsForMeal);
        proposalProductsList = (ListView)view.findViewById(R.id.productsFromDatabase);
        proteinButton = (ImageView)header.findViewById(R.id.favouriteProtein);
        carbButton = (ImageView)header.findViewById(R.id.favouriteCarb);
        fatButton = (ImageView)header.findViewById(R.id.favouriteFat);
        vegetableButton = (ImageView)header.findViewById(R.id.favouriteVegetable);
        proposalProductsList.addHeaderView(header, null, false);
        proposalProductsList.setAdapter(proposalProductsForMealAdapter);
        mealProductsList.setAdapter(proposalProductsForMealAdapter2);

    }


    public void onClicks(){

        proteinButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                proposalProductsForMealAdapter = new ProposalProductsForMealAdapter(getActivity(),R.layout.proposal_products_for_meal_item);
                proposalProductsList.setAdapter(proposalProductsForMealAdapter);
                DatabaseDAOProdCarb dao = new DatabaseDAOProdCarb(getActivity());
                dao.open();
                List<DatabaseProduct> productList = dao.getAllProducts();
                dao.close();
                for(int i=0;i<productList.size();i++){
                    ProposalProductsForMealDataProvider item = new ProposalProductsForMealDataProvider("protein",productList.get(i).getName(),productList.get(i).getId(),0,false);
                    proposalProductsForMealAdapter.add(item);
                }
            }
        });


    }


    public static class ListUtils {
        public static void setDynamicHeight(ListView mListView) {
            ListAdapter mListAdapter = mListView.getAdapter();
            if (mListAdapter == null) {
                // when adapter is null
                return;
            }
            int height = 0;
            int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
            for (int i = 0; i < mListAdapter.getCount(); i++) {
                View listItem = mListAdapter.getView(i, null, mListView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                height += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = mListView.getLayoutParams();
            params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
            mListView.setLayoutParams(params);
            mListView.requestLayout();
        }
    }

}
公共类MealCreator扩展DialogFragment{
字符串日期类型,mealNumber;
ListView mealProductsList、proposalProductsList;
ImageView蛋白质按钮、carbButton、fatButton、vegetableButton;
视图;
视图组标题;
ProposalProductsFormalAdapter ProposalProductsFormalAdapter;
ProposalProductsFormalAdapter ProposalProductsFormalAdapter 2;
公共测量生成器(字符串日期类型、字符串测量编号){
this.dayType=dayType;
this.mealNumber=mealNumber;
}
@凌驾
public void onStart(){
super.onStart();
Dialog=getDialog();
如果(对话框!=null)
{
int width=ViewGroup.LayoutParams.MATCH\u父项;
int height=ViewGroup.LayoutParams.MATCH_父项;
dialog.getWindow().setLayout(宽度、高度);
}
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u mean\u creator,container,false);
标题=(视图组)充气器。充气(R.layout.fragment\u product\u headerview,proposalProductsList,false);
初始化组件();
ListUtils.setDynamicHeight(mealProductsList);
ListUtils.setDynamicHeight(proposalProductsList);
onClicks();
返回视图;
}
公共组件(){
ProposalProductsFormalAdapter=新的ProposalProductsFormalAdapter(getActivity(),R.layout.proposal\u products\u用于餐项);
ProposalProductsformeAdapter2=新的ProposalProductsformeAdapter(getActivity(),R.layout.proposal\u products\u用于餐项);
mealProductsList=(ListView)view.findViewById(R.id.ProductsFormal);
proposalProductsList=(ListView)view.findViewById(R.id.productsFromDatabase);
proteinButton=(ImageView)header.findViewById(R.id.FavoriteProtein);
carbButton=(ImageView)header.findViewById(R.id.FavoriteCrob);
fatButton=(ImageView)header.findViewById(R.id.FavoriteFat);
vegetableButton=(ImageView)header.findviewbyd(R.id.favorite);
proposalProductsList.addHeaderView(标头,null,false);
proposalProductsList.setAdapter(ProposalProductsFormalAdapter);
mealProductsList.setAdapter(ProposalProductsFormalAdapter2);
}
公共void onClicks(){
proteinButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ProposalProductsFormalAdapter=新的ProposalProductsFormalAdapter(getActivity(),R.layout.proposal\u products\u用于餐项);
proposalProductsList.setAdapter(ProposalProductsFormalAdapter);
DatabaseDAOProdCarb dao=新的DatabaseDAOProdCarb(getActivity());
dao.open();
List productList=dao.getAllProducts();
dao.close();
对于(inti=0;我也请添加“fragment\u-mean\u-creator”布局。谢谢。
<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:orientation="vertical"
    tools:context="com.plan.aplikacjamobilna.MealCreator">

    <ListView
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:paddingTop="3dp"
        android:id="@+id/productsForMeal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="3dp">
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="#f00000">
    </LinearLayout>

    <ListView
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:paddingTop="3dp"
        android:id="@+id/productsFromDatabase"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="3dp">
    </ListView>


</LinearLayout>