单击按钮后更改列表视图?

时间:2014-04-05 22:53:33

标签: android xml

当我点击我的应用中的修改按钮时,我希望我的ListView有一个"删除"按钮出现在每行的右侧。我该怎么做呢?

activity_recording.xml:

<FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@color/darkbluenice"
    tools:context="edu.berkeley.cs160.lasercats.RecordActivity">


    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:text="Record"
        android:textSize="12pt"
        android:id="@+id/recordButton"
        android:background="@drawable/roundedbutton"
        android:onClick="recordButtonPressed"
        android:layout_gravity="left|bottom" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:text="Edit"
        android:textSize="12pt"
        android:id="@+id/editButton"
        android:background="@drawable/roundedbutton"
        android:layout_gravity="right|bottom" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="326dp"
        android:background="@color/closeyoureyes"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal|top" />

</FrameLayout>

2 个答案:

答案 0 :(得分:1)

您必须自己编写Adapter并使用自定义ListView填充Views。 看看this tutorial

答案 1 :(得分:1)

您可以按照此tutorial了解如何在Android中创建自定义列表视图。

。要回答你的问题,你的适配器应该有一个布尔变量来检查ListView上是否启用了编辑模式,如下所示:

public class CustomAdapter extends BaseAdapter {

    Context context;
    boolean isEditModeEnabled = false;


    protected List<Car> listCars;
    LayoutInflater inflater;

    public CustomAdapter (Context context, List<Car> listCars) {
        this.listCars = listCars;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public int getCount() {
        return listCars.size();
    }

    public Car getItem(int position) {
        return listCars.get(position);
    }

    public long getItemId(int position) {
        return listCars.get(position).getDrawableId();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {

            holder = new ViewHolder();
            convertView = this.inflater.inflate(R.layout.layout_list_item,
                    parent, false);

            holder.txtModel = (TextView) convertView
                    .findViewById(R.id.txt_car_model);
            holder.txtColor = (TextView) convertView
                    .findViewById(R.id.txt_car_color);
            holder.txtPrice = (TextView) convertView
                    .findViewById(R.id.txt_car_price);
            holder.imgCar = (ImageView) convertView.findViewById(R.id.img_car);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Car car = listCars.get(position);
        holder.txtModel.setText(car.getModel());
        holder.txtColor.setText(car.getColor());
        holder.txtPrice.setText(car.getPrice() + " €");
        holder.imgCar.setImageResource(car.getDrawableId());

        //check if the EditMode is activated or not 
        if(isEditModeEnalbed) {
            yourDeleteButton.setVisibility(View.VISIBLE)
        }
        else
            yourDeleteButton.setVisibility(View.GONE);


        return convertView;
    }

    public void setEditModeEnabled(boolean isEditModeEnabled) {
        this.isEditModeEnabled = isEditModeEnabled;
    }

    private class ViewHolder {
        TextView txtModel;
        TextView txtColor;
        TextView txtPrice;
        ImageView imgCar;
    }
}

并且在EditButton的onClick()方法中,您应该将适配器的isEditModeEnabled设置为true,然后像这样刷新列表视图:

public void onClick(View v) {
    switch(v.getId()) {
    case : R.id.btnEdit : 
    yourCustomAdapterInstance.setEditModeEnabled(true);
    yourCustomAdapterInstance.notifyDataSetChanged();
    break;
    }
}