Recyclerview无法控制视图

时间:2016-11-29 04:03:58

标签: android android-recyclerview linearlayoutmanager

我正在使用RecyclerView LinearLayoutManager横向。我每个视图显示3个项目并使用无限滚动。我的问题是它们是3张图像,我希望中间图像放大2倍,但我无法控制控制器来解决这个问题,有没有办法做到这一点?

ex)[] [] [] =一个视图,但中间的一个是大小的2倍,用户可以左右滚动

代码段: 以下是我将活动初始化为适配器的方法:

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

        }
    });

适配器:

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

2 个答案:

答案 0 :(得分:1)

试试这个......

首先在Recycler适配器中声明常量

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

获取Recyler项目的位置

   @Override
    public int getItemViewType(int position) {
        final int type = position % 3;

        switch (type) {
            case 1:
                return TYPE_2X;
        }
        return TYPE_1X;
    }

获取viewtype并基于视图类型,您可以调整大小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);
}

答案 1 :(得分:0)

ok @Kiran Dsouza你的答案是正确的我必须改变相对布局参数并且它适用于第一个初始视图,但是当我向水平和向后滚动时它不再起作用,因为它返回到其原始视图。