Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取Infinite RecyclerView的中心可见项值_Android - Fatal编程技术网

Android 获取Infinite RecyclerView的中心可见项值

Android 获取Infinite RecyclerView的中心可见项值,android,Android,我已经创建了一个循环回收视图,通过将适配器计数为Integer.MAX。现在,我需要突出显示图像中的中心回收器项目。请帮助我 在BindView中,您可以检查适配器的中间位置,并为特定支架的图像应用逻辑 onBindViewHolder(View holder, int postion){ if(position == getItemCount() / 2) { //Write image logic for holder }} 如果要使用horizontalscrollview(动态创建

我已经创建了一个循环回收视图,通过将适配器计数为Integer.MAX。现在,我需要突出显示图像中的中心回收器项目。请帮助我

在BindView中,您可以检查适配器的中间位置,并为特定支架的图像应用逻辑

 onBindViewHolder(View holder, int postion){
if(position == getItemCount() / 2) 
{ //Write image logic for holder
}}

如果要使用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>
在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;
}
}
公共类示例扩展了片段{
回收视图列表视图;
ArrayListWeekdaysList=新建ArrayList();
直线布局经理MLINEARLAYOUT MANAGERFORDATELIST;
日期适配器mDateAdapter;
TimeListAdapter mtimeAdapter;
私人互联网中心;
私有布尔mAutoSet=true;
活动能力;
公共NigaichiNiralFrag(){
//必需的空公共构造函数
}
@凌驾
创建视图(最终布局)上的公共视图充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u nigaichi\u niral,容器,假);
mactivity=getActivity();
mWeekDaysList.add(“星期日”);
mWeekDaysList.add(“星期一”);
mWeekDaysList.add(“星期二”);
mWeekDaysList.add(“星期三”);
mWeekDaysList.add(“星期四”);
mWeekDaysList.add(“星期五”);
mWeekDaysList.add(“星期六”);
listView=(RecycleService)view.FindView wByd(R.id.list);
mlinearLayoutManagerForDateList=新的线性布局管理器(mactivity);
mlinearLayoutManagerForDateList.setOrientation(LinearLayoutManager.HORIZONTAL);
setLayoutManager(mlinearLayoutManagerForDateList);
最终TextView mCenterIndicator=(TextView)view.findViewById(R.id.centerIndicator);
final int itemWidth=(int)getResources().getDimension(R.dimen.flexible\u space\u image\u height);
mlinearLayoutManagerForDateList.scrollToPosition(Integer.MAX_值/2);
mDateAdapter=newdateadapter(mWeekDaysList);
setAdapter(mDateAdapter);
mCenterIndicator.getViewTreeObserver().addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener()){
@凌驾
公共图书馆{
int center=(mCenterIndicator.getLeft()+mCenterIndicator.getRight())/2;
int padding=center-itemWidth/2;//假设所需的左、右填充相同
setPadding(5,0,5,0);
mcenterpoint=中心;
listView.addOnScrollListener(新的RecyclerView.OnScrollListener(){
@凌驾
CrollStateChanged上的公共无效(RecyclerView RecyclerView,int newState){
super.onScrollStateChanged(recyclerView、newState);
LinearLayoutManager lm=(LinearLayoutManager)recyclerView.getLayoutManager();
如果(mCenterPivot==0){
//默认轴,有点不准确。
//最好将中心轴作为中心指示器视图的
//LobalLayoutListener事件上的计算中心
mcenterpoint=lm.getOrientation()==LinearLayoutManager.HORIZONTAL?(recyclerView.getLeft()+recyclerView.getRight()):(recyclerView.getTop()+recyclerView.getBottom());
}
if(!mAutoSet){
if(newState==RecyclerView.SCROLL\u STATE\u IDLE){
//滚动停止
视图=FindCenter视图(lm);//获取最靠近中心的视图
//视图.setBackgroundColor(颜色.红色);
int position=recyclerView.getChildAdapterPosition(视图)%mWeekDaysList.size();
Log.d(“isideScroll”,mWeekDaysList.get(position));
mDateAdapter.setSelecteditem(位置);
int viewCenter=lm.getOrientation()==LinearLayoutManager.HORIZONTAL?(view.getLeft()+view.getRight())/2:(view.getTop()+view.getBottom())/2;
//从中心开始计算滚动
int scrollNeeded=viewCenter-mcenterpoint;//在此处添加或减去所需的任何偏移量
if(lm.getOrientation()==LinearLayoutManager.HORIZONTAL){
recyclerView.smoothScrollBy(ScrollRequired,0);
}
其他的
{
smoothScrollBy(0,(int)(ScrollRequired));
}
mAutoSet=true;
}
}
if(newState==RecyclerView.SCROLL_STATE_拖动| | newState==RecyclerView.SCROLL_STATE_结算){
mAutoSet=false;
}
}
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
}
});
}
});
返回视图;
}
私有无效滚动中心(视图v){
int itemToScroll=listView.getChildAdapterPosition(v);
int centerOfScreen=listView.getWidth()/2-v.getWidth()/2;
//v、 setBackgroundColor(颜色:红色);
mlinearLayoutManagerForDateList.scrollToPositionWithOffset(itemToScroll,屏幕中心);
}
私有视图FindCenter视图(LinearLayoutManager lm){
智力距离=0;
By using center indicator(textview) in the layout and addOnScrollListner we can achieve this
please refer the following example
<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();
    }

}
<?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>