Android fragmentlist中的按钮只响应一次,然后;发布“;单击行时
当我在列表视图中单击按钮时,我只看到一次祝酒词。然后,当我离开按钮单击时,进一步的单击不会显示任何内容,并且在ListView中的空白区域中,所有先前没有响应的按钮单击事件立即出现(许多祝酒词一个接一个地弹出)。你知道怎么解决这个问题吗Android fragmentlist中的按钮只响应一次,然后;发布“;单击行时,android,android-fragments,android-listview,Android,Android Fragments,Android Listview,当我在列表视图中单击按钮时,我只看到一次祝酒词。然后,当我离开按钮单击时,进一步的单击不会显示任何内容,并且在ListView中的空白区域中,所有先前没有响应的按钮单击事件立即出现(许多祝酒词一个接一个地弹出)。你知道怎么解决这个问题吗 class Adapter extends ArrayAdapter { HashMap<Integer, View> rowViews = new HashMap<Integer, View>();
class Adapter extends ArrayAdapter
{
HashMap<Integer, View> rowViews = new HashMap<Integer, View>();
public Adapter(Context context) {
super(context, R.layout.manage_member_row, members);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View rowView;
if(rowViews.containsKey(position)) {
rowView = rowViews.get(position);
} else {
rowView = inflater.inflate(R.layout.manage_member_row, parent, false);
((TextView) rowView.findViewById(R.id.manage_member_row_name)).setText(members.get(position).name);
rowViews.put(position, rowView);
}
((Button) rowView.findViewById(R.id.manage_member_row_delete)).setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "btnclick", Toast.LENGTH_SHORT).show();
}
});
Log.i("CRC", "called");
return rowView;
}
//manage_member_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:background="#FFFFFF"
android:orientation="vertical">
<TextView
android:id="@+id/manage_member_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="18sp"
android:textColor="#53585E"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp" />
<Button
android:id="@+id/manage_member_row_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:paddingLeft="8dip"
android:clickable="true"
android:focusable="true"
android:textSize="18sp"
android:textColor="#4E4E4E"
android:background="#EAEAEA"
android:paddingRight="8dip"
android:textStyle="bold"
android:text="REMOVE"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip" />
</RelativeLayout>
//fragment_manage_members.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="activities.ManageMembersActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="0dip"
android:dividerHeight="2.0dip"
android:divider="#EEEDF3"
android:layout_height="wrap_content"
android:id="@android:id/list" />
</RelativeLayout>
类适配器扩展了ArrayAdapter
{
HashMap行视图=新建HashMap();
公共适配器(上下文){
super(上下文,R.layout.manage\u member\u行,members);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终视图行视图;
if(行视图.容器(位置)){
rowView=rowViews.get(位置);
}否则{
rowView=充气机。充气(R.layout.manage\u member\u row,parent,false);
((TextView)rowView.findViewById(R.id.manage\u member\u row\u name)).setText(members.get(position.name);
行视图。放置(位置,行视图);
}
((按钮)rowView.findViewById(R.id.manage\u member\u row\u delete)).setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),“btnclick”,Toast.LENGTH\u SHORT.show();
}
});
Log.i(“CRC”,“被称为”);
返回行视图;
}
//manage_member_row.xml
//fragment_manage_members.xml
问题在于,有时getView中的位置不同步,以前就存在此问题
还请注意,使用HashMap
只是为了检查视图是否已经存在,这会占用大量内存并导致OutOfMemoryException
因此,与其将其放在hashMap中,不如为所有视图创建一个ViewHolder,并将ViewHolder的对象添加到视图的标记中
示例:
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if(view == null)
{
viewHolder = new ViewHolder();
view = mInflater.inflate(R.layout.chat_online_user_list_view_item, null);
viewHolder.userName = (TextView) view.findViewById(R.id.chat_online_user_name);
view.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) view.getTag();
//DO THE ON CLICK LISTENER HERE
return view;
}
private class ViewHolder
{
private TextView userName;
}
你说的“当我离开按钮并在列表视图的空白处单击时”是什么意思?我点击删除按钮,我得到一个祝酒词,我点击删除按钮两次,没有祝酒词,我点击删除按钮左侧(没有视图,只有背景)两个祝酒词依次出现。