简体   繁体   English

在RecyclerView.ViewHolder中获取上下文

[英]Get context within RecyclerView.ViewHolder

I want to animate ListView items when they first appear. 我想为ListView项首次出现时设置动画。 I have the following viewholder: 我有以下观点者:

public class SimpleViewHolder extends RecyclerView.ViewHolder
{
    private TextView  simpleTextView;

    public SimpleViewHolder(final View itemView, final SimpleAdapter.onItemClickListener listener) 
    {
        super(itemView);

        simpleTextView  = (TextView)  itemView.findViewById(R.id.simple_text);

        RotateAnimation rotate = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rotate.setDuration(1000);
        rotate.setRepeatCount( 0 );
        simpleTextView.setAnimation(rotate);
    }

    public void bindData(final SimpleViewModel viewModel)
    {
        simpleTextView.setText( viewModel.getSimpleText() );
    }
}

Everything works great, except instead of setting the animations programmatically, I would like to load them from an XML file using the following method: 一切工作都很好,除了不是通过编程方式设置动画,我想使用以下方法从XML文件加载它们:

Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);

But I'm not clear how to get/pass the context to the RecyclerView.ViewHolder or is this even the proper place where to do animations. 但是我不清楚如何将上下文获取/传递给RecyclerView.ViewHolder,或者这是否是执行动画的适当位置。

How can I load the XML animation within the RecyclerView.ViewHolder and is this correct place for doing animations for the list items? 如何在RecyclerView.ViewHolder中加载XML动画,这是为列表项制作动画的正确位置吗? Thanks! 谢谢!

您可以使用itemView.getContext()获取上下文

I don't disagree with the placing of animation. 我不同意动画的摆放位置。 I think it's the correct place. 我认为这是正确的地方。 About context, I would send it in the constructor. 关于上下文,我将其发送到构造函数中。

public SimpleViewHolder(final View itemView, final SimpleAdapter.onItemClickListener listener, Context context) {
 //use this context...

}

if your Recyclerview has no context then you can pass context to Recycleview as well. 如果您的Recyclerview没有上下文,那么您也可以将上下文传递给Recycleview。 I dont think there is another way around 我不认为还有其他办法

the proper way to obtain the Context might be eg. 获取Context的正确方法可能是例如。 within the implementation of onLongClick() : onLongClick()的实现中:

@Override
public boolean onLongClick(View viewHolder) {

    this.mRecyclerView = (SomeLinearView) viewHolder.getParent();

    Context context;
    if(viewHolder.isInEditMode()) {
        context = ((ContextThemeWrapper) this.mRecyclerView.getContext()).getBaseContext();
    } else  {
        context = this.mRecyclerView.getContext();
    }
}

and this does not crash in edit-mode (which is the XML preview). 并且这不会在编辑模式(XML预览)下崩溃。 declaring all the variables as final is merely useless and often hindering, unless being required to do so, because of changing the scope. 将所有变量声明为final只是无用的,并且通常会受到阻碍,除非需要这样做,因为更改了范围。

and one can apply a layout-animation alike: 并且可以同样地应用布局动画:

int resId = R.anim.some_animation;
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(context, resId);
this.mRecyclerView.setLayoutAnimation(animation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM