我有一个ListView
和Button
。我想隐藏从第二个项目点击Button
时可见的项目。
如果单击“1”按钮,我想隐藏蓝色“布局”。如果点击是相同的项目,我可以管理布局,但是当我从另一个项目单击按钮时,布局仍然可见。如何设置这些项目的可见性?
这可能是代码。 我的LISTADAPTER
public View getView(int position, View convertView, ViewGroup parent) {
final String item = items.get(position);
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.item, parent, false);
TextView itemTV = (TextView) v.findViewById(R.id.item);
itemTV.setText(item);
final ImageView number1 = (ImageView) v.findViewById(R.id.number1);
final ImageView number2 = (ImageView) v.findViewById(R.id.number2);
final ImageView button1 = (ImageView) v.findViewById(R.id.button1);
final ImageView button2 = (ImageView) v.findViewById(R.id.button2);
final RelativeLayout blue = (RelativeLayout) v.findViewById(R.id.blue);
final RelativeLayout green = (RelativeLayout) v
.findViewById(R.id.green);
blue.setVisibility(RelativeLayout.GONE);
green.setVisibility(RelativeLayout.GONE);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (on == 0) //show hide ---int on=0;
{
blue.setVisibility(RelativeLayout.VISIBLE);
green.setVisibility(RelativeLayout.GONE);
on = 1;
Toast.makeText(context, "ImageButton clicked" + item,
Toast.LENGTH_SHORT).show();
number1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context,
"CLICK" + item,
Toast.LENGTH_SHORT).show();
}
});
number2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context,
"ClICK" + item,
Toast.LENGTH_SHORT).show();
}
});
}// on equals 0
else {
on = 0;
blue.setVisibility(RelativeLayout.GONE);
green.setVisibility(RelativeLayout.GONE);
}
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
blue.setVisibility(RelativeLayout.GONE);
green.setVisibility(RelativeLayout.VISIBLE);
Toast.makeText(context, "CLICK" + item,
Toast.LENGTH_SHORT).show();
number2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "CLICK" + item,
Toast.LENGTH_SHORT).show();
}
});
}
});
return v;
}
我的活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView lv = (ListView) findViewById( R.id.list );
lv.setOnItemClickListener(
new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
android.view.View arg1, int arg2, long arg3) {
Toast.makeText( ListImageButton.this,
"List item clicked",
Toast.LENGTH_SHORT).show();
}
});
ArrayList<String> items = new ArrayList<String>();
items.add( "item1");
items.add( "item2");
items.add( "item3");
ListAdapter adapter = new ListAdapter( this, items);
lv.setAdapter( adapter );
}
答案 0 :(得分:1)
我假设您要在单个行上显示蓝色或绿色部分,具体取决于从行中单击的数字按钮。尝试这样的事情:
// two fields in the adapter class
private int mBlueFlag = -1;
private int mGreenFlag = -1;
public View getView(int position, View convertView, ViewGroup parent) {
final String item = items.get(position);
View v = null;
if (convertView != null) {
v = convertView;
} else {
v = inflater.inflate(R.layout.item, parent, false);
}
TextView itemTV = (TextView) v.findViewById(R.id.item);
itemTV.setText(item);
final ImageView number1 = (ImageView) v.findViewById(R.id.number1);
final ImageView number2 = (ImageView) v.findViewById(R.id.number2);
// I assume this are the buttons with 1 and 2 which show the green/blue row part
final ImageView button1 = (ImageView) v.findViewById(R.id.button1);
final ImageView button2 = (ImageView) v.findViewById(R.id.button2);
final RelativeLayout blue = (RelativeLayout) v.findViewById(R.id.blue);
final RelativeLayout green = (RelativeLayout) v.findViewById(R.id.green);
if (mBlueFlag != -1 && mBlueFlag == position) {
blue.setVisibility(View.VISIBLE);
} else {
blue.setVisibility(View.GONE);
}
if (mGreenFlag != -1 && mGreenFlag == position) {
green.setVisibility(View.VISIBLE);
} else {
green.setVisibility(View.GONE);
}
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mGreenFlag = position;
mBlueFlag = -1;
// set the other OnCLickListener
notifyDataSetChanged();
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mBlueFlag = position;
// set the other OnClickListeners
notifyDataSetChanged();
}
});
return v;
}
答案 1 :(得分:0)
您需要更改基于此列表的信息,并调用notifityDataSetChanged(),它将重新创建列表。