这可能很简单,但我找不到解决方案。问题是在 onitemlongclicklistener 方法中为 gridview 项目设置背景颜色。
我有两个线性布局,一个包含 textview 和 gridview,另一个包含一些按钮(我将此布局设置为屏幕顶部的菜单栏)。我使用 setVisibility(View.GONE) 从屏幕上隐藏第二个,当我对 gridview 项目执行 itemlongclick 时,第二个将同时使用 setVisibility(View.VISIBLE) 出现我想更改单击的 bgcolor /选定的项目。
一切正常,但只要第二个布局可见并且滚动gridview 时,单击/选择的bgcolor 就会消失。
我尝试了很多方法,但我无法解决这个问题。请查看我的编码并告诉我我做错了什么。
gv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View strings,
int position, long id) {
final Option o = dir.get(position);
//gv.performItemClick(gv, position,gv.getItemIdAtPosition(position));
/* if(pos!=-1)
{
gv.getChildAt(pos).setBackgroundColor(0xffffffff);
}*/
strings.setSelected(true);
// TextView tx=(TextView)strings.findViewById(R.id.grid_item_label);
// tx.findViewById(id).setBackgroundColor(0xff0000ff);
/*if(!o.getData().equalsIgnoreCase("o")){
//gv.getChildAt(position).setSelected(true);
fill(new File(new File(o.getPath()).getParent()),position);
}*/
/*try
{
gv.getSelectedView().setBackgroundColor(0xffcccccc);
}
catch(Exception e){
Toast.makeText(FffsdActivity.this, "ok\n"+e,Toast.LENGTH_SHORT).show();
}*/
/*for(int i=0;i<nofifo;i++)
{
try{
if(gv.getChildAt(i).isSelected())
{
gv.getChildAt(i).setBackgroundColor(0xffbbbbbb);
// Toast.makeText(FffsdActivity.this, "yes ok\n"+i,Toast.LENGTH_SHORT).show();
}
else{
gv.getChildAt(i).setBackgroundColor(0xffffffff);
//Toast.makeText(FffsdActivity.this, "no ok\n"+i,Toast.LENGTH_SHORT).show();
}
}
catch(Exception e)
{
Toast.makeText(FffsdActivity.this, "i wanna e :"+e+"\n"+i,Toast.LENGTH_SHORT).show();
}
try{
gv.getChildAt(i).setSelected(false);
//Toast.makeText(FffsdActivity.this, "gv ok\n"+i,Toast.LENGTH_SHORT).show();
}catch(Exception e){
Toast.makeText(FffsdActivity.this, "i wanna setselected e :"+e+"\n"+i,Toast.LENGTH_SHORT).show();
}
}*/
// Toast.makeText(FffsdActivity.this, "i wanna c :"+gv.getChildAt(position).isSelected(),Toast.LENGTH_LONG).show();
//gv.getSelectedView().setBackgroundColor(0xffcccccc);
pos=position;
if(o.getData().equalsIgnoreCase("parent directory"))
{
fill(new File(o.getPath()),-1);
}
else if(o.getData().equalsIgnoreCase("folder"))
{
llfftools.setVisibility(View.VISIBLE);
TranslateAnimation slide = new TranslateAnimation(0, 0,-llfftools.getHeight(),0 );
slide.setDuration(100);
slide.setFillAfter(true);
llfftools.startAnimation(slide);
//fill(new File(new File(o.getPath()).getParent()));
onFolderClick(o);
//gv.getChildAt(position).setBackgroundColor(0xffaaaaaa);
}
我的baseadapter是:
public class ImageAdapter extends BaseAdapter {
private Context context;
private final List<Option> mobileValues;
public ImageAdapter(Context context,List<Option> fofivalues) {
this.context = context;
this.mobileValues = fofivalues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
//if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
/*} else {
gridView = (View) convertView;
}*/
// set value into textview
TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
//to set the max no.of chararcters in textview
String iname=(mobileValues.get(position).getName().length()>10)?mobileValues.get(position).getName().substring(0, 9)+"..":mobileValues.get(position).getName();
if(mobileValues.get(position).getPosition()==position&&mobileValues.get(position).getPosition()!=-1)
{
textView.setText(iname);
textView.setTextColor(0xff0000ff);
}
else{
textView.setText(iname);
}
// set image based on selected text
final ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
/*gridView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
gridView.setBackgroundColor(0xffaaaaaa);
// TODO Auto-generated method stub
return false;
}
});*/
String mobile = mobileValues.get(position).getData();
if(mobile=="o")
{
//do nothing
imageView.setImageResource(R.drawable.white);
}
else if(mobile=="Parent Directory")
{
imageView.setImageResource(R.drawable.parent);
}
else if (mobile=="Folder")
{
imageView.setImageResource(R.drawable.folderg);
}
else{
imageView.setImageResource(R.drawable.picicon);
}
return gridView;
}
public int getCount() {
return mobileValues.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
在此先感谢您,如果您还需要更多信息,请告诉我...