首页 > 解决方案 > 试图将 ViewList 每个项目设置为全屏

问题描述

我使用ListViewwithArrayAdapter是为了在“即将发生的事件”上显示在屏幕上

我的目标是ListView在全屏高度显示每个项目,这样用户可以在每个项目之间滚动,而且看起来也更好。

只有当我手动将ConstraintLayout高度设置为手机屏幕的大小时,我才会得到想要的布局。

我还尝试设置ConstraintLayout抛出适配器的高度:

    // Find the CategoryItemView in order to set his height to full screen
    ConstraintLayout constraintLayout = listItemView.findViewById(R.id.container);
    constraintLayout.setMinHeight(686);

但由于某种原因,这根本不会改变高度。

我的代码:

public class HomeCategoryAdapter extends ArrayAdapter<HomeCategory>{

    public int mScreenHeightDp;
    /**
    * This is our own custom constructor (it doesn't mirror a superclass constructor).
    * The context is used to inflate the layout file, and the list is the data we want
    * to populate into the lists.
    */

    public HomeCategoryAdapter(Activity context, ArrayList<HomeCategory> homeCategories, int screenHeightDp) {
        // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
        // the second argument is used when the ArrayAdapter is populating a single TextView.
        super(context, 0, homeCategories);
        mScreenHeightDp = screenHeightDp;
    }


    /**
     * Provides a view for an AdapterView (ListView, GridView, etc.)
     *
     * @param position The position in the list of data that should be displayed in the
     *                 list item view.
     * @param convertView The recycled view to populate.
     * @param parent The parent ViewGroup that is used for inflation.
     * @return The View for the position in the AdapterView.
     */

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View listItemView = convertView;
        if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_category, parent, false);
        }

        // Find the CategoryItemView in order to set his height to full screen
        ConstraintLayout constraintLayout = listItemView.findViewById(R.id.container);
        constraintLayout.setMinHeight(686);


        // Get the {@link Word} object located at this position in the list
        final HomeCategory current = getItem(position);

        // Find the TextView in the list_category.xml layout with the ID version_name
        TextView categoryName = (TextView) listItemView.findViewById(R.id.header);
        // Get the version name from the current Word object and
        // set this text on the name TextView
        categoryName.setText(current.getmCategoryHeader());

        // Find the TextView in the list_category.xml layout with the Starts
        TextView categoryDescription = (TextView) listItemView.findViewById(R.id.description);
        // Get the version number from the current Word object and
        // set this text on the number TextView
        categoryDescription.setText(current.getmCategoryDescription());

        // Find the ImageView in the list_category.xml layout
        ImageView imageCategory = (ImageView) listItemView.findViewById(R.id.img_header);
        // Get the version image from the current Word object and
        // set this image on the ImageView
        imageCategory.setImageResource(current.getmImageResourceID());
        // Make sure the view is visible
        imageCategory.setVisibility(View.VISIBLE);

        // Return the whole list item layout (
        // so that it can be shown in the ListView
        return listItemView;
    }

}

和,

public class MasterFragment extends Fragment {

    private ArrayList<Category> mData;

    public MasterFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle bundle) {


        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.item_list, container, false);
        mData = (ArrayList<Category>)getArguments().getSerializable("Data");

        // Create an {@link WordAdapter}, whose data source is a list of {@link Category}s. The
        // adapter knows how to create list items for each item in the list.
        CategoryAdapter adapter = new CategoryAdapter(getActivity(), mData, R.color.colorAccent);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // word_list.xml layout file.
        ListView listView = (ListView) rootView.findViewById(R.id.list);

        // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
        // {@link ListView} will display list items for each {@link Word} in the list.
        listView.setAdapter(adapter);

        return rootView;
    }
}

和 XML:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="32dp"
    android:background="@drawable/customborder">


    <ImageView
        android:id="@+id/img_header"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:contentDescription="Header Image"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toTopOf="@id/guideline2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        tools:src="@drawable/attractions_beit_hatfutsot" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.55" />

    <TextView
        android:id="@+id/slogan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:text="@string/slogan"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/slogan"
        tools:text="Attractions"/>

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header"
        tools:layout_editor_absoluteX="26dp"
        tools:text="@string/random_text" />

</android.support.constraint.ConstraintLayout>

标签: javaandroidandroid-arrayadapter

解决方案


试试看 :

android:layout_height:"match_parent"

但我认为你应该尝试使用 RecyclerView。


推荐阅读