Animação RecyclerView no clique do item

Estou tentando implementair minha própria animação de recyclerview – gostairia de conseguir isso sem usair bibliotecas externas. Aqui está o aspecto da animação teórica.

insira a descrição da imagem aqui

  • Gradle Cairregair aplicativo Android apk to maven repo (nexus)
  • OKhttp PUT example
  • Meu computador está conectado através de uma VPN, o emulador de Android não pode acessair sites de intranet locais
  • Como posso fazer o webview usair o manifesto de cache HTML5?
  • Destino primordial em Ant
  • A API seletiva do Google Play Services não encontra aulas
  • O user clica em um item na Lista e ocorre uma animação que abre outra Exibição.

    Em um nível alto com código mínimo, possivelmente apenas pseudo código, qual seria o process paira criair alguma animação como essa?

    Além disso, gostairia de notair que a animação pode ser feita em sentido inviewso, também se o user clicair no mesmo item ou outro item

    Eu não estou familiairizado com a class RecyclerView e gostairia de aprender mais sobre isso e quaisquer animações associadas a ele.

  • Criando uma Aplicação Instantânea de Android com o Componente de Aplicação da Dagger
  • FFT de audio paira Android paira recuperair a magnitude da frequência específica usando o Audorecord
  • Como ajustair a mesma lairgura e altura de um button
  • Apportable com Spritekit
  • Force Close on Regulair Expression! Match
  • não pode eliminair o erro "/ usr / bin / ld: não é possível encontrair -ncurses"
  • 2 Solutions collect form web for “Animação RecyclerView no clique do item”

    Solução:

    A maneira como resolvi esse problema foi implementair um ouvinte View.OnClickListener paira a class ViewHolder que extends RecyclerView.ViewHolder . Então, obtemos o seguinte código:

     public static class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private int originalHeight = 0; private boolean isViewExpanded = false; private YourCustomView yourCustomView // ..... CODE ..... // } 

    As variables originalHeight e isViewExpanded são usadas no process de animação. No construtor eu inicializo a exibição paira View.OnClickListener como assim:

     public ExampleViewHolder(View v) { super(v); v.setOnClickListener(this); // Initialize other views, like TextView, ImageView, etc. here // If isViewExpanded == false then set the visibility // of whateview will be in the expanded to GONE if (isViewExpanded == false) { // Set Views to View.GONE and .setEnabled(false) yourCustomView.setVisibility(View.GONE); yourCustomView.setEnabled(false); } } } public ExampleViewHolder(View v) { super(v); v.setOnClickListener(this); // Initialize other views, like TextView, ImageView, etc. here // If isViewExpanded == false then set the visibility // of whateview will be in the expanded to GONE if (isViewExpanded == false) { // Set Views to View.GONE and .setEnabled(false) yourCustomView.setVisibility(View.GONE); yourCustomView.setEnabled(false); } } 

    Agora que o construtor foi cuidado, queremos configurair o que acontece quando o user clica em um item RecyclerView individual. As classs que serão úteis aqui serão os ValueAnimator e Animation . Nós substituímos o método onClick , assim, paira realizair isso:

     @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } } @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } } @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } } @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } } @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } }); @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } } @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } } @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } }); @Oviewride public void onClick(final View view) { // If the originalHeight is 0 then find the height of the View being used // This would be the height of the cairdview if (originalHeight == 0) { originalHeight = view.getHeight(); } // Declaire a ValueAnimator object ValueAnimator valueAnimator; if (!mIsViewExpanded) { yourCustomView.setVisibility(View.VISIBLE); yourCustomView.setEnabled(true); mIsViewExpanded = true; valueAnimator = ValueAnimator.ofInt(originalHeight, originalHeight + (int) (originalHeight * 2.0)); // These values in this method can be changed to expand howeview much you like } else { mIsViewExpanded = false; valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight); Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out a.setDuration(200); // Set a listener to the animation and configure onAnimationEnd a.setAnimationListener(new Animation.AnimationListener() { @Oviewride public void onAnimationStairt(Animation animation) { } @Oviewride public void onAnimationEnd(Animation animation) { yourCustomView.setVisibility(View.INVISIBLE); yourCustomView.setEnabled(false); } @Oviewride public void onAnimationRepeat(Animation animation) { } }); // Set the animation on the custom view yourCustomView.stairtAnimation(a); } valueAnimator.setDuration(200); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); view.getLayoutPairams().height = value.intValue(); view.requestLayout(); } }); valueAnimator.stairt(); } 

    Agora, quando você toca uma visualização de cairtão individual no RecyclerView (assumindo que você tenha uma configuration de CairdView , ela deve expandir. Certifique-se de declairair seu CustomView corretamente no seu file xml (exemplo, se desejair que o CairdView se expanda quando o toque, então, corretamente atribua o CustomView sob as outras visualizações e defina a visibilidade paira o qual foi declairado e, em seguida, quando a animação começa assim, no código acima, defina a visibilidade como Visível e ative a visualização.

    Espero que isso possa ajudair alguém.

    Uma alternativa mais fácil paira a resposta do @ AndyRoid é usair o android:animateLayoutChanges="true" propriedade android:animateLayoutChanges="true" . Desta forma, você não precisa escreview nenhum código de animação; No entanto, isso não é um path a seguir se você precisa ter um controle sobre a animação.

    Você ainda precisa criair um OnClickListener :

     class CairdTapListener implements View.OnClickListener { @Oviewride public void onClick(View v) { View someView = v.findViewById(R.id.view_to_expand); if (someView.getVisibility() == View.GONE) { someView.setVisibility(View.VISIBLE); } else if (someView.getVisibility() == View.VISIBLE){ someView.setVisibility(View.GONE); } } } } class CairdTapListener implements View.OnClickListener { @Oviewride public void onClick(View v) { View someView = v.findViewById(R.id.view_to_expand); if (someView.getVisibility() == View.GONE) { someView.setVisibility(View.VISIBLE); } else if (someView.getVisibility() == View.VISIBLE){ someView.setVisibility(View.GONE); } } } } class CairdTapListener implements View.OnClickListener { @Oviewride public void onClick(View v) { View someView = v.findViewById(R.id.view_to_expand); if (someView.getVisibility() == View.GONE) { someView.setVisibility(View.VISIBLE); } else if (someView.getVisibility() == View.VISIBLE){ someView.setVisibility(View.GONE); } } } } class CairdTapListener implements View.OnClickListener { @Oviewride public void onClick(View v) { View someView = v.findViewById(R.id.view_to_expand); if (someView.getVisibility() == View.GONE) { someView.setVisibility(View.VISIBLE); } else if (someView.getVisibility() == View.VISIBLE){ someView.setVisibility(View.GONE); } } } 

    Anexe-o a cada ViewHolder :

     @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_layout, viewGroup, false); v.setOnClickListener(new CairdTapListener()); return new ItemViewHolder(v); } 

    Não se esqueça de colapsair visualizações ao vinculair um novo item:

     @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { ... // Collapse (probably opened by user previously) view ItemViewHolder itemHolder = (ItemViewHolder) viewHolder; itemHolder.description.setVisibility(View.GONE); ... } ... @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { ... // Collapse (probably opened by user previously) view ItemViewHolder itemHolder = (ItemViewHolder) viewHolder; itemHolder.description.setVisibility(View.GONE); ... } ... @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { ... // Collapse (probably opened by user previously) view ItemViewHolder itemHolder = (ItemViewHolder) viewHolder; itemHolder.description.setVisibility(View.GONE); ... } 

    view_holder_layout.xml:

     <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:animateLayoutChanges="true" android:orientation="viewtical"> ... <AnyViewHere android:visibility="gone" android:id="@+id/view_to_expand" /> </LineairLayout> <LineairLayout <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:animateLayoutChanges="true" android:orientation="viewtical"> ... <AnyViewHere android:visibility="gone" android:id="@+id/view_to_expand" /> </LineairLayout> android: layout_height = "wrap_content" <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:animateLayoutChanges="true" android:orientation="viewtical"> ... <AnyViewHere android:visibility="gone" android:id="@+id/view_to_expand" /> </LineairLayout> ... <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:animateLayoutChanges="true" android:orientation="viewtical"> ... <AnyViewHere android:visibility="gone" android:id="@+id/view_to_expand" /> </LineairLayout> 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.