问题:
一开始给gridview设置了一个setOnItemSelectedListener()选中监听,根据选中id去放大对应的item,这样一来每选中一个item都得notifyDataSetChanged()一下UI,而且出现图片重新加载时有抖动的感觉。
解决思路:
直接给子控件焦点,在adapter中监听子控件的相应动作和设置获取焦点放大等动画效果(TV控件放大、adapter加载列表),这样就不需要每次item被选中都notifyDataSetChanged()。
来两个XML举个栗子:
<GridView
android:id="@+id/home_lgv_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:descendantFocusability="afterDescendants"
android:focusable="false"
android:focusableInTouchMode="false" >
</GridView>
聪明的小哥哥/小姐姐们你们懂的,看这里:android:descendantFocusability="afterDescendants"
<com.minghui.launcher.widget.ScaleLayout
android:id="@+id/item_video_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/hotle_list_selector"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="3dp" >
<ImageView
android:id="@+id/item_gv_videos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/tv_video_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item_gv_videos"
android:background="#181b20"
android:padding="5dp"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="18sp" />
</com.minghui.launcher.widget.ScaleLayout>
有点懒,懒得去掉这些边距、背景什么的了,因为这些都不重要,你知道的主要在这里:ScaleLayout,这是一个自定义控件哈,用于放大图片效果(连接)。用薛之谦的话说什么鬼啊!不来点有用的。大家理解一下哈,我也是在凑字数,因为这样整体看起来好看一点。
自定义ScaleLayout关键代码:
public void scaleOut() {
ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY",
SCALE_MIN_VALUE, SCALE_MAX_VALUE);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX",
SCALE_MIN_VALUE, SCALE_MAX_VALUE);
AnimatorSet animSet = new AnimatorSet();
animSet.play(scaleX).with(scaleY);
animSet.setDuration(200);
animSet.start();
}
public void scaleIn() {
ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY",
SCALE_MAX_VALUE, SCALE_MIN_VALUE);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX",
SCALE_MAX_VALUE, SCALE_MIN_VALUE);
AnimatorSet animSet = new AnimatorSet();
animSet.play(scaleX).with(scaleY);
animSet.setDuration(200);
animSet.start();
}
OK就是这么简单,到这里获取焦点放大效果就已经完成。接下来监听什么的就简单了,和你平时用的一毛一样,下面举个点击监听的栗子:
adapter中设置点击监听:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_hotel_type, null);
holder = new ViewHolder();
holder.bg = (RelativeLayout) convertView
.findViewById(R.id.item_video_bg);
holder.VideoName = (TextView) convertView
.findViewById(R.id.tv_video_name);
holder.ImageView = (ImageView) convertView
.findViewById(R.id.item_gv_videos);
convertView.setTag(holder);
/**
* 设置点击事件监听
*/
holder.bg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//这里是你自己的操作
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
为什么直接在adapter里设置监听呢,因为父级控件(gridview/listview)等各个view已经无法获取焦点,所以你懂得。到这里 就已经结束了,祝君好运。
效果图