Android 高仿微信(QQ)滑动弹出编辑、删除菜单效果,增加下拉刷新功能

  1. 云栖社区>
  2. 博客>
  3. 正文

Android 高仿微信(QQ)滑动弹出编辑、删除菜单效果,增加下拉刷新功能

无名公子 2015-12-13 19:19:00 浏览2615
展开阅读全文

  不可否认,微信、QQ列表的滑动删除、编辑功能着实很经典(从IOS那边模仿过来的),然、Android这边,对列表的操作,其实大多还停留上下文菜单来实现。 

  Android如何实现list item的滑动呢?介绍一个很简单实用的开源项目:

  https://github.com/baoyongzhang/SwipeMenuListView

  使用该开源项目,我们只需要很简单的一些逻辑,就可以实现滑动菜单功能了!先看下效果图:

     



1. 下载开源项目,并将其中的liberary导入到自己的项目中:



2. 使用SwipeMenuListView代替ListView,在页面中布局:

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshMusic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.baoyz.swipemenulistview.SwipeMenuListView
            android:id="@+id/listViewMusic"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>

 注: SwipeRefreshLayout主要是为了给该列表增加下拉刷新功能!


3. 为该ListView创建适配器,与正常的实现并无二致:

public class MusicAdapter extends BaseAdapter {

    private List<Music> mDatas;

    private Context mContext;

    public MusicAdapter(List<Music> mDatas, Context mContext) {
        this.mDatas = mDatas;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Music getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.list_item_music, null);
            new ViewHolder(convertView);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();

        Music music = mDatas.get(position);

        holder.textTitle.setText(music.title);
        holder.textDesc.setText(music.desc);

        return convertView;
    }

    class ViewHolder {
        TextView textTitle;
        TextView textDesc;

        public ViewHolder(View view) {
            textTitle = (TextView) view.findViewById(R.id.textTitle);
            textDesc = (TextView) view.findViewById(R.id.textDesc);
            view.setTag(this);
        }
    }
}


4. 为SwipeMenuListView实现Adapter,并在之后,创建滑动菜单:

public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.listViewMusic)
    private SwipeMenuListView listViewMusic;

    @ViewInject(R.id.swipeRefreshMusic)
    private SwipeRefreshLayout swipeRefreshMusic;

    private List<Music> musics;

    private MusicAdapter musicAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        x.view().inject(this);
        musics = new ArrayList<>();

        for (int i = 0; i < 4; i++) {

            Music music = new Music();
            music.title = "上邪 : " + i;
            music.desc = "我欲与君相知,长命无绝衰";
            musics.add(music);
        }

        musicAdapter = new MusicAdapter(musics, this);
        listViewMusic.setAdapter(musicAdapter);

        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "edit" item
                SwipeMenuItem editItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                editItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                        0xCE)));
                // set item width
                editItem.setWidth(dp2px(90));
                // set item title
                editItem.setTitle("编辑");
                // set item title fontsize
                editItem.setTitleSize(18);
                // set item title font color
                editItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(editItem);

                // create "edit" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.parseColor("#ff0000")));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set item title
                deleteItem.setTitle("删除");
                // set item title fontsize
                deleteItem.setTitleSize(18);
                // set item title font color
                deleteItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };

        listViewMusic.setMenuCreator(swipeMenuCreator);

        listViewMusic.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {// index即是操作menu添加的顺序
                    case 0:
                        Toast.makeText(MainActivity.this, "编辑 : " + position, Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(MainActivity.this, "删除 : " + position, Toast.LENGTH_SHORT).show();
                        musics.remove(position);
                        musicAdapter.notifyDataSetChanged();
                        break;
                }

                return false;
            }
        });

        swipeRefreshMusic.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                swipeRefreshMusic.setRefreshing(true);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        for (int i = 0; i < 4; i++) {

                            Music music = new Music();
                            music.title = "无缘 : " + i;
                            music.desc = "风雨千山玉独行,天下倾心叹无缘";
                            musics.add(music);
                        }
                        musicAdapter.notifyDataSetChanged();

                        swipeRefreshMusic.setRefreshing(false);
                    }
                }, 2000);

            }
        });
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}

注: 

  1. SwipeMenuCreator是为了给该listitem创建菜单使用,需要实现该的接口中的create方法,在该方法中,可以自由创建菜单项,比如删除、编辑,可以设置菜单的背景颜色,字体颜色大小等; 

  2. 在创建SwipeMenuCreator实例后,需为列表设置菜单:listViewMusic.setMenuCreator(swipeMenuCreator);

  3. 通过设置SwipeMenuListView的setOnMenuItemClickListener方法,监听菜单的点击事件,通过onMenuItemClick(int position, SwipeMenu menu, int index)方法,对菜单事件进行处理;

  4. 通过设置SwipeMenuListView的setOnRefreshListener方法,为菜单添加下拉刷新功能!


如此这般,便可完美的实现的list item滑动编辑删除功能啦,欢迎指正!

如有疑问,欢迎进QQ群:487786925( Android研发村 )



源码下载地址:https://github.com/zuiwuyuan/SwipeRefreshStickyList  

网友评论

登录后评论
0/500
评论
无名公子
+ 关注