上代码
package com.aisat.realtek.gridviewtest; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private GridView gv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gv = (GridView) findViewById(R.id.gv); gv.setNumColumns(5); List<String> list=new ArrayList<>(); for (int i=0;i<100;i++){ list.add("Item"+i%10); } gv.setAdapter(new MyGvAdapter(this,list)); gv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { MyGvAdapter adapter = (MyGvAdapter) gv.getAdapter(); adapter.notifyDataSetChanged(position); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }
package com.aisat.realtek.gridviewtest; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; /** * Created by Vincent on 2017/6/15. */ public class MyGvAdapter extends BaseAdapter { Context context; LayoutInflater mInflater; List<String> list; private TextView tv; public MyGvAdapter(Context context, List<String> strings) { mInflater = LayoutInflater.from(context); this.context = context; this.list = strings; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } private int selectid = -1; //用于setOnItemSelectedListener中调用,赋值当前选择的item的position public void notifyDataSetChanged(int id) { selectid = id; super.notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = mInflater.inflate(R.layout.mygv_item_layout, null); tv = (TextView) convertView.findViewById(R.id.item_tv); tv.setText(list.get(position)); if (selectid == position) { //开启动画 Animation testAnim = AnimationUtils.loadAnimation(context, R.anim.vodani); convertView.startAnimation(testAnim); } return convertView; } }
动画xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"> <scale android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.3" android:fromYScale="1.0" android:toYScale="1.3" android:pivotX="50%" android:pivotY="50%" android:duration="300" /> </set>
效果如下
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦