java - recyclerview 中的 ViewPager

标签 java android android-viewpager android-recyclerview

在将其标记为重复项之前,我已经尝试了所有解决方案,但均无济于事。请完成这个问题并帮助我。

我有一个回收器 View ,它将有多个 viewPagers,并且它们都将单独滚动。我似乎无法实现这一点。我的代码如下

RecyclerView 适配器

    public class Adapter extends RecyclerView.Adapter<Adapter.holder> {


    private final LayoutInflater mLayoutInflater;
    private final Context context;
    private final RecyclerView r;

    private OnLoadMoreListener onLoadMoreListener;

    ArrayList<ceo> mArray = new ArrayList<>();
    private int heightPixels;
    private int widthPixels;
    MainActivity contextActivity;
    public holder mh;


    public Adapter(Context mContext, final RecyclerView r,MainActivity conts) {

        contextActivity=conts;



        this.r=r;
        context = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        r.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!r.canScrollVertically(1)) {
                    onLoadMoreListener.onLoadMore();
                }
            }
        });
    }

    public void setmArray(ArrayList<ceo> mArray) {
        this.mArray = mArray;

        Log.e("This->", mArray.toString());
        notifyItemRangeChanged(0, mArray.size());

    }

    @Override
    public holder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View v = mLayoutInflater.inflate(R.layout.recycler_custom_photo,viewGroup, false);
        mh = new Adapter.holder(v);
        return mh;
    }
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }





    @Override
    public void onBindViewHolder(holder holder, int i) {

        ViewPagerAdapter mp = new ViewPagerAdapter(context,mArray);
        holder.vp.setAdapter(mp);


     }





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

    class holder extends RecyclerView.ViewHolder {



        ViewPager vp;
        public holder(View itemView) {
            super(itemView);
           vp=(ViewPager)itemView.findViewById(R.id.pager);

        }
         }
           }

查看寻呼机适配器

    public class ViewPagerAdapter extends PagerAdapter {

    ArrayList<ceo> alias=null;
    private boolean tk=true;
    Context mcov;

    public ViewPager(Context context,ArrayList<ceo> checkAlias) {

        mcov=context;
        this.alias=checkAlias;
    }

    @Override
    public int getCount() {

        Log.e("Sze",alias.size()+"");
        return alias.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view ==  object;
    }

    public void setNestedIndic(boolean token)
    {
      tk=token;
    }
    public Bitmap decodeFile(File f) {

        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            //The new size we want to scale to
            final int REQUIRED_SIZE = 290;

            //Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {


        LayoutInflater mlAy = (LayoutInflater) mcov
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View m=mlAy.inflate(R.layout.custom_photo, container, false);
        ImageView mimg=(ImageView)m.findViewById(R.id.mainImageFeed);
        TextView tc=(TextView)m.findViewById(R.id.texr);
            String path=alias.get(position).getPath();


        Log.e("Pos",""+position);
        tc.setText(position+" ");

      container.addView(m);
       return  m;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((android.support.v4.view.ViewPager) container).removeView((RelativeLayout) object);

    }
    }

recycler_custom_photo.xml

    <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" >
    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

     </RelativeLayout>

custom_photo.xml

      <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:background="#313131"
    android:id="@+id/taken"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/old"
        android:id="@+id/mainImageFeed"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
       />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/texr"
        android:textSize="40dp"
        />

   </RelativeLayout>

我需要进行哪些更改才能使这些工作正常进行?谢谢您的回答!

最佳答案

代码很好。我错过的是在 recyclerview 中为 viewpager 添加了一个特定的高度。此外,我在 PagerAdapter 的构造函数中实例化了 LayoutInflater 而不是实例化 Item。希望这对以后的人有帮助!

关于java - recyclerview 中的 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41028303/

相关文章:

android - 在 ViewPager 中引用 EditText 会产生空指针异常

java - Scala:如何概括不相关的 Java 类的行为

java - 启动 jboss 服务器时未构建 Maven 依赖项

android - BR无法解决

android - Facebook 聊天头是如何实现的?

java - Android Jetpack 导航组件太慢。有什么问题?

java - 无法返回 href (jSoup) 中的文本

java - for循环中的控制流逻辑

android - Abi 过滤器 - 被一个或多个具有更高版本代码的 APK 遮蔽

java - ViewPager PagerAdapter 不更新 View