Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)


demo源码,需要可以下载
1.这是一个github开源项目,先去github上面下载,github下载地址。
2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应的lid应用,如图勾选。Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)_第1张图片
3.activity_main.xml 的布局文件代码:

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

主要是加入这个控件。
4.item_list_app.xml布局文件,代码如下:

<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="wrap_content" android:padding="8dp" >  

    <ImageView android:id="@+id/iv_icon" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher" />  

    <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/iv_icon" android:text="name" android:textColor="@android:color/black" android:textSize="18sp" />  

</RelativeLayout>  

5.加入ic_delete.png图片。
6.定义扩展自BaseAdapter的adapter,用来提供列表数据。
同时重写swipeMenu的回调方法,主要是实现SwipeMenuCreator 这个类的create()方法,和相应的OnMenuItemClickListener接口。至于数据来源则是List类型的,是由 getPackageManager().getInstalledApplications(0); 方法获得。
如果要重用这个代码,需要实现SwipeMenuCreator 类和相应的OnMenuItemClickListener接口。
主要代码如下:

public class MainActivity extends Activity {  

    private List<ApplicationInfo> mAppList;  
    private AppAdapter mAdapter;  
    private SwipeMenuListView mListView;  

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

        mAppList = getPackageManager().getInstalledApplications(0);  

        mListView = (SwipeMenuListView) findViewById(R.id.listView);  
        mAdapter = new AppAdapter();  
        mListView.setAdapter(mAdapter);  

        SwipeMenuCreator creator = new SwipeMenuCreator() {  

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

                // create "delete" item 
                SwipeMenuItem deleteItem = new SwipeMenuItem(  
                        getApplicationContext());  
                // set item background 
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,  
                        0x3F, 0x25)));  
                // set item width 
                deleteItem.setWidth(dp2px(90));  
                // set a icon 
                deleteItem.setIcon(R.drawable.ic_delete);  
                // add to menu 
                menu.addMenuItem(deleteItem);  
            }  
        };  

        // set creator 
        mListView.setMenuCreator(creator);  

        mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {  
            @Override  
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {  
                switch (index) {  
                case 0:  
                    // open 
                    Toast.makeText(getApplicationContext(), "Open", Toast.LENGTH_SHORT).show();  
                    break;  
                case 1:  
                    // delete 
                    Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();  
// mAppList.remove(position); 
// mAdapter.notifyDataSetChanged(); 
                    break;  
                }  
                // false : close the menu; true : not close the menu 
                return false;  
            }  
        });  

    }  
    class AppAdapter extends BaseAdapter {  

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

        @Override  
        public ApplicationInfo getItem(int position) {  
            return mAppList.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(getApplicationContext(),  
                        R.layout.item_list_app, null);  
                new ViewHolder(convertView);  
            }  
            ViewHolder holder = (ViewHolder) convertView.getTag();  
            ApplicationInfo item = getItem(position);  
            holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager()));  
            holder.tv_name.setText(item.loadLabel(getPackageManager()));  
            return convertView;  
        }  

        class ViewHolder {  
            ImageView iv_icon;  
            TextView tv_name;  

            public ViewHolder(View view) {  
                iv_icon = (ImageView) view.findViewById(R.id.iv_icon);  
                tv_name = (TextView) view.findViewById(R.id.tv_name);  
                view.setTag(this);  
            }  
        }  
    }  

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

你可能感兴趣的:(源码,android,ListView,控件,github上)