下面列出了android.support.v7.widget.LinearLayoutManager#scrollToPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint("CheckResult")
public DialogHolder(View itemView) {
super(itemView);
initUi(itemView);
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.scrollToPosition(0);
filterRecycler.setLayoutManager(layoutManager);
filterRecycler.setHasFixedSize(true);
filterRecycler.setItemAnimator(new DefaultItemAnimator());
RxTextView.textChanges(searchEdt)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(charSequence -> {
getAdapter().filter(charSequence.toString());
});
}
private void moveToPosition(int position) {
if (position > data.size())
return;
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int first = layoutManager.findFirstVisibleItemPosition();
int end = layoutManager.findLastVisibleItemPosition();
if (first == -1 || end == -1)
return;
if (position <= first) {
layoutManager.scrollToPosition(position);
} else if (position >= end) {
isMove = true;
scrollPosition = position;
layoutManager.smoothScrollToPosition(recyclerView, null, position);
} else {//中间部分
int n = position - layoutManager.findFirstVisibleItemPosition();
if (n > 0 && n < data.size()) {
int top = layoutManager.findViewByPosition(position).getTop();
recyclerView.scrollBy(0, top);
}
}
}
public void collapseChildrenComment(int position) {
ArrayList<Comment> childrenComments = new ArrayList<>();
Comment parentComment = (Comment) mItemList.get(position);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
// if a comment header is not visible when collapsing, scroll to it's header
if (position != linearLayoutManager.findFirstCompletelyVisibleItemPosition()) {
linearLayoutManager.scrollToPosition(position);
}
for (int curPosition = position + 1;
(mItemList.get(curPosition) instanceof Comment
&& ((Comment) mItemList.get(curPosition)).getLevel() > parentComment.getLevel());
curPosition++) {
childrenComments.add((Comment) mItemList.get(curPosition));
}
if (!childrenComments.isEmpty()) {
mCollapsedChildrenCommentsIndex.put(parentComment.getCommentId(), childrenComments);
for (Comment comment : childrenComments) {
mItemList.remove(comment);
}
notifyItemChanged(position);
notifyItemRangeRemoved(position + 1, childrenComments.size());
}
}
private void initHorizontalList() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
layoutManager.scrollToPosition(0);
thumbListView.setLayoutManager(layoutManager);
thumbListView.setHasFixedSize(true);
bindDataToAdapter();
}
private void initHorizontalList() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
layoutManager.scrollToPosition(0);
thumbListView.setLayoutManager(layoutManager);
thumbListView.setHasFixedSize(true);
bindDataToAdapter();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blog_transition);
initViews();
// Setup layout manager for mBlogList and column count
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// Control orientation of the mBlogList
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.scrollToPosition(0);
// Attach layout manager
mRecyclerView.setLayoutManager(layoutManager);
// Bind adapter to recycler
mBlogList = new ArrayList<>();
// Some of colors
int[] color = {0xFFC2185B, 0xFFB3E5FC, 0xFFFFEB3B, 0xFFF8BBD0, 0xFFFF5252,
0xFFE91E63, 0xFF448AFF, 0xFF00796B, 0xFFE91E63, 0xFFFF5252, 0xFFF8BBD0, 0xFF0288D1,};
// Some of images
int[] drawable = {R.drawable.apple_46, R.drawable.apple_style, R.drawable.steve_jobs,
R.drawable.apple_46, R.drawable.apple_style, R.drawable.steve_jobs, R.drawable.apple_46,
R.drawable.apple_style, R.drawable.steve_jobs, R.drawable.apple_46, R.drawable.apple_style,
R.drawable.steve_jobs};
//
for (int i = 0; i < color.length; i++) {
Blog blog = new Blog();
blog.setImageRes(drawable[i]);
blog.setBackGroundColor(color[i]);
blog.setTitle("Label " + i);
blog.setSubTitle("This is subTitle with number " + i + " and some description here");
mBlogList.add(blog);
}
// Setting adapter
final BlogRecyclerAdapter adapter = new BlogRecyclerAdapter(mBlogList);
mRecyclerView.setAdapter(adapter);
// Listen to the item touching
mRecyclerView
.addOnItemTouchListener(new RecyclerItemClickListener(
this,
new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
if (!isOpen) {
BlogTransitionActivity.this.position = position;
openBlogInDetails(itemView);
}
}
}));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view_sample);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
String[] mDataset = new String[4];
for (int i = 0; i < 4; i++) {
mDataset[i] = i + "hh";
}
mAdapter = new MyAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.scrollToPosition(0);
mRecyclerView.setLayoutManager(layoutManager);
// RecyclerView.ItemDecoration itemDecoration =
// new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
// mRecyclerView.addItemDecoration(itemDecoration);
// this is the default;
// this call is actually only necessary with custom ItemAnimators
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// onClickDetection is done in this Activity's OnItemTouchListener
// with the help of a GestureDetector;
// Tip by Ian Lake on G+ in a comment to this post:
// https://plus.google.com/+LucasRocha/posts/37U8GWtYxDE
// mRecyclerView.addOnItemTouchListener(this);
// gesturedetector =
// new GestureDetectorCompat(this, new RecyclerViewDemoOnGestureListener());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view_sample);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
String[] mDataset = new String[4];
for (int i = 0; i < 4; i++) {
mDataset[i] = i + "hh";
}
mAdapter = new MyAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.scrollToPosition(0);
mRecyclerView.setLayoutManager(layoutManager);
// RecyclerView.ItemDecoration itemDecoration =
// new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
// mRecyclerView.addItemDecoration(itemDecoration);
// this is the default;
// this call is actually only necessary with custom ItemAnimators
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// onClickDetection is done in this Activity's OnItemTouchListener
// with the help of a GestureDetector;
// Tip by Ian Lake on G+ in a comment to this post:
// https://plus.google.com/+LucasRocha/posts/37U8GWtYxDE
// mRecyclerView.addOnItemTouchListener(this);
// gesturedetector =
// new GestureDetectorCompat(this, new RecyclerViewDemoOnGestureListener());
}