基于ListView的滑动删除、添加、修改
2016-05-17 12:25
225 查看
在很多App应用当中我们都看见过各式各样的listview,当然,我们自己对于这个控件肯定也是相当熟悉,那么为什么要在listview中嵌套进一个侧滑删除等这些细节性的功能呢?
其实原因很简单,就是用户体验!试想一下当一个界面中展示给用户得信息过多,而这些信息又并不是特别重要的,那么用户为了方便肯定有这个需求了,而这个方便在我们看来就是需求。接下来看看这个“方便”是怎么达到效果的
activity_main.xml布局文件
listview_adapter.xml文件
MainActivity
AdapterTest 适配器
OK! listview侧滑功能就实现了(原理:HorizontalScrollView嵌套ListView).
其实原因很简单,就是用户体验!试想一下当一个界面中展示给用户得信息过多,而这些信息又并不是特别重要的,那么用户为了方便肯定有这个需求了,而这个方便在我们看来就是需求。接下来看看这个“方便”是怎么达到效果的
activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:clickable="true" android:orientation="vertical" tools:context="com.example.administrator.testpopuwindow.MainActivity" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listView" /> </LinearLayout>
listview_adapter.xml文件
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/hsv" android:layout_width="wrap_content" android:layout_height="80dip" android:scrollbars="none" > <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <LinearLayout android:id="@+id/ll_content" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="" /> </LinearLayout> <LinearLayout android:id="@+id/ll_action" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/darker_gray" android:paddingLeft="20dip" android:paddingRight="20dip" android:text="添加" /> <Button android:id="@+id/button2" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@android:color/darker_gray" android:paddingLeft="20dip" android:paddingRight="20dip" android:text="删除" /> <Button android:id="@+id/button3" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray" android:paddingLeft="20dip" android:paddingRight="20dip" android:text="变色" /> </LinearLayout> </LinearLayout> </HorizontalScrollView>
MainActivity
package com.example.administrator.testpopuwindow; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.view.View; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Instantiation(); } public void Instantiation(){ listView = (ListView) findViewById(R.id.listView); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); AdapterTest adapterTest = new AdapterTest(this,dm.widthPixels); listView.setAdapter(adapterTest); } }
AdapterTest 适配器
package com.example.administrator.testpopuwindow; import android.content.Context; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.HorizontalScrollView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016-05-12. */ public class AdapterTest extends BaseAdapter implements View.OnClickListener { // 数据源,用于存放颜色值的。 private List<Integer> colors; private Context mContext; // 屏幕宽度,由于我们用的是HorizontalScrollView,所以按钮选项应该在屏幕外 private int mScreentWidth; private View view; /** * 构造方法 * * @param context * @param screenWidth */ public AdapterTest(Context context, int screenWidth) { // 初始化 mContext = context; mScreentWidth = screenWidth; // 填充list的内容模拟数据,否则应该异步执行 colors = new ArrayList<Integer>(); for (int i = 0; i < 15; i++) { colors.add(R.color.zise); } } @Override public int getCount() { return colors.size(); } @Override public Object getItem(int position) { return colors.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; // 如果没有设置过,初始化convertView if (convertView == null) { // 获得设置的view convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_adapter, parent, false); // 初始化holder holder = new ViewHolder(); holder.hSView = (HorizontalScrollView) convertView.findViewById(R.id.hsv); holder.action = convertView.findViewById(R.id.ll_action); holder.btOne = (Button) convertView.findViewById(R.id.button1); holder.btTwo = (Button) convertView.findViewById(R.id.button2); holder.btThree = (Button) convertView.findViewById(R.id.button3); holder.tvContent = (TextView) convertView.findViewById(R.id.tv); // 设置内容view的大小为屏幕宽度,这样按钮就正好被挤出屏幕外 holder.content = convertView.findViewById(R.id.ll_content); LayoutParams lp = holder.content.getLayoutParams(); lp.width = mScreentWidth; convertView.setTag(holder); } else { // 有直接获得ViewHolder holder = (ViewHolder) convertView.getTag(); } // 把位置放到view中,这样点击事件就可以知道点击的是哪一条item holder.btOne.setTag(position); holder.btTwo.setTag(position); holder.btThree.setTag(position); // 设置监听事件 convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (view != null) { ViewHolder viewHolder1 = (ViewHolder) view.getTag(); viewHolder1.hSView.smoothScrollTo(0, 0); } case MotionEvent.ACTION_UP: // 获得ViewHolder ViewHolder viewHolder = (ViewHolder) v.getTag(); view = v; // 获得HorizontalScrollView滑动的水平方向值. int scrollX = viewHolder.hSView.getScrollX(); // 获得操作区域的长度 int actionW = viewHolder.action.getWidth(); // 注意使用smoothScrollTo,这样效果看起来比较圆滑,不生硬 // 如果水平方向的移动值<操作区域的长度的一半,就复原 if (scrollX < actionW / 2) { viewHolder.hSView.smoothScrollTo(0, 0); } else{// 否则的话显示操作区域 viewHolder.hSView.smoothScrollTo(actionW, 0); } return true; } return false; } }); // 这里防止删除一条item后,ListView处于操作状态,直接还原 if (holder.hSView.getScrollX() != 0) { holder.hSView.scrollTo(0, 0); } // 设置背景颜色,设置填充内容. holder.content.setBackgroundResource(colors.get(position)); holder.tvContent.setText("" + position); // 设置监听事件 holder.btOne.setOnClickListener(this); holder.btTwo.setOnClickListener(this); holder.btThree.setOnClickListener(this); return convertView; } /** * ViewHolder * * @Title: * @Description:主要是避免了不断的view获取初始化. */ class ViewHolder { public HorizontalScrollView hSView; public View content; public TextView tvContent; public View action; public Button btOne; public Button btTwo; public Button btThree; } @Override public void onClick(View v) { int position = (Integer) v.getTag(); switch (v.getId()) { case R.id.button1: colors.add(R.color.zise); break; case R.id.button2: colors.remove(position); break; case R.id.button3: if (colors.get(position) == R.color.zise) { colors.set(position, R.color.huang); } else { colors.set(position, R.color.zise); } break; default: break; } // 刷新ListView内容 notifyDataSetChanged(); } }
OK! listview侧滑功能就实现了(原理:HorizontalScrollView嵌套ListView).
相关文章推荐
- 文件存储与B+数据结构
- 【MATLAB 学习笔记】 SimMechanics 流程攻略(1)
- 一个牧场目前一共有20头刚出生的羊,母羊、公羊各一半。假如母羊5岁时后每年生一胎(母羊,公羊各一半)。羊活到10岁后死亡。请问20年后这个牧场有多少只羊? 请用C#写出算法。
- 2016 UESTC Training for Dynamic Programming P - 柱爷的矩阵 矩阵、递推
- php面试总结
- 剑指offer03--空格用%20替换
- Python多线程编程
- 第10,11周 项目一(3)
- 使用 NGINX 进行微程序缓存的好处
- JavaScript经典题目汇总
- MySQL事务管理+安全管理+MySQL数据类型
- 指针和数组名
- 应用服务器知识(5)--Tomcat源码分析
- Servlet监听器
- IOS Socket 04-利用框架CocoaAsyncSocket实现客户端/服务器端
- 随机模拟与采样方法
- HttpClient 发送HTTP请求
- UVA10791 Minimum Sum LCM(数论)
- EditPlus注册码在线生成,强大
- 为何CNAME和MX不能共存