The fact is that you need to select or change the background item for 5 seconds so that the user can see which item appears. I rummaged everywhere, I can not find the answer everywhere a selection at the click ... Thanks in advance ...
1 answer
I used this solution
1) Use class to store ListView data
public class ArrayGroup { private String name; private boolean isNew; //именно здесь храню признак нового элемента public ArrayGroup(String name, boolean isNew) { this.name = name; this.isNew = isNew; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isNew() { return isNew; } public void setNew(boolean aNew) { isNew = aNew; } } 2) Create an adapter:
public class AdapterGroup extends BaseAdapter { Context cont; LayoutInflater lInflater; ArrayList<ArrayGroup> objects; AdapterGroup(Context context, ArrayList<ArrayGroup> mylist){ cont= context; objects= mylist; lInflater=(LayoutInflater) cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return objects.size(); } @Override public Object getItem(int position) { return objects.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = lInflater.inflate(R.layout.item, parent, false); } ArrayGroup p = ((ArrayGroup) getItem(position)); ((TextView) view.findViewById(R.id.textView1)).setText(p.getName()); if(p.isNew()){ //АНИМАЦИЯ ЕСЛИ НОВЫЙ p.setNew(false); //Снимаем флаг нового // ******** В ЭТОМ МЕСТЕ АНИМАЦИЕЙ ОТМЕЧАЙТЕ ВАШ НОВЫЙ ЭЛЕМЕНТ AlphaAnimation alphaAnimation = new AlphaAnimation(0.4f, 0.9f); alphaAnimation.setDuration(3000); alphaAnimation.setFillAfter(true); alphaAnimation.setRepeatMode(Animation.REVERSE); ((TextView) view.findViewById(R.id.textView1)).setAnimation(alphaAnimation); //ДЕЛАЙТЕ С ЭЛЕМЕНТОМ ListView что угодно //Можете изменить backGround и в новом потоке через х секунд вернуть backGround } return view; } } В Activity все просто public class MainActivity extends AppCompatActivity { Button btnAdd; AdapterGroup adapter; ArrayList<ArrayGroup> items = new ArrayList<ArrayGroup>(); ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new AdapterGroup(this, items); listView = (ListView) findViewById(R.id.listView); listView.setAdapter(adapter); btnAdd = (Button)findViewById(R.id.buttonAdd); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { items.add(new ArrayGroup("Hello! "+new Date().toString(), true)); adapter.notifyDataSetChanged(); } }); } } - you can simply initialize
isNewin theArrayGroupisNew, why youisNewparameter when you create the object -true- pavlofff - oneThen the pavloff that ArreyGroup can be filled with hundreds of elements and displayed, and only one must be added and animated. According to your pavloff logic, it turns out that when you first start all the elements will be new and the animation will go through the entire list. The code that I provided has already been successfully tested - ivansoft
|
RecyclerViewthere are already ready default animations for inserting, deleting and moving items (you can change them for your own). adapter methods are responsible for this:notifyItemInserted(),notifyItemMoved(),notifyItemRemoved()- perhaps this solution will be more appropriate. - pavlofff