文件名称:
SwipeListViewTest项目
开发工具:
文件大小: 2mb
下载次数: 0
上传时间: 2016-05-23
详细说明: 先来看activity_main.xml 这里就一个swipelistview控件,我说几个不易理解的属性 表示滑动时的操作,dismiss表示滑动时删除,如果设置为reveal表示滑动时会显示出item后面的选项 swipe:swipeActionLeft=”dismiss” swipe:swipeActionRight=”reveal” 这个是背面布局的id(我们把直接看到的布局叫做前面的,滑动之后才能看到的布局叫做背面的),必须与背面布局id对应 swipe:swipeBackView=”@+id/back” 这个是滚动时候是否关闭背面的布局,true表示关闭,false表示不关闭,一般设置为true swipe:swipeCloseAllItemsWhenMoveList=”true” 这个是前面布局的id,要与布局的id对应 swipe:swipeFrontView=”@+id/front” both表示可以向左滑也可以向右滑,right和left分别表示只能向有或者向左滑动。 swipe:swipeMode=”both” 下面两个表示向左或者向右滑动时的偏移量,一般不在xml文件中设置,而是在代码中根据设置的大小来设置偏移量。 swipe:swipeOffsetLeft=”0dp” swipe:swipeOffsetRight=”0dp” 再来看看Item布局文件,这里包括前面的和后面的,两个重叠在一起: 这个布局是一个常规布局,我就不解释了。 MainActivity.Java,关键地方都有注释 public class MainActivity extends Activity { private SwipeListView mSwipeListView ; private SwipeAdapter mAdapter ; public static int deviceWidth ; private List testData ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSwipeListView = (SwipeListView) findViewById(R.id.example_lv_list); testData = getTestData(); //数据适配 mAdapter = new SwipeAdapter(this, R.layout.package_row, testData,mSwipeListView); //拿到设备宽度 deviceWidth = getDeviceWidth(); mSwipeListView.setAdapter(mAdapter); //设置事件监听 mSwipeListView.setSwipeListViewListener( new TestBaseSwipeListViewListener()); reload(); } private List getTestData() { String [] obj = new String[]{"红楼梦","西游记","水浒传","管锥编","宋诗选注","三国演义","android开发高级编程","红楼梦","西游记","水浒传","管锥编","宋诗选注","三国演义","android开发高级编程"}; List list = new ArrayList(Arrays.asList(obj)); return list; } private int getDeviceWidth() { return getResources().getDisplayMetrics().widthPixels; } private void reload() { // mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT); // mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL); // mSwipeListView.setSwipeActionRight(settings.getSwipeActionRight()); //滑动时向左偏移量,根据设备的大小来决定偏移量的大小 mSwipeListView.setOffsetLeft(deviceWidth * 1 / 3); mSwipeListView.setOffsetRight(deviceWidth * 1 / 3); // mSwipeListView.setOffsetRight(convertDpToPixel(settings.getSwipeOffsetRight())); //设置动画时间 mSwipeListView.setAnimationTime(30); mSwipeListView.setSwipeOpenOnLongPress(false); } class TestBaseSwipeListViewListener extends BaseSwipeListViewListener{ //点击每一项的响应事件 @Override public void onClickFrontView(int position) { super.onClickFrontView(position); Toast.makeText(getApplicationContext(), testData.get(position), Toast.LENGTH_SHORT).show(); } //关闭事件 @Override public void onDismiss(int[] reverseSortedPositions) { for (int position : reverseSortedPositions) { Log.i("lenve", "position--:"+position); testData.remove(position); } mAdapter.notifyDataSetChanged(); } } } 数据适配器: public class SwipeAdapter extends ArrayAdapter { private LayoutInflater mInflater ; private List objects ; private SwipeListView mSwipeListView ; public SwipeAdapter(Context context, int textViewResourceId,List objects, SwipeListView mSwipeListView) { super(context, textViewResourceId, objects); this.objects = objects ; this.mSwipeListView = mSwipeListView ; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null ; if(convertView == null){ convertView = mInflater.inflate(R.layout.package_row, parent, false); holder = new ViewHolder(); holder.mFrontText = (TextView) convertView.findViewById(R.id.example_row_tv_title); holder.mBackEdit = (Button) convertView.findViewById(R.id.example_row_b_action_3); holder.mBackDelete = (Button) convertView.findViewById(R.id.example_row_b_action_2); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.mBackDelete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //关闭动画 mSwipeListView.closeAnimate(position); //调用dismiss方法删除该项(这个方法在MainActivity中) mSwipeListView.dismiss(position); } }); String item = getItem(position); holder.mFrontText.setText(item); return convertView; } class ViewHolder{ TextView mFrontText ; Button mBackEdit,mBackDelete ; } } 以上就是SwipeListViewTest的用法,希望对你有帮助 ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.