listview中item的上移、下移和删除
来源:互联网 发布:mac虚拟机怎么退出全屏 编辑:程序博客网 时间:2024/06/05 01:04
最近遇到一个需求,在开店时需要店主上传图文详情,并且店主可以随时调整每个图文的顺序,现将代码贴出来
public class MainActivity extends AppCompatActivity { private ListView listview; private ImageView add; private PicAndFondAdapter adapter; private List<PicAndfontBean> list = new ArrayList<PicAndfontBean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview); View foot_view = View.inflate(MainActivity.this,R.layout.foot_view,null); add = (ImageView) foot_view.findViewById(R.id.add); listview.addFooterView(foot_view); initview(); } private void initview() { list.clear(); adapter = new PicAndFondAdapter(MainActivity.this,list); listview.setAdapter(adapter); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PicAndfontBean bean = new PicAndfontBean(); bean.setPic("http://img.pconline.com.cn/images/upload/upc/tx/itbbs/1202/09/c3/10378469_1328796216421_1024x1024it.jpg"); bean.setWenzi(""); list.add(bean); adapter.notifyDataSetChanged(); } }); }}
适配器是重点
/** * description:图文详情适配器 * @author: libaojian * Time: 2016/10/25 10:44 */public class PicAndFondAdapter extends BaseAdapter{ private List<PicAndfontBean> list = new ArrayList<PicAndfontBean>(); private Activity context; private DisplayImageOptions displayImageOptions; public PicAndFondAdapter(Activity context, List<PicAndfontBean> list){ this.context = context; this.list = list; // 初始化ImageLoader ImageLoaderUtils.initBigImageLoader(context); displayImageOptions = ImageLoaderUtils.getDisplayOptions(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = View.inflate(context, R.layout.item_pic_wenzi, null); holder.img = (ImageView) convertView.findViewById(R.id.img); holder.edit = (EditText) convertView.findViewById(R.id.edit); holder.icon_up = (ImageView) convertView.findViewById(R.id.icon_up); holder.icon_down = (ImageView) convertView.findViewById(R.id.icon_down); holder.icon_delete = (ImageView) convertView.findViewById(R.id.icon_delete); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (position == 0){ holder.icon_up.setVisibility(View.GONE); }else{ holder.icon_up.setVisibility(View.VISIBLE); } if (position == list.size()-1&&position != 0){ holder.icon_down.setVisibility(View.GONE); }else{ holder.icon_down.setVisibility(View.VISIBLE); } final PicAndfontBean bean = list.get(position); holder.edit.setText(bean.getWenzi()); ImageLoader.getInstance().displayImage( bean.getPic(), holder.img, displayImageOptions); //下移 holder.icon_down.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bean.setWenzi(holder.edit.getText().toString()); bean.setPic("http://img.pconline.com.cn/images/upload/upc/tx/itbbs/1202/09/c3/10378469_1328796216421_1024x1024it.jpg"); indexExChange(list,position,position+1); notifyDataSetChanged(); } }); //上移 holder.icon_up.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bean.setWenzi(holder.edit.getText().toString()); bean.setPic("http://img.pconline.com.cn/images/upload/upc/tx/itbbs/1202/09/c3/10378469_1328796216421_1024x1024it.jpg"); indexExChange(list, position, position - 1); notifyDataSetChanged(); } }); holder.icon_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); notifyDataSetChanged(); } }); holder.edit.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){//获得焦点 // ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); }else{//失去焦点 bean.setWenzi(holder.edit.getText().toString()); bean.setPic("http://img.pconline.com.cn/images/upload/upc/tx/itbbs/1202/09/c3/10378469_1328796216421_1024x1024it.jpg"); } } }); return convertView; } class ViewHolder{ public ImageView img,icon_up,icon_down,icon_delete; private EditText edit; } public static <T> List<T> indexExChange(List<T> list,int index1,int index2){ T t = list.get(index1); list.set(index1, list.get(index2)); list.set(index2, t); return list; }}
/** * description:图文详情实体类 * @author: libaojian * Time: 2016/10/25 10:40 */public class PicAndfontBean implements Serializable{ private String pic; private String wenzi; public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public String getWenzi() { return wenzi; } public void setWenzi(String wenzi) { this.wenzi = wenzi; }}下面是布局文件
<?xml version="1.0" encoding="utf-8"?><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" android:background="#f3f3f3" tools:context="com.tianyijing.uploadimagedemo.MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"/></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f3f3f3"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:layout_marginTop="8dp" android:layout_below="@+id/listview" > <ImageView android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/add_icon_add"/> </RelativeLayout></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="58dp" > <ImageView android:id="@+id/img" android:layout_width="58dp" android:layout_height="58dp" android:scaleType="centerCrop" android:background="#999999"/> <EditText android:id="@+id/edit" android:layout_toRightOf="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:gravity="top|left" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" > <ImageView android:id="@+id/icon_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_up" android:layout_marginRight="16dp"/> <ImageView android:id="@+id/icon_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_down" android:layout_marginRight="16dp"/> <ImageView android:id="@+id/icon_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_delete" android:layout_marginRight="16dp"/> </LinearLayout> </RelativeLayout></LinearLayout>
具体demo可以联系我qq:763352330
0 0
- listview中item的上移、下移和删除
- Android中RecyclerView的item中控件的点击事件添加删除一行、上移下移一行的代码实现
- Android中RecyclerView的item中控件的点击事件添加删除一行、上移下移一行的代码实现
- 类google的上移和下移
- datagrid实现行的上移和下移
- scrollTop上移和下移
- ListView 上移下移、上调下调
- 基于jquery框架实现以行的添加、上移、下移和删除操作
- jquery实现table动态添加行、删除行以及行的上移和下移
- jquery实现table动态添加行、删除行以及行的上移和下移
- 自己修改, jquery实现table动态添加行、删除行以及行的上移和下移
- flexgrid的上移下移
- Jquery Datetable 行上移、下移 点击 和 删除事件
- XZ_iOS中textField的键盘弹出和回收导致的界面上移和下移
- ASP.Net中的TreeView控件中对节点的上移和下移操作
- C#中DataGrid加载XML数据---上移和下移
- EXT 中tree中节点的上移与下移
- php 实现上移和下移
- Convert a Number to Hexadecimal
- 前端开发日记之点击悬浮框之外的地方隐藏悬浮框
- 系统设计时关于性能问题处理的几点心得
- opencv图像读取
- 每一个梦想都要让它长上翅膀
- listview中item的上移、下移和删除
- PHP 和MySQL Web开发
- gdb调试命令
- 关于权限对象
- 如何同时上内网和外网
- 利用反射动态调用多个方法
- 安卓vitamio从导入项目到listview播放视频
- 补间动画,帧动画
- 2016阿里巴巴73款开源产品全向图