Android 将滚动侦听器添加到RecyclerView适配器

Android 将滚动侦听器添加到RecyclerView适配器,android,Android,如何实现scroll listener to RecyclerView适配器,以便我可以在活动类上使用。您可以在下面找到我的回收器视图适配器。之所以需要滚动侦听器,是因为我希望在滚动时更改位置。现在,只有单击“回收”视图中的项目,才能更改它 基本上,我想做的是在activity类中有一个文本视图,当我在recyclerView中滚动时,我应该得到项目的名称 Activity activity; ArrayList<BookData> images = new ArrayLi

如何实现scroll listener to RecyclerView适配器,以便我可以在活动类上使用。您可以在下面找到我的回收器视图适配器。之所以需要滚动侦听器,是因为我希望在滚动时更改位置。现在,只有单击“回收”视图中的项目,才能更改它


基本上,我想做的是在activity类中有一个文本视图,当我在recyclerView中滚动时,我应该得到项目的名称

 Activity activity;
    ArrayList<BookData> images = new ArrayList<>();
    AlbumDBHandler db;
    private Context mContext;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView thumbnail;
        public TextView name;
        public Button button;

        public MyViewHolder(View view) {
            super(view);

            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
            name = (TextView) view.findViewById(R.id.textViewGallery);
            button = (Button) view.findViewById(R.id.main_activity_button_carasoul);
        }
    }
    public GalleryAdapter(Context context, ArrayList<BookData> images) {
        mContext = context;
        this.images = images;
        this.db = new AlbumDBHandler(context);
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.gallery_thumbnail, parent, false);

        return new MyViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        BookData image = images.get(position);

        if("".equals(images.get(position).getImageLocalPath())){

            Glide.with(mContext)
                    .load(images.get(position).getImagePath_2())
                    .thumbnail(0.5f)
                    .crossFade()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.thumbnail);

            new ImageDownloaderTask(images.get(position)).execute(images.get(position).getImagePath_2());

        }else{
            Glide.with(mContext).load(new File(images.get(position).getImageLocalPath())).into(holder.thumbnail);
        }

        holder.name.setText(images.get(position).getName());
        }

    class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<BookData> bookDataWeakReference;

        public ImageDownloaderTask(BookData bookData) {
            bookDataWeakReference = new WeakReference<BookData>(bookData);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            return downloadBitmap(params[0]);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (bookDataWeakReference != null && bitmap != null) {
                BookData bookData = bookDataWeakReference.get();
                if (bookData != null){

                    if (activity instanceof mainActivityCarasoul){
                        String path = FileUtils.saveBitmapToCamera(activity,bitmap,bookData.getName()+".jpg", BookData.Book).getPath();
                        bookData.setImageLocalPath(path);
                        db.updateABook(bookData);
                    }
                }
            }
        }
        private Bitmap downloadBitmap(String url) {
            HttpURLConnection urlConnection = null;
            try {
                URL uri = new URL(url);
                urlConnection = (HttpURLConnection) uri.openConnection();
                int statusCode = urlConnection.getResponseCode();
                if (statusCode != HttpURLConnection.HTTP_OK) {
                    return null;
                }

                InputStream inputStream = urlConnection.getInputStream();
                if (inputStream != null) {
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                }
            } catch (Exception e) {
                urlConnection.disconnect();
                Log.w("ImageDownloader", "Error downloading image from " + url);
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
            return null;
        }
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    public interface ClickListener {
        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }

    public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private GalleryAdapter.ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final GalleryAdapter.ClickListener clickListener) {
            this.clickListener = clickListener;

            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                }
            });
        }
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }
        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    }
活动;
ArrayList images=新的ArrayList();
数据库;
私有上下文;
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
公共图像视图缩略图;
公共文本视图名称;
公共按钮;
公共MyViewHolder(视图){
超级(视图);
缩略图=(ImageView)view.findViewById(R.id.缩略图);
name=(TextView)view.findViewById(R.id.textViewGallery);
button=(button)view.findViewById(R.id.main\u activity\u button\u);
}
}
公共GalleryAdapter(上下文、ArrayList图像){
mContext=上下文;
这个。图像=图像;
this.db=新的AlbumDBHandler(上下文);
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutInflater.from(parent.getContext())
.充气(R.layout.gallery_缩略图,父级,假);
返回新的MyViewHolder(itemView);
}
@凌驾
公共无效onBindViewHolder(最终MyViewHolder,内部位置){
BookData image=images.get(位置);
if(“.”.equals(images.get(position.getImageLocalPath())){
使用(mContext)滑动
.load(images.get(position.getImagePath_2())
.缩略图(0.5f)
.crossFade()
.diskCacheStrategy(diskCacheStrategy.ALL)
.插入(支架.缩略图);
新建ImageDownloaderTask(images.get(position)).execute(images.get(position.getImagePath_2());
}否则{
Glide.with(mContext).load(新文件(images.get(position.getImageLocalPath())).into(holder.缩略图);
}
holder.name.setText(images.get(position.getName());
}
类ImageDownloaderTask扩展异步任务{
私有最终WeakReference bookDataWeakReference;
公共图像下载任务(BookData BookData){
bookDataWeakReference=新的WeakReference(bookData);
}
@凌驾
受保护位图doInBackground(字符串…参数){
返回下载位图(参数[0]);
}
@凌驾
受保护的void onPostExecute(位图){
如果(isCancelled()){
位图=空;
}
if(bookDataWeakReference!=null&&bitmap!=null){
BookData BookData=bookDataWeakReference.get();
如果(bookData!=null){
if(mainActivityCarasoul的活动实例){
String path=FileUtils.saveBitmapToCamera(活动,位图,bookData.getName()+“.jpg”,bookData.Book).getPath();
bookData.setImageLocalPath(路径);
db.UpdateBook(bookData);
}
}
}
}
私有位图下载位图(字符串url){
HttpURLConnection-urlConnection=null;
试一试{
URL uri=新的URL(URL);
urlConnection=(HttpURLConnection)uri.openConnection();
int statusCode=urlConnection.getResponseCode();
if(statusCode!=HttpURLConnection.HTTP\u确定){
返回null;
}
InputStream InputStream=urlConnection.getInputStream();
如果(inputStream!=null){
位图位图=位图工厂.decodeStream(inputStream);
返回位图;
}
}捕获(例外e){
urlConnection.disconnect();
Log.w(“ImageDownloader”,“从“+url”下载图像时出错);
}最后{
if(urlConnection!=null){
urlConnection.disconnect();
}
}
返回null;
}
}
@凌驾
public int getItemCount(){
返回图像。size();
}
公共界面ClickListener{
void onClick(视图,int位置);
仅长按无效(视图,int位置);
}
公共静态类RecyclerTouchListener实现RecyclerView.OnItemTouchListener{
私人手势检测器;
private GalleryAdapter.ClickListener ClickListener;
公共RecyclerTouchListener(上下文上下文、最终RecyclerView RecyclerView、最终GalleryAdapter.ClickListener ClickListener){
this.clickListener=clickListener;
gestureDetector=new gestureDetector(上下文,new gestureDetector.SimpleOnGestureListener()){
@凌驾
公共布尔onSingleTapUp(运动事件e){
返回true;
}
@凌驾
公开无效在线新闻(运动事件e){
View child=recyclerView.findChildViewUnder(e.getX(),e.getY());
if(child!=null&&clickListener!=null){
clickListener.onLongClick(child,recyclerView.getChildPosition(child));
}
}
});
}
@凌驾
公共布尔值onInterceptTouchEvent(RecyclerView rv,MotionEvent e){
View child=rv.findChildViewUnder(e.getX(),e.getY());
if(child!=null&&clickListener!=null&&gestureDetector.onTouchEvent(e)){
单击Listener.onClick(子对象,rv.getC