我有自己的适配器自定义ListView
。我正在处理Button
项ListView
中的点击,我希望ListView
在此次点击中不可见。
我不知道如何从适配器访问ListView
。
public class ScheduleArrayAdapter extends ArrayAdapter<ScheduleListItem> {
/*...*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
final ScheduleListItem o = items.get(position);
if (o != null) {
/*...*/
Button details=(Button)v.findViewById(R.id.btn_details);
details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//HOW TO MAKE (R.id.lv_schedule) TO BECOME INVISIBLE HERE?
}
});
}
return v;
}
}
答案 0 :(得分:65)
ViewGroup parent
保留对View
返回的getView()
的父级的引用,在您的情况下,它是您的自定义列表视图。
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
...
details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parent.setVisibility(View.INVISIBLE); // or View.GONE
}
});
...
return v;
}
答案 1 :(得分:8)
您可以通过适配器的构造函数传递Listview的引用,并将其存储在本地Listview变量中,而不是这样。您可以使用它来访问Listviews方法。就像这个
public ViewPackagesAdapter(Activity mActivity, ListView cmgListView) {
this.mActivity = mActivity;
this.mListView=cmgListView;
}
现在通过mListView访问Activity的Listview ..
答案 2 :(得分:0)
In my case GridItemClickListner didn't worked for some reason. I access the Gridview from Adapter class using this
public View getView(final int position, View convertView, ViewGroup parent) {
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ClockGridView) v.getParent()).performItemClick(v, position, v.getId());
(OR)
((ClockGridView)parent).performItemClick(v, position, v.getId());
}
});
}
"ClockGridView" which extends "GridView" Class.