温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

listview向左滑有删除功能

发布时间:2020-07-09 16:07:17 来源:网络 阅读:1139 作者:范恒标 栏目:移动开发

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

public class MainActivity extends Activity {

private AppAdapter mAdapter;

private SwipeMenuListView mListView;

ArrayList<String> data=new ArrayList<String>();

@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也是很简单的。

<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="match_parent"

    tools:context=".MainActivity" >

 <com.baoyz.swipemenulistview.SwipeMenuListView

        android:id="@+id/listView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

</RelativeLayout>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI