我已经通过将适配器数量设置为Integer.MAX.Now创建了一个循环回收视图,我需要突出显示图像中的中心回收站项目。请帮助我!获取Infinite RecyclerView的中心可见项目值
4
A
回答
1
By using center indicator(textview) in the layout and addOnScrollListner we can achieve this
please refer the following example
在XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:text="↓"
android:id="@+id/centerIndicator"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
android:textColor="@color/theme_yellow"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_marginTop="27dp"
android:background="@android:color/transparent"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/list"
android:clipToPadding="false"
android:divider="@android:color/transparent"
android:layout_height="wrap_content"/>
</RelativeLayout>
在活动/片段:
public class Sample extends Fragment {
RecyclerView listView;
ArrayList<String>mWeekDaysList=new ArrayList<>();
LinearLayoutManager mlinearLayoutManagerForDateList;
DateAdapter mDateAdapter;
TimeListAdapter mtimeAdapter;
private int mCenterPivot;
private boolean mAutoSet = true;
Activity mactivity;
public NigaichiNiralFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_nigaichi_niral, container, false);
mactivity=getActivity();
mWeekDaysList.add("Sunday");
mWeekDaysList.add("Monday");
mWeekDaysList.add("Tuesday");
mWeekDaysList.add("Wednesday");
mWeekDaysList.add("Thursday");
mWeekDaysList.add("Friday");
mWeekDaysList.add("Saturday");
listView = (RecyclerView) view.findViewById(R.id.list);
mlinearLayoutManagerForDateList = new LinearLayoutManager(mactivity);
mlinearLayoutManagerForDateList.setOrientation(LinearLayoutManager.HORIZONTAL);
listView.setLayoutManager(mlinearLayoutManagerForDateList);
final TextView mCenterIndicator = (TextView) view.findViewById(R.id.centerIndicator);
final int itemWidth = (int) getResources().getDimension(R.dimen.flexible_space_image_height) ;
mlinearLayoutManagerForDateList.scrollToPosition(Integer.MAX_VALUE/2);
mDateAdapter=new DateAdapter(mWeekDaysList);
listView.setAdapter(mDateAdapter);
mCenterIndicator.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int center = (mCenterIndicator.getLeft() + mCenterIndicator.getRight())/2 ;
int padding = center - itemWidth/2; //Assuming both left and right padding needed are the same
listView.setPadding(5,0,5,0);
mCenterPivot = center;
listView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
if(mCenterPivot == 0) {
// Default pivot , Its a bit inaccurate .
// Better pass the center pivot as your Center Indicator view's
// calculated center on it OnGlobalLayoutListener event
mCenterPivot = lm.getOrientation() == LinearLayoutManager.HORIZONTAL ? (recyclerView.getLeft() + recyclerView.getRight()) : (recyclerView.getTop() + recyclerView.getBottom());
}
if(!mAutoSet) {
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
//ScrollStoppped
View view = findCenterView(lm);//get the view nearest to center
//view.setBackgroundColor(Color.RED);
int position = recyclerView.getChildAdapterPosition(view) % mWeekDaysList.size();
Log.d("isideScroll",mWeekDaysList.get(position));
mDateAdapter.setSelecteditem(position);
int viewCenter = lm.getOrientation() == LinearLayoutManager.HORIZONTAL ? (view.getLeft() + view.getRight())/2 :(view.getTop() + view.getBottom())/2;
//compute scroll from center
int scrollNeeded = viewCenter - mCenterPivot; // Add or subtract any offsets you need here
if(lm.getOrientation() == LinearLayoutManager.HORIZONTAL) {
recyclerView.smoothScrollBy(scrollNeeded, 0);
}
else
{
recyclerView.smoothScrollBy(0, (int) (scrollNeeded));
}
mAutoSet =true;
}
}
if(newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING){
mAutoSet =false;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
});
return returnView;
}
private void scrollToCenter(View v) {
int itemToScroll = listView.getChildAdapterPosition(v);
int centerOfScreen = listView.getWidth()/2 - v.getWidth()/2;
//v.setBackgroundColor(Color.RED);
mlinearLayoutManagerForDateList.scrollToPositionWithOffset(itemToScroll, centerOfScreen);
}
private View findCenterView(LinearLayoutManager lm) {
int minDistance = 0;
View view = null;
View returnView = null;
boolean notFound = true;
for(int i = lm.findFirstVisibleItemPosition(); i <= lm.findLastVisibleItemPosition() && notFound ; i++) {
view=lm.findViewByPosition(i);
int center = lm.getOrientation() == LinearLayoutManager.HORIZONTAL ? (view.getLeft() + view.getRight())/ 2 : (view.getTop() + view.getBottom())/ 2;
int leastDifference = Math.abs(mCenterPivot - center);
if(leastDifference <= minDistance || i == lm.findFirstVisibleItemPosition())
{
minDistance = leastDifference;
returnView=view;
}
else
{
notFound=false;
}
}
return returnView;
}
}
适配器:
public class DateAdapter extends RecyclerView.Adapter<DateAdapter.ReviewHolder> {
ArrayList<String> mData;
private int selectedItem = -1;
int pos=0;
public DateAdapter(ArrayList<String> data){
mData=data;
}
@Override
public ReviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View v= LayoutInflater.from(context).inflate(R.layout.item_horz,parent,false);
return new DateAdapter.ReviewHolder(v);
}
@Override
public void onBindViewHolder(ReviewHolder holder, int position) {
pos=position;
position = position % mData.size();
holder.tvName.setText(mData.get(position));
holder.tvName.setGravity(Gravity.CENTER);
if (position == selectedItem) {
Log.d("CenterPosition", "center" + position);
holder.tvName.setTextColor(Color.RED);
holder.tvName.setTextSize(20);
holder.tvName.setBackgroundColor(Color.parseColor("#fccd00"));
} else {
holder.tvName.setTextColor(Color.WHITE);
holder.tvName.setTextSize(16);
holder.tvName.setBackgroundColor(Color.BLACK);
}
}
@Override
public int getItemCount() {
// return mData.size();
return Integer.MAX_VALUE;
}
public class ReviewHolder extends RecyclerView.ViewHolder {
protected TextView tvName;
View container;
public ReviewHolder(View itemView) {
super(itemView);
container=itemView;
tvName= (TextView) itemView.findViewById(R.id.text);
}
}
public void setSelecteditem(int selecteditem) {
Log.d("POSITION",String.valueOf(selecteditem));
this.selectedItem = selecteditem;
notifyDataSetChanged();
}
}
item_horz.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="151dp"
android:id="@+id/wrapper"
android:background="@color/white"
android:orientation="horizontal"
android:layout_height="50dp">
<LinearLayout
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@color/black">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:id="@+id/text"
android:textColor="@color/white"
android:text="21"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
希望这会帮助你们..
+0
哇!它为我的情况正确工作。谢谢很多! – Darsy
0
在BindView中,您可以检查适配器的中间位置,并为该特定支架应用图像的逻辑。
onBindViewHolder(View holder, int postion){
if(position == getItemCount()/2)
{ //Write image logic for holder
}}
+0
它不工作,因为适配器的大小是无限的..它会给IndexOutOfboundexception – Darsy
1
如果您想使用horizontalscrollview(动态创建项目)而不是recyclerview。
创建父布局
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</HorizontalScrollView>
膨胀您的孩子的物品父布局。
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < list.size(); i++) {
// inflate child element
final LinearLayout titleRow = (LinearLayout) inflater.inflate(R.layout.item_gallery, fragmentGalleryScrollLl, false);
final CircleImageView shapeImageView = (CircleImageView) titleRow.findViewById(R.id.item_gallery_iv);
shapeImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollItem(titleRow);
}
});
fragmentGalleryScrollLl.addView(titleRow);
}
当您单击项目时使用此方法。
private void scrollItem(LinearLayout titleRow) {
int scrollX = (titleRow.getLeft() - (fragmentGalleryScrollSv.getWidth()/2)) + (titleRow.getWidth()/2);
fragmentGalleryScrollSv.smoothScrollTo(scrollX, 0);
}
相关问题
- 1. 在RecyclerView中获取当前可见项目
- 2. 当前在RecyclerView中的可见项目
- 3. 如何获得Recyclerview中项目视图的可见性?
- 4. android java/kotlin recyclerview项目不可见
- 5. RecyclerView项目动画:只动画最初可见的项目
- 6. 如何在Android中获取Recyclerview项目的价值?
- 7. Android:不使用`getAdapterPosition()获取价值recyclerview项目的值`
- 8. Android - 检测RecyclerView中的最后一个项目是否可见
- 9. Recyclerview与中心项目装饰
- 10. 如何点击在RecyclerView的不可见ViewHolder项目后面可见的视图?
- 11. 如何获取jCarouselLite中的可见项?
- 12. 从RecyclerView获取缓存项目
- 13. 更换RecyclerView项目时获取IndexOutOfBoundsException
- 14. 计算适配器内的RecyclerView项目可见性百分比
- 15. RecyclerView问题与儿童项目的可见性
- 16. Android RecyclerView - 当键盘打开时,保持最后一个可见项目可见
- 17. 确定项目是否在RecyclerView上可见
- 18. RecyclerView先保留第一个可见项
- 19. WP7在列表框中获取可见项目
- 20. NativeScript ListView。获取第一个可见项目的位置iOS
- 21. Android ListView:获取可见项目的数据索引
- 22. 网格视图获取用户可见的项目
- 23. ArrayList中的RecyclerView项目
- 24. Android RecyclerView获取子项
- 25. 获取RecyclerView在espresso中的总项目数
- 26. ObjectListView项目不可见?
- 27. ListBox中获取特定项目的值
- 28. 获取arrayadapter中的项目值
- 29. Android的 - 在它选择的意见选择recyclerview项目
- 30. 删除RecyclerView中的项目
有趣的概念,刻意突出的东西无限的中心。 ;) 我认为你可以尝试获取第一个和最后一个可见项目并从中推断出中心。请参阅:https://stackoverflow.com/a/25053500/1827254 – Eselfar
这可以是你的答案。见链接.https://stackoverflow.com/a/39191696/6155031 – 6155031