想要呈现的效果是:点击listview中的item,将原本隐藏的button显示出来,当再次点击这个item或者点击其他的item时,之前显示的这个button又隐藏起来。
目前,我只能做到,点击item,会显示button,再点击这个item或者其他的item,之前的显示出来的button也不会隐藏
getView方法如下:
public View getView(final int position, View convertView,
ViewGroup parent) {
System.out.println("每次,绘制listview中的每一行时,都会调用getView()");
LinearLayout layout = null;
if (convertView != null) {
layout = (LinearLayout) convertView;
} else {
layout = (LinearLayout) LayoutInflater.from(context)
.inflate(R.layout.activity_main, null);
}
ImageView img = (ImageView) layout.findViewById(R.id.img);
TextView name = (TextView) layout.findViewById(R.id.name);
TextView sign = (TextView) layout.findViewById(R.id.introduction);
final Button btn = (Button) layout.findViewById(R.id.deleteBtn);
img.setBackgroundResource((Integer) mydata.get(position).get("img"));
name.setText((String) mydata.get(position).get("name"));
sign.setText((String) mydata.get(position).get("intro"));
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(View.VISIBLE);
System.out.println("Test!!");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("删除第 "+position+" 行");
mydata.remove(position);
System.out.println("删除后剩下的数据:"+mydata);
//setListAdapter(adapter);
//刪除后,刷新listview
notifyDataSetChanged();
}
});
}
});
return layout;
}
完整代码如下:
public class MyAdapter extends BaseAdapter {
private List<Map<String, Object>> mydata;
private Context context = null;
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.img1);
map.put("name", "铁路");
map.put("intro", "这是青藏铁路!");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.img2);
map.put("name", "B");
map.put("intro", "i am B");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.img3);
map.put("name", "C");
map.put("intro", "i am C");
list.add(map);
return list;
}
public MyAdapter(Context context) {
this.context = context;
//给mydata赋初始值
mydata = getData();
}
@Override
public int getCount() {
return mydata.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
System.out.println("每次,绘制listview中的每一行时,都会调用getView()");
LinearLayout layout = null;
if (convertView != null) {
layout = (LinearLayout) convertView;
} else {
layout = (LinearLayout) LayoutInflater.from(context)
.inflate(R.layout.activity_main, null);
}
ImageView img = (ImageView) layout.findViewById(R.id.img);
TextView name = (TextView) layout.findViewById(R.id.name);
TextView sign = (TextView) layout.findViewById(R.id.introduction);
final Button btn = (Button) layout.findViewById(R.id.deleteBtn);
img.setBackgroundResource((Integer) mydata.get(position).get("img"));
name.setText((String) mydata.get(position).get("name"));
sign.setText((String) mydata.get(position).get("intro"));
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(View.VISIBLE);
System.out.println("Test!!");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("删除第 "+position+" 行");
mydata.remove(position);
System.out.println("删除后剩下的数据:"+mydata);
//setListAdapter(adapter);
//刪除后,刷新listview
notifyDataSetChanged();
}
});
}
});
return layout;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="170dp"
android:orientation="vertical"
android:descendantFocusability= "blocksDescendants"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:background="#D3D3D3"
android:layout_height="120dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginRight="10dp"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginTop="25dp"
android:gravity="left"/>
<TextView
android:id="@+id/introduction"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginTop="1dp"
android:gravity="left"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/deleteBtn"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="刪除"/>
</LinearLayout>
目前的效果图:
。
------解决思路----------------------
.
你的ListView的btn都是默认Gone吗?Dialog和ListView的子View所处的线程不同哦,你用回调调用你适配器里面让按钮不显示的方法试试,最好用Handler处理。
------解决思路----------------------
。。。。。。
楼主你这样试试;
不用private List<Map<String, Object>> mydata这种方式;你自己创建一个实体类Entity;
使用这样的方式 AllaryList<Entity>list;
该Entity有一个属性是是否显示删除按钮的,比如说isDelete(true/false)
默认情况下这个值是false,就是不显示;
那当你点击这条item时先判断这个isdelete的值。然后判断执行的逻辑。执行完毕后改变这个isdelete的状态。
大概就是这个意思。。。