这里使用的是RecyclerView不是listview,原理一样
recyclerview.setOnItemClickListener(new FM_FenLei_LeftAdapter.OnItemClickListener() {
private int childCount = 0;
private int middlechild = 0;
@Override
public void ItemClick(View view, int position) {
//得到布局
RecyclerView.LayoutManager manager = left_recyclerView.getLayoutManager();
//竖排类型,所以强转LinearLayoutManager,如果是ListView就不需要强转
LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
//得到屏幕可见的item的总数
childCount = layoutManager.getChildCount();
if (childCount != left_list.size()) {
//可见item的总数除以2 就可以拿到中间位置
middlechild = childCount / 2;
}
//判断你点的是中间位置的上面还是中间的下面位置
//RecyclerView必须加 && position != 2,listview不需要
if (position <= (layoutManager.findFirstVisibleItemPosition() + middlechild) && position != 2) {
left_recyclerView.smoothScrollToPosition(position + 1 - middlechild);
} else {
left_recyclerView.smoothScrollToPosition(position - 1 + middlechild);
}
}
});