当列表项较少时,ListView无法正确显示

时间:2016-03-14 15:39:27

标签: android listview

我正在尝试创建一个应用程序,在我使用列表视图时,我看到一个奇怪的问题,其中如果我的listview中的项目数量少于它没有正确填充。 但是,只要列表视图中有更多元素,它就会正确填充。 我附上了两个应该解释问题的截图。请在这里帮助我。

List view appearing properly

See the white space when i have just 2 elements to show

我的适配器代码如下: -

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


    if(layoutInflater == null){
        layoutInflater= (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    if (convertView == null){
        convertView=layoutInflater.inflate(R.layout.describing_dish_list,null);
    }

    if (dishImageLoader == null){
        dishImageLoader=AppController.getInstance().getImageLoader();
    }

    Log.d(OrderUpdateConfig.TAG,"DishListAdapter SET DISH NAME");
    TextView dishName=(TextView) convertView.findViewById(R.id.tv_dish_name);
    TextView dishExplore=(TextView) convertView.findViewById(R.id.tv_dish_explore);
    FeedImageView dishImageView =(FeedImageView) convertView.findViewById(R.id.image_dish_pic);

    DishDetails dishDetails= dishDetailsList.get(position);
    dishName.setText(dishDetails.getDish_name());
    //Fetching Dish Image
    if(dishDetails.getDish_picture() != null) {

        dishImageView.setImageUrl(dishDetails.getDish_picture(), dishImageLoader);
        dishImageView.setVisibility(View.VISIBLE);
        dishImageView.setResponseObserver(new FeedImageView.ResponseObserver(){

            @Override
            public void onError() {
                Log.d(OrderUpdateConfig.TAG,"The IMG URL met an error");
            }

            @Override
            public void onSuccess() {
                Log.d(OrderUpdateConfig.TAG,"The IMG URL was loaded successfully ");
            }
        });
    }else{
        dishImageView.setVisibility(View.GONE);
    }


    return convertView;
}

布局文件如下: -

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

    <ListView
        android:id="@android:id/list"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@null"/>
</LinearLayout>

列表元素的布局文件: -

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/input_login_hint"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

            <TextView
                android:id="@+id/tv_dish_explore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:paddingTop="13dp"/>

            <TextView
                android:id="@+id/tv_dish_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"/>

        <com.example.saunakc.utils.FeedImageView
            android:id="@+id/image_dish_pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:scaleType="fitXY"
            android:visibility="visible" />

    </LinearLayout>
</LinearLayout>

0 个答案:

没有答案
相关问题