本文實例為大家分享了Android仿QQ長按刪除彈出框的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)專注于成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對待客戶,用專業(yè)的服務(wù)創(chuàng)造價值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。廢話不說,先看一下效果圖:
對于列表來說,如果想操作某個列表項,一般會采用長按彈出菜單的形式,默認(rèn)的上下文菜單比較難看,而QQ的上下文菜單就人性化多了,整個菜單給用戶一種氣泡彈出的感覺,而且會顯示在手指按下的位置,而技術(shù)實現(xiàn)我之前是使用popupWindow和RecyclerView實現(xiàn)的,上面一個RecyclerView,下面一個小箭頭ImageView,但后來發(fā)現(xiàn)沒有必要,而且可定制化也不高,還是使用多個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 ListmDataList = 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(); } }