Android自定义列表视图,项目之间的空间太大

时间:2017-08-03 16:08:02

标签: android layout

我正在从mysql数据库中检索一些数据到listview中,但它看起来每次为数据库中的每个项目生成一个新的listview,因此它会使listview项目之间有太多的空间。任何帮助都会真的很难受! :D我试图通过使它成为match_parent来修复布局,但没有一个能够工作。

first

second

原始布局:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="wrap_content"
        android:background="@drawable/background"
        android:weightSum="1">

        <ImageView
            android:id="@+id/logoImageView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="17dp"
            android:layout_weight="0.07"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/logo" />

        <TextView
            android:id="@+id/titletextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/logoImageView"
            android:layout_marginLeft="33dp"
            android:layout_toEndOf="@+id/logoImageView"
            android:text="Title"
            android:textColor="@android:color/background_light"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/descriptiontextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="33dp"
            android:text="Description"
            android:textColor="@android:color/background_light"
            android:textSize="17sp"
            android:layout_below="@+id/titletextview"
            android:layout_centerHorizontal="true" />

        <TextView
            android:id="@+id/startdateTextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="33dp"
            android:text="Start Date"
            android:textColor="@android:color/background_light"
            android:textSize="14sp"
            android:layout_alignBottom="@+id/logoImageView"
            android:layout_toEndOf="@+id/logoImageView" />

        <TextView
            android:id="@+id/enddatetextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="33dp"
            android:layout_marginStart="28dp"
            android:text="End Date"
            android:textColor="@android:color/background_light"
            android:textSize="14sp"
            android:layout_alignBottom="@+id/logoImageView"
            android:layout_toEndOf="@+id/startdateTextview" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="33dp"
            android:background="@drawable/trash"
            android:layout_alignBaseline="@+id/descriptiontextview"
            android:layout_alignBottom="@+id/descriptiontextview"
            android:layout_toEndOf="@+id/enddatetextview" />

    </RelativeLayout>

CustomLayout:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="wrap_content"
    android:background="@drawable/background"
    android:weightSum="1">

    <ImageView
        android:id="@+id/logoImageView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="17dp"
        android:layout_weight="0.07"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/logo" />

    <TextView
        android:id="@+id/titletextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/logoImageView"
        android:layout_marginLeft="33dp"
        android:layout_toEndOf="@+id/logoImageView"
        android:text="Title"
        android:textColor="@android:color/background_light"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/descriptiontextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="33dp"
        android:text="Description"
        android:textColor="@android:color/background_light"
        android:textSize="17sp"
        android:layout_below="@+id/titletextview"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/startdateTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="33dp"
        android:text="Start Date"
        android:textColor="@android:color/background_light"
        android:textSize="14sp"
        android:layout_alignBottom="@+id/logoImageView"
        android:layout_toEndOf="@+id/logoImageView" />

    <TextView
        android:id="@+id/enddatetextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="33dp"
        android:layout_marginStart="28dp"
        android:text="End Date"
        android:textColor="@android:color/background_light"
        android:textSize="14sp"
        android:layout_alignBottom="@+id/logoImageView"
        android:layout_toEndOf="@+id/startdateTextview" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="33dp"
        android:background="@drawable/trash"
        android:layout_alignBaseline="@+id/descriptiontextview"
        android:layout_alignBottom="@+id/descriptiontextview"
        android:layout_toEndOf="@+id/enddatetextview" />

</RelativeLayout>

My BaseAdapter类:

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.alex.bookingapplication.CompanyData;
import com.example.alex.bookingapplication.R;

import java.util.ArrayList;

/**
 * Created by AlexPC on 8/3/2017.
 */

public class CustomAdapter extends BaseAdapter {
    Context c;
    ArrayList<CompanyData> informations = new ArrayList<>();
    LayoutInflater inflater;

    public CustomAdapter(Context c, ArrayList<CompanyData> informations) {
        this.c = c;
        this.informations = informations;
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public CustomAdapter() {
        super();
    }

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

    @Override
    public Object getItem(int i) {
        return informations.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null) {
            view = inflater.inflate(R.layout.yourservicescustomlayout, null);
        }

            // ImageView logo = (ImageView)view.findViewById(R.id.logoImageView);
            TextView title = (TextView) view.findViewById(R.id.titletextview);
            TextView description = (TextView) view.findViewById(R.id.descriptiontextview);
            TextView startDate = (TextView) view.findViewById(R.id.startdateTextview);
            TextView endDate = (TextView) view.findViewById(R.id.enddatetextview);

            CompanyData companyData = informations.get(i);

            title.setText(companyData.getTitle());
            description.setText(companyData.getDescription());
            startDate.setText(companyData.getStartDate());
            endDate.setText(companyData.getEndDate());

        return view;

    }

    @Override
    public CharSequence[] getAutofillOptions() {
        return new CharSequence[0];
    }
}

1 个答案:

答案 0 :(得分:0)

您使用的是列表适配器吗? 如果你(你可能应该,它们运行良好)那么你可以只更新适配器上的对象列表然后更新。如下

adapter.clear();
adapter.notifyDataSetChanged();

ListAdapter Documentation

Adapter Documentation