android gridview+viewpager,怎样根据setOnItemClickListener position改变item背景。
viewpager是循环加入的。
activity里
for (i = 0; i < videoNum / 4; i++) {//我这里的videoNum是20
view = getActivity().getLayoutInflater().inflate(
R.layout.fragment_four_video, null);
if (viewList.size() < (videoNum / 4)) {
viewList.add(view);
gridView = (GridView) view.findViewById(R.id.gv);
mAdapter = new BaseMoreAdapter(getActivity(), 2, 2,
displaychannels, i);
gridView.setAdapter(mAdapter);
gridView.setSelection(0);
gridView.setOnItemClickListener(onItemClick);
}
}
mVpAdapter = new ViewPagerAdapter(viewList);
mVPager.setAdapter(mVpAdapter);
在点击事件里调用了适配器里的方法更换背景
private OnItemClickListener onItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
mAdapter.setSeclection(position);
mAdapter.notifyDataSetChanged();}
adapter下
public void setSeclection(int position) {
clickTemp = position;
}
adapter getview方法里
if (clickTemp == position) {
viewHolder.mRlContent.setBackgroundResource(R.drawable.a);
} else {
viewHolder.mRlContent.setBackgroundResource(R.drawable.b);
}
这样写的结果是,因为循环的关系每次都是操作了最后一页,怎样可以更换viewpager当前页的gridview的item的背景。