网站建设资讯

NEWS

网站建设资讯

listview向左滑有删除功能-创新互联

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

我们提供的服务有:成都网站设计、成都网站制作、微信公众号开发、网站优化、网站认证、平定ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的平定网站制作公司

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" />

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:listview向左滑有删除功能-创新互联
分享地址:http://cdweb.net/article/disjop.html