在我的图库中,我正在为TextView模拟单击效果设置背景。
即。当我单击TextView时,设置所需的背景。当我点击其他TextView时,TextView获得此背景,前一个获得Null背景。
我看到的是,如果我滚动浏览图库并返回,然后我选择了一些项目,我得到之前的点击背景持久性以及新的一些像下面的图像: -
有人可以帮我这个!
我的代码: -
public void MyFunc(){
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter());
gallery.setSpacing( 10 );
gallery.setAnimationDuration( 1000 );
gallery.setSelection(0,false);
final View selectedView = gallery.getSelectedView();
selectedView.setBackgroundDrawable( getResources().getDrawable( R.drawable.gallery_background ) );
cposition=0;
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
View t_lastSelectedTextView = parent.getChildAt(cposition);
if(t_lastSelectedTextView!=null){
t_lastSelectedTextView.setBackgroundDrawable( null );
t_lastSelectedTextView.setBackgroundColor( Color.TRANSPARENT );
}
v.setBackgroundDrawable( getResources().getDrawable( R.drawable.gallery_background ));
cposition=position;
});
}
public class ImageAdapter extends BaseAdapter {
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView mTextView = (TextView)getLayoutInflater().inflate(R.layout.gn_text_view, null);
mTextView.setText(options[position]);
mTextView.setLayoutParams(new Gallery.LayoutParams( Gallery.LayoutParams.WRAP_CONTENT,Gallery.LayoutParams.WRAP_CONTENT));
if(position == cposition){
mTextView.setBackgroundDrawable( getResources().getDrawable( R.drawable.gallery_background ));
}
return mTextView;
}
}