当我点击相应的项目时,我想更改listview适配器中名为eventInformations
的子布局的可见性。
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/list_view_padding_top"
android:paddingBottom="@dimen/list_view_padding_bottom"
android:paddingRight="@dimen/list_view_padding_right"
android:paddingLeft="@dimen/list_view_padding_left">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/eventInformations"
android:visibility="gone"
android:id="@+id/eventPopup">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
</View>
<Button
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:id="@+id/eventActionBtn"
android:text="@string/planning_action_register"
/>
</LinearLayout>
</RelativeLayout>
我的ListView
:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/eventList"
android:layout_gravity="center"
android:descendantFocusability="blocksDescendants" />
这是我的OnClick
事件处理程序:
eventListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LinearLayout eventPopup = (LinearLayout)parent.getAdapter().getView(position, view, parent).findViewById(R.id.eventPopup);
if (eventPopup.getVisibility() == View.VISIBLE)
eventPopup.setVisibility(View.GONE);
else
eventPopup.setVisibility(View.VISIBLE);
}
});
它工作得有点合适,但我遇到了两个问题:
我似乎无法找到它为什么这样工作。
感谢。
答案 0 :(得分:3)
显示子布局时,我无法单击列表视图 项目以隐藏它。
问题可能是您的可见sublayout
包含一些可聚焦的视图,该视图拦截了您ListView onItemClick()
的点击事件。
解决方案:
而不是使用ListView onItemClick()
。在onClickListener()
的相应视图中尝试使用ListView Adapter
方法点击您要隐藏/取消隐藏sublayout
当我向下滚动列表视图时,所选项目会随机更改...
这种情况正在发生,因为您的观点正在被回收/重复使用,从而导致问题。
解决方案:
我假设您有一个自定义对象列表,您将传递给ListView Adapter
表示为
List<Modal> modalsList=new ArrayList<Modal>();
为了维持sublayout
的可见性状态,请在模态类中取一个标记,例如
private boolean isVisible;
并为这些
包含相应的getter和setterpublic boolean isVisible() {
return isVisible;
}
public void setIsVisible(boolean isVisible) {
this.isVisible = isVisible;
}
并在getView()
的{{1}}方法中,使用此标记管理ListView Adapter
的可见性
sublayout
希望这会对你有所帮助。如果有任何问题,请告诉我。
答案 1 :(得分:0)
我不是真的你的意思。但我觉得你希望你的listView项目有孩子,点击你打开的项目并关闭它。如果我认为错误,但是为了做到这一点,你可以使用expandableListView。以下是适配器的示例:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private LayoutInflater inflater;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter2(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this.context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
final String childText = (String) getChild(groupPosition, childPosition);
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.expandable_list_item, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.lblListItem);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.title.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
ViewHolder holder;
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.expandable_list_group, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.lblListHeader);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.title.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class ViewHolder {
public TextView title;
}
}
无论如何,如果你想保存listView的位置,你可以在adapeter中保存数据并在getView方法中再次加载它。例如,你想保存一些可检查项的状态,你可以使用布尔数组:
private boolean[] itemTF;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.score_list_item, null, false);
TextView textView15 = (TextView) v.findViewById(R.id.textView15);
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox2);
if (itemTF[position])
checkBox.setChecked(true);
else checkBox.setChecked(false);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
itemTF[position] = isChecked;
}
});
return v;
}