这可能很简单,但我找不到解决方法。问题是在onitemlongclicklistener方法中为gridview项设置背景颜色。
我有两个线性布局,一个包含textview和gridview,另一个包含一些按钮(我将此布局设置为屏幕顶部的菜单栏)。我使用setVisibility(View.GONE)从屏幕隐藏第二个,当我在gridview项目上执行itemlongclick时,第二个将使用setVisibility(View.VISIBLE)出现,同时我想更改被点击的bgcolor /所选项目。
一切正常,但每当第二个布局可见时,每当滚动网格视图时,点击/选择的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;
}
}
提前致谢,如果您需要更多信息,请告诉我们......
答案 0 :(得分:0)
在你的XML中将android:background属性设置为xml,如下所示:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_btn_sessions_pressed"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="@drawable/home_btn_sessions_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="@drawable/home_btn_sessions_selected" android:state_focused="true" />
<item android:drawable="@drawable/home_btn_sessions_default"
android:state_focused="false"
android:state_pressed="false" /> </selector>
这里不需要编写Java代码。