下面列出了androidx.recyclerview.widget.RecyclerView#setLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecyclerView recyclerView = new RecyclerView(this);
recyclerView.setId(R.id.test_recycler);
recyclerView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new TestAdapter(this));
setContentView(recyclerView);
helper = new SwipeOpenItemTouchHelper(
new SwipeOpenItemTouchHelper.SimpleCallback(
SwipeOpenItemTouchHelper.START | SwipeOpenItemTouchHelper.END));
helper.attachToRecyclerView(recyclerView);
if (savedInstanceState != null) {
helper.restoreInstanceState(savedInstanceState);
}
}
private RecyclerView createRecyclerView(boolean listTypeGrid) {
Context context = requireContext();
RecyclerView recyclerView = new RecyclerView(context);
recyclerView.setLayoutParams(
new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
int verticalPadding =
context.getResources().getDimensionPixelSize(R.dimen.album_list_padding_vertical);
recyclerView.setPadding(0, verticalPadding, 0, verticalPadding);
recyclerView.setClipToPadding(false);
if (listTypeGrid) {
recyclerView.setLayoutManager(new GridLayoutManager(context, GRID_SPAN_COUNT));
} else {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.addItemDecoration(
new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
}
return recyclerView;
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setNestedScrollingEnabled(false);
ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
params.height = (int) (recyclerView.getResources().getDisplayMetrics().heightPixels * 0.80);
recyclerView.setLayoutParams(params);
recyclerView.setHasFixedSize(true);
}
ViewHolder() {
super(new RecyclerView(getContext()));
RecyclerView recyclerView = (RecyclerView) getItemView();
recyclerView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mAdapter = new AddressAdapter(getContext());
mAdapter.setOnItemClickListener(this);
recyclerView.setAdapter(mAdapter);
}
/**
* 动态添加多图裁剪底部预览图片列表
*/
private void addPhotoRecyclerView() {
mRecyclerView = new RecyclerView(this);
mRecyclerView.setId(R.id.id_recycler);
mRecyclerView.setBackgroundColor(ContextCompat.getColor(this, R.color.ucrop_color_widget_background));
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, dip2px(80));
mRecyclerView.setLayoutParams(lp);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(mLayoutManager);
resetCutDataStatus();
list.get(cutIndex).setCut(true);
adapter = new PicturePhotoGalleryAdapter(this, list);
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener((position, view) -> {
if (cutIndex == position) {
return;
}
cutIndex = position;
resetCutData();
});
uCropMultiplePhotoBox.addView(mRecyclerView);
changeLayoutParams();
FrameLayout uCropFrame = findViewById(R.id.ucrop_frame);
((RelativeLayout.LayoutParams) uCropFrame.getLayoutParams())
.addRule(RelativeLayout.ABOVE, R.id.id_recycler);
}
/**
* 设置文件夹列表的高度
*
* @param mFolderListRecyclerView 文件夹列表
* @param mImageSetMask 文件夹列表的灰色透明蒙层
* @param isCrop 是否是小红书样式
*/
protected void setFolderListHeight(RecyclerView mFolderListRecyclerView, View mImageSetMask, boolean isCrop) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mFolderListRecyclerView.getLayoutParams();
RelativeLayout.LayoutParams maskParams = (RelativeLayout.LayoutParams) mImageSetMask.getLayoutParams();
PickerUiConfig uiConfig = getUiConfig();
int height = uiConfig.getFolderListOpenMaxMargin();
if (uiConfig.getFolderListOpenDirection() == PickerUiConfig.DIRECTION_BOTTOM) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
if (isCrop) {
params.bottomMargin = bottomBar != null ? bottomBar.getViewHeight() : 0;
params.topMargin = (titleBar != null ? titleBar.getViewHeight() : 0) + height;
maskParams.topMargin = (titleBar != null ? titleBar.getViewHeight() : 0);
maskParams.bottomMargin = bottomBar != null ? bottomBar.getViewHeight() : 0;
} else {
params.bottomMargin = 0;
params.topMargin = height;
}
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
if (isCrop) {
params.bottomMargin = height + (bottomBar != null ? bottomBar.getViewHeight() : 0);
params.topMargin = titleBar != null ? titleBar.getViewHeight() : 0;
maskParams.topMargin = (titleBar != null ? titleBar.getViewHeight() : 0);
maskParams.bottomMargin = bottomBar != null ? bottomBar.getViewHeight() : 0;
} else {
params.bottomMargin = height;
params.topMargin = 0;
}
}
mFolderListRecyclerView.setLayoutParams(params);
mImageSetMask.setLayoutParams(maskParams);
}
private void setupAlignment(RecyclerView recycler) {
if (!mAlreadyAligned) {
//setting alignment of header
ViewGroup.LayoutParams currentParams = getLayoutParams();
FrameLayout.LayoutParams newHeaderParams;
int width = ViewGroup.LayoutParams.WRAP_CONTENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
int gravity = (mReversed ? Gravity.BOTTOM : Gravity.TOP) | Gravity.CENTER_HORIZONTAL;
if (currentParams != null) {
newHeaderParams = new FrameLayout.LayoutParams(getLayoutParams()); //to copy all the margins
newHeaderParams.width = width;
newHeaderParams.height = height;
newHeaderParams.gravity = gravity;
} else {
newHeaderParams = new FrameLayout.LayoutParams(width, height, gravity);
}
RecyclerViewHeader.this.setLayoutParams(newHeaderParams);
//setting alignment of recycler
FrameLayout newRootParent = new FrameLayout(recycler.getContext());
newRootParent.setLayoutParams(recycler.getLayoutParams());
ViewParent currentParent = recycler.getParent();
if (currentParent instanceof ViewGroup) {
int indexWithinParent = ((ViewGroup) currentParent).indexOfChild(recycler);
((ViewGroup) currentParent).removeViewAt(indexWithinParent);
recycler.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
newRootParent.addView(recycler);
newRootParent.addView(RecyclerViewHeader.this);
((ViewGroup) currentParent).addView(newRootParent, indexWithinParent);
}
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
RecyclerView recyclerView = new RecyclerView(mContext);
recyclerView.setLayoutManager(new GridLayoutManager(mContext, COLUMN_COUNT));
recyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
EmoticonChildAdapter adapter = new EmoticonChildAdapter(mContext, mHeight);
adapter.setData(EmoticonUtils.EMOTICON_LABEL[position][0], EmoticonUtils.EMOTICON_URL[position]);
recyclerView.setAdapter(adapter);
container.addView(recyclerView);
return recyclerView;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRecyclerView = new RecyclerView(container.getContext());
mRecyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mRecyclerView.setLayoutManager(new GridLayoutManager(container.getContext(), 4));
mRecyclerView.setAdapter(new Adapter());
return mRecyclerView;
}
/**
* 动态添加多图裁剪底部预览图片列表
*/
private void addPhotoRecyclerView() {
boolean isMultipleSkipCrop = getIntent().getBooleanExtra(UCrop.Options.EXTRA_SKIP_MULTIPLE_CROP, true);
mRecyclerView = new RecyclerView(this);
mRecyclerView.setId(R.id.id_recycler);
mRecyclerView.setBackgroundColor(ContextCompat.getColor(this, R.color.ucrop_color_widget_background));
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
ScreenUtils.dip2px(this, 80));
mRecyclerView.setLayoutParams(lp);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
if (isAnimation) {
LayoutAnimationController animation = AnimationUtils
.loadLayoutAnimation(getApplicationContext(), R.anim.ucrop_layout_animation_fall_down);
mRecyclerView.setLayoutAnimation(animation);
}
mRecyclerView.setLayoutManager(mLayoutManager);
// 解决调用 notifyItemChanged 闪烁问题,取消默认动画
((SimpleItemAnimator) mRecyclerView.getItemAnimator())
.setSupportsChangeAnimations(false);
resetCutDataStatus();
list.get(cutIndex).setCut(true);
mAdapter = new PicturePhotoGalleryAdapter(this, list);
mRecyclerView.setAdapter(mAdapter);
if (isMultipleSkipCrop) {
mAdapter.setOnItemClickListener(new PicturePhotoGalleryAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position, View view) {
CutInfo cutInfo = list.get(position);
if (MimeType.isHasVideo(cutInfo.getMimeType())) {
return;
}
if (cutIndex == position) {
return;
}
resetLastCropStatus();
cutIndex = position;
oldCutIndex = cutIndex;
resetCutData();
}
});
}
uCropPhotoBox.addView(mRecyclerView);
changeLayoutParams(mShowBottomControls);
// 裁剪框居于RecyclerView之上
FrameLayout uCropFrame = findViewById(R.id.ucrop_frame);
((RelativeLayout.LayoutParams) uCropFrame.getLayoutParams())
.addRule(RelativeLayout.ABOVE, R.id.id_recycler);
// RecyclerView居于BottomControls之上
((RelativeLayout.LayoutParams) mRecyclerView.getLayoutParams())
.addRule(RelativeLayout.ABOVE, R.id.controls_wrapper);
}