dialogfragment中的Android列表视图

时间:2016-11-08 22:03:59

标签: java android android-fragments

我需要在DialogFragment中放置两个listview并使其可滚动。第一个listview在红线上方,第二个在下面。对于普通的片段,我通常使用代码中的setDynamicHeight方法,它完美无缺。我覆盖了OnStart()方法,使我的DialogFragment全屏显示。出了点问题并产生影响:

enter image description here

第二个listView(我的代码中为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();
        }
    }

}

xml文件:

<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>

有什么方法可以解决我的问题吗?

0 个答案:

没有答案