昨天使用ListView实现了列表定位的功能,然后又研究了下使用RecyclerView的实现方式,下面我们就探讨下实现思路。
ListView实现方式看这里
android仿朋友圈列表精准定位
先上图:
得益于RecyclerView的分离式结构,这里使用了三种布局分别测试:
LinearLayoutManager、GridLayoutManager、StaggeredGridLayoutManager
先说下列表数据,这里采用ArrayList添加字符串对象
List<String> list = new ArrayList<>();
for (int i = 0; i < 50; i++) {
if (i % 2 == 0) {
list.add("我是传说中的Java" + i + "\n" + "\n"
+ "因为我身体比较长" + "\n" + "\n"
+ "再加长一点" + "\n" + "\n"
+ "O(∩_∩)O哈哈哈~");
} else if (i % 3 == 0) {
list.add("KotLin" + i);
} else if (i % 5 == 0) {
list.add("ObjectC" + i);
} else if (i % 7 == 0) {
list.add("PHP" + i);
} else {
list.add("Linux" + i);
}
}
适配器继承RecyclerView.Adapter
使用ViewHolder设置具体的值,这里只贴出必须要覆写的三个方法
@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.recycler_item, parent, false);
return new RecyclerHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerHolder holder, int position) {
holder.tvInfo.setText(mItems.get(position));
}
@Override
public int getItemCount() {
return mItems.size();
}
static class RecyclerHolder extends RecyclerView.ViewHolder {
private TextView tvInfo;
public RecyclerHolder(View itemView) {
super(itemView);
tvInfo = (TextView) itemView.findViewById(R.id.textView);
}
}
布局采用CardView,美观与实用同在
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
app:cardElevation="8dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/cardview_light_background"
android:gravity="center"
android:minHeight="50dp"
android:text="TextView"
android:textColor="@color/colorAccent"
android:textSize="18sp" />
</android.support.v7.widget.CardView>
第一种线性布局结构LinearLayoutManager
:
LinearLayoutManager manager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(manager);
第二种宫格布局结构GridLayoutManager
:
GridLayoutManager manager = new GridLayoutManager(this, 3);
mRecyclerView.setLayoutManager(manager);
第三章流式布局结构StaggeredGridLayoutManager
:
StaggeredGridLayoutManager manager =
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(manager);
这三种布局我使用了菜单来切换,title采用的是ToolBar
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/linearlayout"
android:title="线性布局" />
<item
android:id="@+id/gridlayout"
android:title="宫格布局" />
<item
android:id="@+id/staggeredlayout"
android:title="流式布局" />
</menu>
使用ToolBar加载这个菜单布局并设置菜单点击事件监听
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.actionbar_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int type = 0;
switch (item.getItemId()) {
case R.id.linearlayout:
type = 0;
break;
case R.id.gridlayout:
type = 1;
break;
case R.id.staggeredlayout:
type = 2;
break;
}
changeLayoutManager(type);
adapter.notifyDataSetChanged();
location(type);
PreferenceTool.putInt("manage", type);
PreferenceTool.commit();
return true;
}
});
好了,接下来就是重中之重了,其实实现方式和listview差不多,都是在滚动事件中保存position和top,当然了,这里多保存了一个切换布局,recyclerview的添加滚动监听和listview不太一样,不是setOnScrollListener
而是addOnScrollListener
,匿名内部类使用的RecyclerView.OnScrollListener
而ListView使用的是AbsListView.OnScrollListener
这里只覆写onScrollStateChanged
方法就能达到我们想要的效果啦
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
注意第二个参数,它代表的是列表滚动的状态,共有三种状态
分别表示当前列表不在滚动、列表正在被手指拖拽、列表在以动画的方式滚动到一个最终的位置;所以我们直接判断当前列表是否停止滚动,所有保存状态的代码都在判断条件内
if (newState == RecyclerView.SCROLL_STATE_IDLE)
由于采用了切换布局的方式,所以要首先判断manager具体属于哪一个的实例
if (mRecyclerView.getLayoutManager() instanceof LinearLayoutManager)
然后强制转换layoutManager
LinearLayoutManager linearLayoutManager =
(LinearLayoutManager) mRecyclerView.getLayoutManager();
找到当前屏幕内第一个可见的item的position
linearLayoutManager.findFirstVisibleItemPosition()
接着根据这个position再找到对应的itemView
View itemView = linearLayoutManager.findViewByPosition(firstVisiblePosition)
如果这个View不为空就保存它的position和top值,这里的top值表示的是这个view距离顶部的偏移量
if (itemView != null) {
int top = itemView.getTop();
PreferenceTool.putInt("position", firstVisiblePosition);
PreferenceTool.putInt("top", top);
PreferenceTool.commit();
}
GridLayoutManager的使用方式和LinearLayoutManager一样,这里就不赘述了,具体说下StaggeredGridLayoutManager
由于我们设置的是两列,所以要找到的当前屏幕内可见的item有两个
int[] firstVisiblePositionArray = staggeredGridLayoutManager
.findFirstVisibleItemPositions(new int[2]);
我们只需找到整形数组的第一个view就可以了
View itemView = staggeredGridLayoutManager.findViewByPosition(firstVisiblePositionArray[0]);
if (itemView != null) {
int top = itemView.getTop();
PreferenceTool.putInt("position", firstVisiblePositionArray[0]);
PreferenceTool.putInt("top", top);
PreferenceTool.commit();
}
扯了这么多,真正能够实现定位的只有下面这行代码
scrollToPositionWithOffset(int position, int offset)
郁不郁闷!!!
源码中说的很明白了,position指的是item的索引,而offset指的是itemview的上边界和recyclerview的上边界之间的距离,这个距离可正可负;至此大功告成,下面贴下activity的源码仅供参考
public class RecyclerViewPositedAct extends AppCompatActivity {
RecyclerView mRecyclerView;
RecyclerAdapter adapter;
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.actionbar_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int type = 0;
switch (item.getItemId()) {
case R.id.linearlayout:
type = 0;
break;
case R.id.gridlayout:
type = 1;
break;
case R.id.staggeredlayout:
type = 2;
break;
}
changeLayoutManager(type);
adapter.notifyDataSetChanged();
location(type);
PreferenceTool.putInt("manage", type);
PreferenceTool.commit();
return true;
}
});
List<String> list = new ArrayList<>();
for (int i = 0; i < 50; i++) {
if (i % 2 == 0) {
list.add("我是传说中的Java" + i + "\n" + "\n"
+ "因为我身体比较长" + "\n" + "\n"
+ "再加长一点" + "\n" + "\n"
+ "O(∩_∩)O哈哈哈~");
} else if (i % 3 == 0) {
list.add("KotLin" + i);
} else if (i % 5 == 0) {
list.add("ObjectC" + i);
} else if (i % 7 == 0) {
list.add("PHP" + i);
} else {
list.add("Linux" + i);
}
}
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
int type = PreferenceTool.getInt("manage", 0);
changeLayoutManager(type);
adapter = new RecyclerAdapter(this, list);
mRecyclerView.setAdapter(adapter);
location(type);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
int firstVisiblePosition;
if (mRecyclerView.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager =
(LinearLayoutManager) mRecyclerView.getLayoutManager();
firstVisiblePosition = linearLayoutManager.findFirstVisibleItemPosition();
View itemView = linearLayoutManager.findViewByPosition(firstVisiblePosition);
if (itemView != null) {
int top = itemView.getTop();
PreferenceTool.putInt("position", firstVisiblePosition);
PreferenceTool.putInt("top", top);
PreferenceTool.commit();
}
} else if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager)
mRecyclerView.getLayoutManager();
firstVisiblePosition = gridLayoutManager.findFirstVisibleItemPosition();
View itemView = gridLayoutManager.findViewByPosition(firstVisiblePosition);
if (itemView != null) {
int top = itemView.getTop();
PreferenceTool.putInt("position", firstVisiblePosition);
PreferenceTool.putInt("top", top);
PreferenceTool.commit();
}
} else if (mRecyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager)
mRecyclerView.getLayoutManager();
int[] firstVisiblePositionArray = staggeredGridLayoutManager
.findFirstVisibleItemPositions(new int[2]);
View itemView = staggeredGridLayoutManager.findViewByPosition(firstVisiblePositionArray[0]);
if (itemView != null) {
int top = itemView.getTop();
PreferenceTool.putInt("position", firstVisiblePositionArray[0]);
PreferenceTool.putInt("top", top);
PreferenceTool.commit();
}
}
}
}
});
}
private void changeLayoutManager(int type) {
switch (type) {
case 0: {
LinearLayoutManager manager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(manager);
}
break;
case 1: {
GridLayoutManager manager = new GridLayoutManager(this, 3);
mRecyclerView.setLayoutManager(manager);
}
break;
case 2: {
StaggeredGridLayoutManager manager =
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(manager);
}
break;
}
}
/**
* 定位列表
*
* @param type
*/
private void location(int type) {
int top = PreferenceTool.getInt("top", 0);
int position = PreferenceTool.getInt("position", 0);
if (type == 0) {
((LinearLayoutManager) mRecyclerView.getLayoutManager())
.scrollToPositionWithOffset(position, top);
} else if (type == 1) {
((GridLayoutManager) mRecyclerView.getLayoutManager())
.scrollToPositionWithOffset(position, top);
} else if (type == 2) {
((StaggeredGridLayoutManager) mRecyclerView.getLayoutManager())
.scrollToPositionWithOffset(position, top);
}
}
}
如果您发现文中有写的不对的地方,欢迎评论区指正,如果您对我的文章有点兴趣,请不要吝啬您的手指点个赞,毕竟点赞又不收费不是吗O(∩_∩)O~~