This article is longer, about ten minutes to read
In my knowledge [planet] Hi Android, there is a question how a star of Friends accomplish this, exactly, this can be considered a good knowledge point, similar to today's headlines, thus, take a moment to summarize the article for everyone read
Many ways to achieve this, but my first thought is to use RecyclerView do, there may be many other implementations, here I was just referring to my way of thinking, to achieve this effect.
A .TabLayout + ViewPager
Home TabLayout + ViewPager can be used to achieve, but also more convenient and simple a way, we have to rely on the introduction of need:
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
Then go to achieve our layout:
The layout is such that, by a relative layout icon to add a cover TabLayout above, and TabLayout as there is a title navigation bar, under the control of ViewPager, and ViewPager need to bind Fragment, then the question is, how to two-way binding it? The code can look as follows:
通过addOnPageChangeListener以及监听addOnTabSelectedListener来实现的双向滑动监听与控制,其中这段代码出现了几个不同的类,比如TagManager,ContentAdapter我们来看下他们是什么内容?
TagManager只是我为了管理静态的列表数据所创建的类,并没有特殊的用处,因为是Demo,所以定义方面都是比较随意的
ContentAdapter是ViewPager的适配器,可以看到我为每一个Item都new了一个Fragment并且将标题传入,但是在实际项目中,可能就不是传标题,而是传ID了,我们继续往下看
在Fragment中,我将获取的title进行了网络请求,并将请求结果显示出来,这个DataManager就是我为了模拟数据而创建的,loadNetworkData模拟网络数据请求:
这里很好理解,延时2s后将标题和当前的时间返回回去的一个过程,现在我们运行一下即可:
并且左右滑动上下都是可以联动的,这就是我们想要的一个效果了,那么我们接下来就需要解决标签管理id问题了,点击+号按钮直接跳转到标签管理页面
二.RecyclerView
因为我们的标签都已经定义好了,所以现在想实现这个页面太简单了,首先给大家看下我通过DataManager模拟的数据
这两个方法只要在开头调用一下就好了,我们的数据就有了,布局写起来也相当简单
考虑到可能会有很多数据,所以选择了ScrollView,并且这里存在两个RecyclerView,你也可以一个RecyclerView多Type形式来做,都是没问题的,所以我们来看下这个比较简单的适配器吧
这个适配器比较简单,就没有什么可说的了,我们直接看核心代码:
First we look at the code in onCreate, setDisplayHomeAsUpEnabled was to show the title bar of the return key, and onOptionsItemSelected is his response to the click event, and create two RecyclerView is relatively simple, using GridLayoutManager, really should be noted that ItemAddDel this way, when you click on the item to be shown, but in reality delete, add and delete the item below, such a logic, the final results shown in Figure:
Here I did not do press and drag and drop feature, because it is another way, and random I stole a lazy, Gif renderings will be placed at the end of the text, where we continue to talk down, we will close after you have added the current page, regardless of where the top left corner of the return key or click on the bACK button system, we have to tell MainActivity setResult I have broken the back of the
The logic here is relatively simple, because I'm not sure in the end user in the tag management page number of additions and deletions made operational, so I direct removeAllTags, and then re-addTab, and because of changes in data sources, ViewPager have to refresh
Our final look at the effect of it:
But you will find that doing so will still be abnormal, and the data is not refreshed, it is completely refreshed ViewPager on, we need to modify his adapter to achieve our objective:
Change is relatively large, we do it by Tag tag and then go to the update by FragmentManager, so that you can look at our final results:
III. Summary
This effect should be not difficult, but the idea needs to hold their own, of course, my writing is not very good, you can continue to optimize, including the long press can increase the drag aspect, cache data, the speed can continue to re-optimize it.
Download on source, you can go to the comments section of my knowledge of the planet in this article Click to read or download original
You can also add Android Developer exchange group: 417 046 685 downloads