使用過今日頭條的伙計(jì)們對(duì)這個(gè)效果肯定很熟悉。拖拽可排序,點(diǎn)擊標(biāo)簽后可以刪除。今天我們采用RecyclerView來實(shí)現(xiàn)。

實(shí)現(xiàn)思路:
通過ItemTouchHelper來綁定RecyclerView的子控件觸摸事件。
當(dāng)滑動(dòng)拖拽的時(shí)候,通知適配器來交換兩個(gè)子控件的顯示位置。
更改數(shù)據(jù)源,使數(shù)據(jù)源與子空間顯示內(nèi)容一致。
這就是實(shí)現(xiàn)的基本思路,是不是很簡(jiǎn)單?當(dāng)然,首先要了解一下ItemTouchHelper這哥們兒是干啥的,有什么作用。
This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView.
It works with a RecyclerView and a Callback class, which configures what type of interactions are enabled and also receives events when user performs these actions.
Depending on which functionality you support, you should override onMove(RecyclerView, ViewHolder, ViewHolder) and / or onSwiped(ViewHolder, int).
This class is designed to work with any LayoutManager but for certain situations, it can be optimized for your custom LayoutManager by extending methods in the ItemTouchHelper.Callback class or implementing ItemTouchHelper.ViewDropHandler interface in your LayoutManager.
By default, ItemTouchHelper moves the items' translateX/Y properties to reposition them. You can customize these behaviors by overriding onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, boolean) or onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean).
Most of the time you only need to override onChildDraw.
通過API文檔的介紹,這個(gè)哥們兒是為RecyclerView工作的,他需要一個(gè)CallBack,可以回調(diào)RecyclerView的子控件滑動(dòng)和拖拽事件,而且也可以通過這個(gè)CallBack重繪我們的子view。這就一目了然了嘛。通過使用ItemTouchHelper,可以很輕松的就實(shí)現(xiàn)了RecyclerView觸摸事件的回調(diào)。換句話說,只要我們?yōu)镽ecyclerView 綁定了ItemTouchHelper之后,RecyclerView子控件的拖動(dòng)和滑動(dòng)事件已經(jīng)幫我們實(shí)現(xiàn)了。我們所要做的就是在觸摸事件之后,如何去改變?nèi)ジ翧dapter和改變我們的數(shù)據(jù)。
開始擼碼:
public class ChannelActivity extends Activity { public RecyclerView rv; public List<DataBean> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_channel); initData(); initView(); } private void initView() { rv = (RecyclerView) findViewById(R.id.rl_view); rv.setLayoutManager(new GridLayoutManager(this, 4)); MyAdapter adapter = new MyAdapter(this, list); rv.setAdapter(adapter); //關(guān)聯(lián)ItemTouchHelper ItemTouchHelper touchHelper = new ItemTouchHelper(new MyItemTouchCallBack(adapter)); touchHelper.attachToRecyclerView(rv); } private void initData() { DataBean bean1 = new DataBean("體育", 0, "url"); DataBean bean2 = new DataBean("新聞", 1, "url"); DataBean bean3 = new DataBean("影視", 2, "url"); DataBean bean4 = new DataBean("電視劇", 3, "url"); DataBean bean5 = new DataBean("熱點(diǎn)", 4, "url"); DataBean bean6 = new DataBean("推薦", 5, "url"); DataBean bean7 = new DataBean("
主站蜘蛛池模板:
寻甸|
万源市|
册亨县|
福州市|
开鲁县|
漠河县|
舒城县|
类乌齐县|
朝阳县|
榆树市|
云南省|
卫辉市|
井陉县|
虎林市|
江安县|
环江|
黄大仙区|
大同市|
沙雅县|
尼玛县|
正阳县|
德江县|
漳州市|
昌吉市|
兰坪|
淮安市|
琼结县|
道真|
高密市|
芦山县|
织金县|
吉首市|
东方市|
景洪市|
汕头市|
萍乡市|
甘德县|
桐乡市|
安龙县|
安国市|
东乡县|