单击自定义适配器内的按钮后设置ListView高度(android)

时间:2017-06-29 14:48:11

标签: android

我的自定义适配器存在列表视图问题。我使用TextView和Button制作了自定义布局。我希望在单击按钮时,从列表中删除项目后,列表的高度将根据列表中的项目数进行更改。 问题是,我发现这样做的方法请求使用适配器的列表视图,所以我无法在自定义适配器中调用它。 如何在自定义适配器中设置listview的高度?

MyCustomAdapter.java:

public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;


public MyCustomAdapter(ArrayList<String> list, Context context) {
    this.list = list;
    this.context = context;
}

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

@Override
public Object getItem(int pos) {
    return list.get(pos);
}

@Override
public long getItemId(int pos) {
    return 0;
    //just return 0 if your list items do not have an Id variable.
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.custom_row, null);
    }

    TextView listItemText = (TextView)view.findViewById(R.id.item);
    listItemText.setText(list.get(position));

    Button btnExcluir = (Button)view.findViewById(R.id.btnExcluir);

    btnExcluir.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            list.remove(position); 
            notifyDataSetChanged();

    //HERE I want to change ListView's height
        }
    });

    return view;
}

设置ListView高度的函数:

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

custom_row.xml:

<?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="horizontal" >


<TextView
    android:id="@+id/item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="TextView"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_alignBaseline="@+id/btnExcluir"
    android:layout_alignBottom="@+id/btnExcluir"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/btnExcluir"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_btnexcluir"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="87dp"
    android:layout_marginEnd="87dp" />

</LinearLayout>

我真的很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

像这样更改构造函数,

  public MyCustomAdapter(ArrayList<String> list, Context context, ListView lista) {
            this.list = list;
            this.context = context;
            this.lista = lista;
        }

您可以在适配器类中调用它。