如何获取RecyclerView的单击数据位置以查看寻呼机滑动?

时间:2019-01-12 09:22:22

标签: android android-recyclerview android-viewpager

我正在将Viewpager用于滑块,最初是Recyclerview ...当我将图像从Server加载到Recycler时,它已经完成了...现在,当有人单击某个特定项目时,我想这样做,它应该在第二次打开我在其中使用ViewPager的活动。例如:当我单击第一个图像时,它应该在viewPager(slider)中打开该图像。...而且当有人在ViewPager的活动上滑动时,它应该加载更多图像。 代码在下面,不知道下一步该怎么做。请帮助我。

RecyclerView

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.myViewHolder> {

ArrayList<model> mdata;
Context context;
public  RecyclerViewAdapter(){ }

public RecyclerViewAdapter(ArrayList<model> mdata, Context context) {
    this.mdata = mdata;
    this.context = context;
}

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

    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
    View view = inflater.inflate(R.layout.item,viewGroup,false);
    return new myViewHolder(view);
}

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


    Picasso.get().load(mdata.get(holder.getAdapterPosition()).getWallpaper()).into(holder.wallpaper);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        Intent i = new Intent(context,SecondActivity.class);

            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("wallpaperUrl",mdata.get(holder.getAdapterPosition()).getWallpaper());

            context.startActivity(i);

        }
    });



}

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

public  class myViewHolder extends RecyclerView.ViewHolder{

    ImageView wallpaper;
    Button set;

    public myViewHolder(@NonNull View itemView) {
        super(itemView);

        wallpaper = itemView.findViewById(R.id.wallpaper);
        set = itemView.findViewById(R.id.set);
    }
}

具有ViewPager的第二个活动:

public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);


  btn = findViewById(R.id.setWallpaper);
    mdata = new ArrayList<model>();
 viewPager = findViewById(R.id.viewPager);

    Intent intent = getIntent();

    Integer[] colors_temp ={getResources().getColor(R.color.color1),
            getResources().getColor(R.color.color2),
            getResources().getColor(R.color.color3),
            getResources().getColor(R.color.color4),

    } ;

    colors = colors_temp;

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffSet, int pixels) {
            if (position < adapter.getCount() -1 &&  position <(colors.length) -1){

                viewPager.setBackgroundColor(
                        (Integer) argbEvaluator.evaluate
                                (positionOffSet,
                                        colors[position],
                                        colors[position + 1]));
            }else{
                viewPager.setBackgroundColor(colors[colors.length - 1]);
            }

        }

        @Override
        public void onPageSelected(final int i) {

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DownloadImage downloadImage = new DownloadImage();
                    Bitmap bitmap = null;

                    try{

                       bitmap = 
    downloadImage.execute(mdata.get(i).getWallpaper()).get();
                    }catch (Exception e){
                        e.printStackTrace();
                        Toast.makeText(SecondActivity.this, "Something went 
               Wrong! ", Toast.LENGTH_SHORT).show();
                    }

                }
            });

        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });

mDatabase = FirebaseDatabase.getInstance();
    mReference = mDatabase.getReference().child("wallpapers");

    mReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){

                model data = dataSnapshot1.getValue(model.class);
                mdata.add(data);

            }

            adapter = new Adapter(mdata,getApplicationContext());

            viewPager.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            Toast.makeText(SecondActivity.this, "Failed! "+databaseError, 
        Toast.LENGTH_SHORT).show();

        }
    });

  }

MyAdapter:

public class Adapter extends PagerAdapter {

ArrayList<model> mdata;
Context context;
LayoutInflater inflater;

public  Adapter(){

}

public Adapter(ArrayList<model> mdata, Context context) {
    this.mdata = mdata;
    this.context = context;
}

@Override
public int getCount() {
    return mdata.size();
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
    return view.equals(o);
}

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

    inflater = LayoutInflater.from(context);

    View view = inflater.inflate(R.layout.pager_item,container,false);

    ImageView wallpaper;
    Button btn;

    wallpaper = view.findViewById(R.id.wallpaperImage);

    Picasso.get().load(mdata.get(position).getWallpaper()).into(wallpaper);

    container.addView(view,0);

    return view;
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
   container.removeView((View)object);
}

2 个答案:

答案 0 :(得分:0)

在此处的输入代码中,将setOnclickListener添加到holser.iytemView,然后创建一个用于导航到第二个活动的意图,并将此代码添加到onBindViewHolder中:

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: LoadingProfileActivity");
            Intent intent = new Intent(getContext(), SecondActivity.class);
            /*
              Extras
            */
            mainActivityContext.startActivity(intent);
        }
    });

如果要发送此行中包含的图像或ID,请在创建意图后添加intent.putExtra()

例如,如果我想在第二个活动不适中显示图像

Intent intent = new Intent(getContext(), SecondActivity.class);
intent.putExtra("image_url", imageResource);

在SecondActivity中,首先从意图中获取图像资源

String imageResouce = getIntent.getStringExtra("image_url");

然后在图像视图中显示图像

答案 1 :(得分:0)

如果要从服务器加载图像,并且每个图像都有一个ID,则将ID传递到then intent中。当您打开需要单击图像的视图时,请进行服务调用,以使用其ID从服务器检索图像。