Android仿QQ長(zhǎng)按彈出刪除復(fù)制框
本文實(shí)例為大家分享了Android仿QQ長(zhǎng)按刪除彈出框的具體代碼,供大家參考,具體內(nèi)容如下
廢話不說(shuō),先看一下效果圖:




對(duì)于列表來(lái)說(shuō),如果想操作某個(gè)列表項(xiàng),一般會(huì)采用長(zhǎng)按彈出菜單的形式,默認(rèn)的上下文菜單比較難看,而QQ的上下文菜單就人性化多了,整個(gè)菜單給用戶(hù)一種氣泡彈出的感覺(jué),而且會(huì)顯示在手指按下的位置,而技術(shù)實(shí)現(xiàn)我之前是使用popupWindow和RecyclerView實(shí)現(xiàn)的,上面一個(gè)RecyclerView,下面一個(gè)小箭頭ImageView,但后來(lái)發(fā)現(xiàn)沒(méi)有必要,而且可定制化也不高,還是使用多個(gè)TextView更好一點(diǎn)。
我封裝了一下,只需要一個(gè)PopupList.java文件。源碼放在了git上,git地址
使用方式,很簡(jiǎn)單:
只需要一個(gè)PopupList.java文件和幾行代碼,你就可以為L(zhǎng)istView,GridView,甚至任意View綁定一個(gè)長(zhǎng)按彈出的水平氣泡式菜單。
你可以這樣使用:
public class MainActivity extends AppCompatActivity {
private Button btn_long_click;
private ListView lv_main;
private List<String> mDataList = new ArrayList<>();
private ArrayAdapter<String> mDataAdapter;
private List<String> 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();
}
}
注意:`bind()`方法要求你的anchorView不能有touch和longClick/itemLongClick監(jiān)聽(tīng),因?yàn)閌bind()`方法會(huì)主動(dòng)給anchorView設(shè)置監(jiān)聽(tīng)器:
mAnchorView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mRawX = event.getRawX();
mRawY = event.getRawY();
return false;
}
});
if (mAnchorView instanceof AbsListView) {
((AbsListView) mAnchorView).setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mPopupListListener != null
&& !mPopupListListener.showPopupList(parent, view, position)) {
return false;
}
mAdapterView = parent;
mContextView = view;
mContextPosition = position;
showPopupListWindow();
return true;
}
});
} else {
mAnchorView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mPopupListListener != null
&& !mPopupListListener.showPopupList(v, v, 0)) {
return false;
}
mContextView = v;
mContextPosition = 0;
showPopupListWindow();
return true;
}
});
}
這就限制了anchorView不能再添加touch監(jiān)聽(tīng),不能添加longClick監(jiān)聽(tīng),如果你需要添加監(jiān)聽(tīng)可以使用`showPopupListWindow`方法主動(dòng)來(lái)顯示彈出框,而不是使用`bind()`方法:
public class SecondaryActivity extends AppCompatActivity {
private ListView lv_main;
private List<String> mDataList = new ArrayList<>();
private ArrayAdapter<String> mDataAdapter;
private List<String> popupMenuItemList = new ArrayList<>();
private float mRawX;
private float mRawY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondary);
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));
lv_main.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mRawX = event.getRawX();
mRawY = event.getRawY();
return false;
}
});
lv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SecondaryActivity.this, "onItemClicked:" + position, Toast.LENGTH_SHORT).show();
}
});
lv_main.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
PopupList popupList = new PopupList(view.getContext());
popupList.showPopupListWindow(view, position, mRawX, mRawY, 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(contextView.getContext(), contextPosition + "," + position, Toast.LENGTH_SHORT).show();
}
});
return true;
}
});
getData();
}
private void getData() {
for (int i = 0; i < 40; i++) {
mDataList.add("No." + i);
}
mDataAdapter.notifyDataSetChanged();
}
}
更詳細(xì)的配置:
- popupList.setIndicatorView(View)
- popupList.setIndicatorSize(int, int)
- popupList.setNormalBackgroundColor(int)
- popupList.setPressedBackgroundColor(int)
- popupList.setNormalTextColor(int)
- popupList.setPressedTextColor(int)
- popupList.setDividerColor(int)
- popupList.setTextSize(float)
- popupList.setTextPadding(int, int, int, int)
- popupList.setTextPaddingLeft(int)
- popupList.setTextPaddingTop(int)
- popupList.setTextPaddingRight(int)
- popupList.setTextPaddingBottom(int)
- popupList.setBackgroundCornerRadius(int)
- popupList.setDividerWidth(int)
- popupList.setDividerHeight(int)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄
通常手機(jī)通訊錄都會(huì)有索引欄,這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄,現(xiàn)在分享給大家。2016-10-10
android教程viewpager自動(dòng)循環(huán)和手動(dòng)循環(huán)
這篇文章主要介紹了android的viewpager自動(dòng)循環(huán)和手動(dòng)循環(huán)示例,需要的朋友可以參考下2014-02-02
Android自定義View實(shí)現(xiàn)仿GitHub的提交活躍表格
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)仿GitHub的提交活躍表格,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
Android實(shí)現(xiàn)截圖分享qq 微信功能
在日常生活中,經(jīng)常用到qq,微信截圖分享功能,今天小編通過(guò)本文給大家介紹Android實(shí)現(xiàn)截圖分享qq 微信功能,具體實(shí)現(xiàn)代碼大家參考下本文2017-12-12
Android自定義SeekBar滑動(dòng)顯示數(shù)字
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar滑動(dòng)顯示數(shù)字,使用FrameLayout結(jié)合SeekBar滑動(dòng)時(shí),數(shù)值顯示,滑動(dòng)停止時(shí)顯示數(shù)字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android開(kāi)發(fā)之在xml中設(shè)置自定義屬性的方法
下面小編就為大家分享一篇Android開(kāi)發(fā)之在xml中設(shè)置自定義屬性的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android Studio導(dǎo)入項(xiàng)目非常慢的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Studio導(dǎo)入項(xiàng)目非常慢的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

