Recyclerview 无法控制视图
Recyclerview not able to control view
我正在使用 RecyclerView
,LinearLayoutManager
是水平的。我在每个视图中显示 3 个项目并使用无休止的滚动。我的问题是它们是 3 张图片,我希望中间的图片放大 2 倍,但我无法控制控制器来解决这个问题,有没有办法做到这一点?
ex) [] [] [] = 一个视图,但中间的视图大小为 2 倍,用户可以左右滚动
代码片段:
这是我在 activity 中初始化到适配器的方式:
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;
}
}
试试这个....
首先在 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;
}
获取视图类型并根据视图类型调整 2 倍大小。
@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);
}
好的@Kiran Dsouza 你的回答是正确的我必须更改相对布局参数并且它适用于第一个初始视图,但是当我水平滚动并向后滚动时它不再起作用因为它回到原来的状态查看。
我正在使用 RecyclerView
,LinearLayoutManager
是水平的。我在每个视图中显示 3 个项目并使用无休止的滚动。我的问题是它们是 3 张图片,我希望中间的图片放大 2 倍,但我无法控制控制器来解决这个问题,有没有办法做到这一点?
ex) [] [] [] = 一个视图,但中间的视图大小为 2 倍,用户可以左右滚动
代码片段: 这是我在 activity 中初始化到适配器的方式:
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;
}
}
试试这个....
首先在 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;
}
获取视图类型并根据视图类型调整 2 倍大小。
@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);
}
好的@Kiran Dsouza 你的回答是正确的我必须更改相对布局参数并且它适用于第一个初始视图,但是当我水平滚动并向后滚动时它不再起作用因为它回到原来的状态查看。