带有TransitionDrawable的List项上的setBackground

时间:2017-06-12 12:58:12

标签: android animation android-recyclerview adapter android-cardview

在我的RecyclerView中,我有一个项目列表(using custom adapter)。每行在cardview内都有一些文字,例如:

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    card_view:cardBackgroundColor="@color/card_background">

        <TextView
            android:id="@+id/text1" />

        <TextView
            android:id="@+id/text2" />


</android.support.v7.widget.CardView>

我决定使用cardview background为项目背景颜色(TransitionDrawable)制作动画。所以我必须按其位置获得具体项目。因此,我唯一可以想到的是getter&amp; setter通过我的模型类,如:

public TransitionDrawable getTrans() {
    return trans;
}
public void setTrans(TransitionDrawable trans) {
    this.trans = trans;
}
TransitionDrawable trans;

然后使用它:

holder.card.setBackground(product.getTrans());

然后:

ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
TransitionDrawable trans = new TransitionDrawable(color);
product.setTrans(trans);
trans.startTransition(5000);

但这让我StackOverflowError为我的Model类中的TransitionDrawable生成getter&setter

所以问题是如何才能实现这样的目标呢?

更新

适配器:

public class LocalAdapter extends RecyclerView.Adapter<LocalAdapter.MyViewHolder> {

private Activity activity;
private List<ProductLocal> dataList;

public interface PlayPauseClick {
    void playPauseOnClick(int position);
}
private PlayPauseClick callback;
public void setPlayPauseClickListener(PlayPauseClick listener) {
    this.callback = listener;
}


class MyViewHolder extends RecyclerView.ViewHolder{

public LocalAdapter(Activity activity, List<ProductLocal> dataList) {
    this.activity = activity;
    this.dataList = dataList ;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).
             inflate(R.layout.local_list_row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {


    ProductLocal m = dataList.get(position);


    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        holder.card.setBackground(m.getTrans());
    } else {
        holder.card.setBackgroundDrawable(m.getTrans());
    }


    holder.playPauseHive.setImageResource(m.getPlayPauseId());
    holder.playPauseHive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                callback.playPauseOnClick(holder.getAdapterPosition());
            }
        }
    });

}

public ProductLocal getItemAtPosition(int position){
        return dataList.get(position);
}


public void setDurationPlayingColor(int position){
    ProductLocal product = getItemAtPosition(position);
    ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
    TransitionDrawable trans = new TransitionDrawable(color);
    product.setTrans(trans);
    trans.startTransition(5000);
   }

 }

Fragment中的PlayPause回调:

        public void playPauseOnClick(int position) {
        ProductLocal product = adapter.getItemAtPosition(position);
        int selectedUrl = product.getRes();
        if (product.paused) {
            adapter.setDurationPlayingColor(position);
            //*****************************
            startMediaPlayer(selectedUrl);
            //*****************************
        } else {
            adapter.setDurationDefaultColor();
            stopMediaPlayer();
        }
}

非常感谢您提供任何帮助。

0 个答案:

没有答案