Search code examples
androidandroid-recyclerviewlinearlayoutmanager

Recyclerview not able to control view


I am using a RecyclerView with LinearLayoutManager thats horizontal. I am displaying 3 items per view and using endless scrolling. My issue is that they're 3 images and I want the middle image to be enlarged by 2X but I can't get control of the controller to figure that out, is there way to do this?

ex) [] [] [] = one view but the middle one would be 2X the size and user can scroll left and right

Code Snippet: Here is how I initialize in activity to adapter:

LinearLayoutManager layout
            = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);

    // constant data
    constants = new Constants();
    recyclerView = (RecyclerView) findViewById(R.id.ModelCarousal);
    recyclerView.setLayoutManager(layout);
    recyclerView.setAdapter(new Model_Controller(getApplicationContext(), constants.Constants()));
    recyclerView.addOnScrollListener(new EndlessScrollListener(layout) {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            // Triggered only when new data needs to be appended to the list

        }
    });

Adapter:

  public class Model_Controller extends   RecyclerView.Adapter<Model_Controller.MyViewHolder> {
 Context mContext;
 List<Model> models;
 int[] img = new int[]{R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item
             ,R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item
                ,R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item,R.drawable.ic_item};

// view holder of badges
public class MyViewHolder extends RecyclerView.ViewHolder {
    public ImageView viewTwo;

    public MyViewHolder(View view) {
        super(view);
        viewTwo = (ImageView) view.findViewById(R.id.model_child);

    }
}

// constructor
public Badge_Controller(Context c, List<Model> modelSet){
    mContext = c;
    models = modelSet;
}

int height;
int width;
// returning the set of the holder for the child layout of badge view
@Override
public Model_Controller.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.model_child, parent, false);
    height = parent.getMeasuredHeight() * 4;
    width = parent.getWidth()*2;
    return new MyViewHolder(itemView);
}

// what the user is seeing
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
        Helpers helpers = new Helpers();
        holder.viewTwo.setImageDrawable(mContext.getDrawable(img[position]));
    }
}

// size
@Override
public int getItemCount() {
    return models.size()+2;
}
@Override
public int getItemViewType(int position) {
    if (position == 0 || position == getItemCount()-1) {
        return 1;
    }
    return 2;
}
}

Solution

  • Try this ....

    first declare constants in your Recycler adaptor

    private static final int TYPE_1X = 0;
    private static final int TYPE_2X = 1;
    

    Get the position of Recyler item

       @Override
        public int getItemViewType(int position) {
            final int type = position % 3;
    
            switch (type) {
                case 1:
                    return TYPE_2X;
            }
            return TYPE_1X;
        }
    

    Get the viewtype and base on view type you can resize 2X.

    @Override
    public Model_Controller.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
    final int type = viewType;
    View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.model_child, parent, false);
      switch (type) {
                    case TYPE_1X:
                        height = parent.getMeasuredHeight() / 2;
                        width = parent.getWidth()/2;
                        break;
                    case TYPE_2X:
                         height = parent.getMeasuredHeight() ;
                        width = parent.getWidth();
                        break;
    
                }
         RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
               width, //width
               height);//height
       itemView.setLayoutParams(lp);
        return new MyViewHolder(itemView);
    }