用于listview的自定义适配器内的动画按钮

时间:2017-12-08 13:27:08

标签: android listview animation adapter

我使用自己的布局制作了一个带有自定义适配器的ListView,在这个布局上有一个文本视图,一个按钮和三个图像按钮,最初是setVisibility.GONE 我试图动画这个按钮,当用户点击它时,它会从右向左改变它的位置,为三个图像按钮提供空间。

问题是,动画工作的唯一项目是列表中的最后一项。我希望动画可以在所有项目中使用。

以下是我adapter的代码:

public class ListaAdapter extends BaseAdapter implements ListAdapter {

private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;
private Button btnAbrir;
private RelativeLayout relativeLayout;
private ObjectAnimator animation;


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


}

@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_list, null);
}

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

final LinearLayout btnLayout = (LinearLayout) 
 view.findViewById(R.id.btnLayout);

relativeLayout = (RelativeLayout) view.findViewById(R.id.layout);

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

ImageButton btnNaoSei = (ImageButton)view.findViewById(R.id.btnNaoSei);

ImageButton btnAceitar = 
(ImageButton)view.findViewById(R.id.btnAceitar);

ImageButton btnNegar = (ImageButton)view.findViewById(R.id.btnNegar);

animation = ObjectAnimator.ofFloat(btnAbrir,"x",200);

btnLayout.setVisibility(View.GONE);

btnAbrir.setOnClickListener(new View.OnClickListener() {

@Override
        public void onClick(View view) {

            btnLayout.setVisibility(View.VISIBLE);

           animate();
        }
    });



    return view;
}

private void animate()
{
    animation.setDuration(500);
    animation.start();
}
}

这是我的custom_list.xml布局:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">

<RelativeLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@color/white">

    <LinearLayout
        android:id="@+id/btnLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="@dimen/_32sdp"
            android:layout_height="@dimen/_50sdp"
            android:background="@color/compras_barra">

            <ImageButton
                android:id="@+id/btnNaoSei"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="false"
                android:layout_alignParentStart="false"
                android:layout_alignParentTop="false"
                android:backgroundTint="@color/compras_barra"
                android:contextClickable="false"
                app:srcCompat="@drawable/a12" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnNaoSei"
                android:layout_centerHorizontal="true"
                android:text="NÃO SEI"
                android:textColor="@color/white"
                android:textSize="8sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="@dimen/_32sdp"
            android:layout_height="@dimen/_50sdp"
            android:background="@color/compras">

            <ImageButton
                android:id="@+id/btnAceitar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="11dp"
                android:background="@color/compras"
                app:srcCompat="@drawable/a11" />

            <TextView
                android:id="@+id/textView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/btnAceitar"
                android:paddingEnd="@dimen/_3sdp"
                android:paddingTop="@dimen/_8sdp"
                android:text="ACEITAR"
                android:textColor="@color/white"
                android:textSize="8sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="@dimen/_32sdp"
            android:layout_height="@dimen/_50sdp"
            android:background="@color/compras_texto2">

            <ImageButton
                android:id="@+id/btnNegar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:background="@color/compras_texto2"
                app:srcCompat="@drawable/a10" />

            <TextView
                android:id="@+id/textView13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="12dp"
                android:paddingBottom="@dimen/_1sdp"
                android:text="NEGAR"
                android:textColor="@color/white"
                android:textSize="8sp" />

        </RelativeLayout>

    </LinearLayout>

    <Button
        android:id="@+id/btnAbrir"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/a09"
        android:layout_alignTop="@+id/textoItem"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:id="@+id/textoItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:maxWidth="200dp"
        android:text="TextView"
        android:textColor="@color/compras_texto2" />
</RelativeLayout>

有关如何实现这一目标的任何想法?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要采用以下方法并创建ViewHolder,以便您的适配器知道List

中每行的每个元素

该行的最后一个UI元素仅动画,因为这是AdapaterView中创建List时唯一的最后已知组件ID。因此它将为最后一行元素设置动画

您必须在ViewHolder中添加class嵌套Adapter并声明您的UI组件。在setTag

中使用getTaggetView方法

总体而言,您必须在adapter

中创建getView这样的内容
public class SListAdapter extends ArrayAdapter<String> {

    private Context context;
    private String[] seCtnColors;
    private List<Subscription> item;
    private ViewHolder mainViewHolder = null;
    private ViewHolder viewHolder;
    private LayoutInflater inflater;
    private View row;

    public SListAdapter(Context c, List<Subscription> subscriptions)
    {
        super(c, R.layout.row,R.id.rowNameTV);
        this.context=c;
        this.item = subscriptions;
    }


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

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        row = null;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.row,parent,false);
        viewHolder = new ViewHolder();
        initAdapterUI();
        setAdapterDataset(position);

        return row;
    }

    private void initAdapterUI() {

        viewHolder.animatedGifViewHol = row.findViewById(R.id.row_animated_gif);
        viewHolder.alertBarVerticalViewHol = row.findViewById(R.id.alertBarVerticalView);
        viewHolder.firstNameTVHol = (TextView) row.findViewById(R.id.rowNameTV);
        viewHolder.phoneNumberTVHol = (TextView) row.findViewById(R.id.rowNumberTV);
        viewHolder.switchStateTVHol = (TextView) row.findViewById(R.id.switchStateTV);
        row.setTag(viewHolder);
    }

    private void setAdapterDataset(int position) {
        mainViewHolder = (ViewHolder) row.getTag();
        mainViewHolder.alertBarVerticalViewHol.setBackgroundColor(Color.RED);
        mainViewHolder.switchStateTVHol.setTextColor(Color.RED);
        mainViewHolder.firstNameTVHol.setText(item.get(position).getFirstName());
        mainViewHolder.phoneNumberTVHol.setText(item.get(position).getNumber());
    }

    public class ViewHolder{
        View animatedGifViewHol;
        View alertBarVerticalViewHol;
        TextView firstNameTVHol;
        TextView switchStateTVHol;
        TextView phoneNumberTVHol;
    }
}