2017-04-19 19 views
0

スクロールリスナーをRecyclerViewアダプタに実装して、自分のアクティビティクラスで使用できるようにする方法。あなたは私のリサイクラービューアダプターの下にあります。私はスクロールするときに位置を変更したいので、私はスクロールリスナーをしたい理由。今すぐリサイクルビュー内のアイテムをクリックするだけで変更できます。スクロールリスナーをRecyclerViewアダプタに追加する

は基本的に私がしたい私はアクティビティクラスでテキストビューを持つことになりますし、私は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) { 

     } 
    } 
+0

ポジションはどういう意味ですか? – Nobody

+0

たとえば、この行を見てください。 holder.name.setText(images.get(position).getName()); –

+0

基本的に何をしたいのですか?私はアクティビティクラスでテキストビューを表示し、recyclerViewをスクロールするとアイテム名を取得する必要があります –

答えて

0

の名前を取得する必要があります。 getFirstVisiblePosition()というメソッドがあります。このメソッドを使用すると、スクリーン上に表示されるリストの最初の項目を取得し、それに応じてテキストビューを設定できます。ここの例を見てくださいGet visible items in RecyclerView。希望があれば

関連する問題