Created
March 6, 2018 00:09
-
-
Save jpetitto/22f6a8ffb69bf9aa3ffcc6e281d1d37b to your computer and use it in GitHub Desktop.
RecyclerView Animation Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
List<Adapter.ViewHolder> listeners = new ArrayList<>(); | |
private boolean visible = true; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
recyclerView.setAdapter(new Adapter()); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
visible = !visible; | |
for (Adapter.ViewHolder listener : listeners) { | |
listener.animate(); | |
} | |
return true; | |
} | |
class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> { | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
LayoutInflater inflater = LayoutInflater.from(parent.getContext()); | |
View itemView = inflater.inflate(android.R.layout.simple_expandable_list_item_2, parent, false); | |
return new ViewHolder(itemView); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
holder.text1.setText("Animated Text"); | |
holder.text1.setAlpha(visible ? 1 : 0); | |
holder.text2.setText(String.valueOf(position)); | |
listeners.add(holder); | |
} | |
@Override | |
public void onViewRecycled(ViewHolder holder) { | |
super.onViewRecycled(holder); | |
listeners.remove(holder); | |
} | |
@Override | |
public int getItemCount() { | |
return 100; | |
} | |
class ViewHolder extends RecyclerView.ViewHolder { | |
TextView text1; | |
TextView text2; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
text1 = (TextView) itemView.findViewById(android.R.id.text1); | |
text2 = (TextView) itemView.findViewById(android.R.id.text2); | |
} | |
private void animate() { | |
text1.animate().alpha(visible ? 1 : 0).setDuration(2000); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment