Java 循环视图中的ViewPager

Java 循环视图中的ViewPager,java,android,android-viewpager,android-recyclerview,Java,Android,Android Viewpager,Android Recyclerview,在将其标记为副本之前,我尝试了所有的解决方案,但没有任何效果。请通读这个问题并帮助我 我有一个recycler视图,该视图将有多个ViewPager,两个都将单独滚动。我似乎无法实现这一点。我的代码如下 回收视图适配器 public class Adapter extends RecyclerView.Adapter<Adapter.holder> { private final LayoutInflater mLayoutInflater; privat

在将其标记为副本之前,我尝试了所有的解决方案,但没有任何效果。请通读这个问题并帮助我

我有一个recycler视图,该视图将有多个ViewPager,两个都将单独滚动。我似乎无法实现这一点。我的代码如下

回收视图适配器

    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);

    }
    }
公共类适配器扩展了RecyclerView.Adapter{
私人最终布局平面图mLayoutInflater;
私人最终语境;
私人最终回收商;
私有OnLoadMoreListener OnLoadMoreListener;
ArrayList mArray=新的ArrayList();
私有整数像素;
私有整数像素;
主活动上下文活动;
公共持有人mh;
公共适配器(上下文mContext、最终回收器视图r、主活动conts){
contextActivity=conts;
这个。r=r;
上下文=mContext;
mLayoutInflater=LayoutInflater.from(mContext);
r、 addOnScrollListener(新的RecyclerView.OnScrollListener(){
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
如果(!r.c)(1)){
onLoadMoreListener.onLoadMore();
}
}
});
}
公共无效塞特马里莱(ArrayList mArray){
this.mArray=mArray;
Log.e(“This->”,mArray.toString());
notifyItemRangeChanged(0,mArray.size());
}
@凌驾
公共持有者onCreateViewHolder(视图组视图组,int i){
视图v=MLAYOUTIONFLATER.充气(R.布局.回收器\自定义\照片,视图组,false);
mh=新适配器.支架(v);
返回mh;
}
public void setOnLoadMoreListener(OnLoadMoreListener OnLoadMoreListener){
this.onLoadMoreListener=onLoadMoreListener;
}
@凌驾
BindViewHolder上的公共无效(holder holder,int i){
ViewPagerAdapter mp=新的ViewPagerAdapter(上下文,mArray);
支架vp设置适配器(mp);
}
@凌驾
public int getItemCount(){
返回mArray.size();
}
类持有者扩展了RecyclerView.ViewHolder{
可视寻呼机;
公共持有人(查看项目视图){
超级(项目视图);
vp=(ViewPager)itemView.findViewById(R.id.pager);
}
}
}
查看寻呼机适配器

    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);

    }
    }
public类ViewPagerAdapter扩展了PagerAdapter{
ArrayList别名=null;
私有布尔tk=true;
上下文mcov;
公共ViewPager(上下文,ArrayList checkAlias){
mcov=上下文;
this.alias=checkAlias;
}
@凌驾
public int getCount(){
Log.e(“Sze”,别名.size()+);
返回alias.size();
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回视图==对象;
}
public void setNestedIndic(布尔标记)
{
tk=令牌;
}
公共位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//我们要扩展到的新尺寸
所需的最终int_尺寸=290;
//找到正确的刻度值。它应该是2的幂。
int标度=1;
而(o.outWidth/scale/2>=所需尺寸和&o.outHeight/scale/2>=所需尺寸)
比例*=2;
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(新文件输入流(f),null,o2);
}catch(filenotfounde异常){
}
返回null;
}
@凌驾
公共对象实例化项(视图组容器,int位置){
LayoutInflater mlAy=(LayoutInflater)mcov
.getSystemService(上下文布局\充气机\服务);
视图m=mlAy.充气(R.layout.custom_照片,容器,假);
ImageView mimg=(ImageView)m.findViewById(R.id.mainImageFeed);
TextView tc=(TextView)m.findViewById(R.id.texr);
String path=alias.get(position.getPath();
Log.e(“位置”,“位置+”);
tc.setText(位置+“”);
container.addView(m);
返回m;
}
@凌驾
公共项(视图组容器、int位置、对象){
//从viewpager中删除viewpager_item.xml
((android.support.v4.view.ViewPager)容器);
}
}
recycler\u custom\u 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>
      <?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>

自定义照片.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>
      <?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。希望这对将来的人有帮助

请将您的答案标记为“已接受”,这样它就不会出现在“未回答”列表中。您能确保在Recyclerview@Sarthak Mishra内完成实现Viewpager的代码吗