Android仿QQ长按弹出删除复制框-创新互联

本文实例为大家分享了Android仿QQ长按删除弹出框的具体代码,供大家参考,具体内容如下

成都创新互联专注于成都网站制作、成都做网站、外贸营销网站建设、网页设计、网站制作、网站开发。公司秉持“客户至上,用心服务”的宗旨,从客户的利益和观点出发,让客户在网络营销中找到自己的驻足之地。尊重和关怀每一位客户,用严谨的态度对待客户,用专业的服务创造价值,成为客户值得信赖的朋友,为客户解除后顾之忧。

废话不说,先看一下效果图:

Android仿QQ长按弹出删除复制框Android仿QQ长按弹出删除复制框Android仿QQ长按弹出删除复制框

对于列表来说,如果想操作某个列表项,一般会采用长按弹出菜单的形式,默认的上下文菜单比较难看,而QQ的上下文菜单就人性化多了,整个菜单给用户一种气泡弹出的感觉,而且会显示在手指按下的位置,而技术实现我之前是使用popupWindow和RecyclerView实现的,上面一个RecyclerView,下面一个小箭头ImageView,但后来发现没有必要,而且可定制化也不高,还是使用多个TextView更好一点。

我封装了一下,只需要一个PopupList.java文件。源码放在了git上,git地址   

使用方式,很简单:

只需要一个PopupList.java文件和几行代码,你就可以为ListView,GridView,甚至任意View绑定一个长按弹出的水平气泡式菜单。
你可以这样使用:

public class MainActivity extends AppCompatActivity {
 
 
 private Button btn_long_click;
 private ListView lv_main;
 private List mDataList = new ArrayList<>();
 private ArrayAdapter mDataAdapter;
 private List popupMenuItemList = new ArrayList<>();
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btn_long_click = (Button) findViewById(R.id.btn_long_click);
  lv_main = (ListView) findViewById(R.id.lv_main);
  mDataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, mDataList);
  lv_main.setAdapter(mDataAdapter);
 
 
  popupMenuItemList.add(getString(R.string.copy));
  popupMenuItemList.add(getString(R.string.delete));
  popupMenuItemList.add(getString(R.string.share));
  popupMenuItemList.add(getString(R.string.more));
  PopupList popupList = new PopupList(this);
  popupList.bind(lv_main, popupMenuItemList, new PopupList.PopupListListener() {
   @Override
   public boolean showPopupList(View adapterView, View contextView, int contextPosition) {
    return true;
   }
 
 
   @Override
   public void onPopupListClick(View contextView, int contextPosition, int position) {
    Toast.makeText(MainActivity.this, contextPosition + "," + position, Toast.LENGTH_SHORT).show();
   }
  });
 
 
  PopupList normalViewPopupList = new PopupList(this);
  normalViewPopupList.bind(btn_long_click, popupMenuItemList, new PopupList.PopupListListener() {
   @Override
   public boolean showPopupList(View adapterView, View contextView, int contextPosition) {
    return true;
   }
 
 
   @Override
   public void onPopupListClick(View contextView, int contextPosition, int position) {
    Toast.makeText(MainActivity.this, contextPosition + "," + position, Toast.LENGTH_SHORT).show();
   }
  });
 
 
  lv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(MainActivity.this, "onItemClicked:" + position, Toast.LENGTH_SHORT).show();
   }
  });
  btn_long_click.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    startActivity(new Intent(MainActivity.this, SecondaryActivity.class));
   }
  });
  getData();
 }
 
 
 private void getData() {
  for (int i = 0; i < 40; i++) {
   mDataList.add("No." + i);
  }
  mDataAdapter.notifyDataSetChanged();
 }
 
 
}

网站栏目:Android仿QQ长按弹出删除复制框-创新互联
文章地址:http://scyanting.com/article/djhchi.html