我想自动滚动一个水平滚动视图,其中包含一个linearlayout内的很多imageview 当它到达最后一个imageview时,第一个出现,依此类推
我发现this code符合我的需要但是当我添加了自动滚动时,圆形功能无法工作:当到达最后一个图像视图时,第一个图像视图不会出现< / p>
这是我的代码:
slider = (RelativeLayout)findViewById(R.id.slider);
RelativeLayout container = (RelativeLayout) findViewById(R.id.slider);
scrollView = new PSInfiniteScrollView(this,new PSSize(120,120));
for (int i = 0; i < 10; i++) {
MyCloneableView img = new MyCloneableView(this);
img.setId(i + 20);
img.setImageResource(R.drawable.ic_launcher);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setBackgroundColor(c[i]);
img.setTag(c[i]);
scrollView.addItem(img);
}
container.addView(scrollView);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
doLoop();
}
});
t.start();
这里是doLoop方法:
private void doLoop(){
do {
scrollView.scrollBy(2, 0);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
scrollView.scrollBy(2, 0);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
}
答案 0 :(得分:15)
通过使用带水平线性布局管理器的RecyclerView,我实现了类似的要求 这里是可以帮助您的代码片段
要使Recycler View显示为水平,请使用LinearLayout管理器并将其方向设置为水平
mLinearLayoutManager = new LinearLayoutManager(context);
mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
使其成为循环:在您的Recycler View适配器中,使getItemCount返回一个非常大的值,如Integer.MAX_VALUE
。
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
要获得适配器中的实际项目,您可以使用像这样的简单模数操作
@Override
public void onBindViewHolder(ActionItemViewHolder holder, int position) {
position = position % ACTUAL_SIZE_OF_LIST;
};
这是让它循环的技巧!
让您的RecyclerView滚动到索引,该索引是适配器getItemCount返回的值的一半,您可以通过以下代码实现此目的:
mLinearLayoutManager.scrollToPosition(Integer.MAX_VALUE / 2);
(这是一个简单的黑客攻击)
我不明白你的自动滚动究竟是什么意思,如果你希望这些项目在点击它时滚动到屏幕的中心就是代码
private void scrollToCenter(View v) {
int itemToScroll = mRecyclerView.getChildPosition(v);
int centerOfScreen = mRecyclerView.getWidth() / 2 - v.getWidth() / 2;
mLayoutManager.scrollToPositionWithOffset(itemToScroll, centerOfScreen);
}
希望这能帮到你!
答案 1 :(得分:0)
在这里,我使用自动滚动
制作了Recyclerviewpublic class AdsViewer extends FrameLayout {
private static final String TAG = "AdsViewer";
private final long SWITCH_TIME = 5000;
@BindView(R.id.ads_recycler_view)
RecyclerView adsRecyclerView;
private AdsAdapter adsAdapter;
private int currentItem = 0;
private Handler switchHandler = new Handler();
private Runnable switchRunnable = new Runnable() {
@Override
public void run() {
showNextAd();
}
};
public AdsViewer(@NonNull Context context) {
super(context);
init();
}
public AdsViewer(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
inflate(getContext(), R.layout.ads_viewer, this);
ButterKnife.bind(this);
initRecyclerView();
}
private void initRecyclerView() {
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
adsRecyclerView.setLayoutManager(linearLayoutManager);
adsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
currentItem = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
resetTimer();
}
});
// add pager behavior, Add this to have paging scrolling
//PagerSnapHelper snapHelper = new PagerSnapHelper();
//snapHelper.attachToRecyclerView(adsRecyclerView);
}
private void resetTimer() {
switchHandler.removeCallbacks(switchRunnable);
switchHandler.postDelayed(switchRunnable, SWITCH_TIME);
}
public void clear() {
if (adsAdapter != null) {
adsAdapter.clear();
}
}
public void setAds(List<Ads> ads) {
if (adsAdapter == null || ads.size() != adsAdapter.getItemCount()) {
initializeAdapter();
adsAdapter.addItems(ads);
resetTimer();
}
}
private void initializeAdapter() {
adsAdapter = new AdsAdapter(LayoutInflater.from(getContext()));
adsRecyclerView.setAdapter(adsAdapter);
}
private void showNextAd() {
int adsCount = adsAdapter.getItemCount();
if (adsCount > 1) {
if (currentItem == (adsCount - 1)) {
currentItem = 0;
adsRecyclerView.scrollToPosition(currentItem);
resetTimer();
} else {
currentItem++;
adsRecyclerView.smoothScrollToPosition(currentItem);
}
}
}
}