189 8069 5689

listview向左滑有删除功能

我当时也是比较迷茫选哪个控件比较好,后面再github上找到一个这个SwipeMenuListView开源项目完成的,使用这个项目非常简单,里面有两个例子,复杂的跟简单的,我当时自己写了个Demos来学习,废话不多说了,直接贴代码。

创新互联服务项目包括定南网站建设、定南网站制作、定南网页制作以及定南网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,定南网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到定南省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

public class MainActivity extends Activity {

private AppAdapter mAdapter;

private SwipeMenuListView mListView;

ArrayList data=new ArrayList();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

data.add("position为"+i);

}

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);

}

};

mListView.setMenuCreator(creator);

mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {

@Override

public void onMenuItemClick(int position, SwipeMenu menu, int index) {

//index=0 open index =1 删除

switch (index) {

case 0:

Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();

break;

case 1:

// Log.e("position", ""+position);

data.remove(position);

Log.e("onMenuItemClick 1", "position="+position+"##index="+index+"data.tos"+data.toString());

Toast.makeText(MainActivity.this, ""+position+"index="+index, Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

mAdapter.notifyDataSetChanged();

}

}, 500);

break;

default:

break;

}

}

});

}

class AppAdapter extends BaseAdapter{

@Override

public int getCount() {

// TODO Auto-generated method stub

Log.e("getview", data.toString());

return data.size();

}

@Override

public String getItem(int position) {

// TODO Auto-generated method stub

return data.get(position);

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// biao bb = null;

// if(convertView==null){

// convertView = getLayoutInflater().inflate(R.layout.item, null);

// bb = new biao();

// bb.text = (TextView) convertView.findViewById(R.id.textView1);

// convertView.setTag(bb);

// }else{

// bb = (biao) convertView.getTag();

// }

// bb.text.setText(data.get(position));

// Log.e("tv", data.get(position));

if (convertView == null) {

convertView = View.inflate(getApplicationContext(),

R.layout.item, null);

new ViewHolder(convertView);

}

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

holder.tv.setText(data.get(position));

return convertView;

}

class ViewHolder {

TextView tv;

public ViewHolder(View view) {

tv = (TextView) view.findViewById(R.id.textView1);

view.setTag(this);

}

}

}

private int dp2px(int dp) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,

getResources().getDisplayMetrics());

}

}

主XML也是很简单的。

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

        android:id="@+id/listView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


网站题目:listview向左滑有删除功能
新闻来源:http://cdxtjz.cn/article/ppgsic.html

其他资讯