我遇到了麻烦!我有这个RecyclerView,我使用GridLayoutManager来实现两列和几行。 但这是我的问题: 我在这个RecyclerView中最多有8个项目,并且我想根据屏幕尺寸拟合它们
到目前为止,我已经得到了这个:
使用这段代码:
preg_replace_callback
我的 Rect rectangle = new Rect();
Window window = ((Activity)context).getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop =
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
int softButtonsHeight = 0;
DisplayMetrics metrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
DisplayMetrics realMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(realMetrics);
if(realMetrics.heightPixels > metrics.heightPixels){
softButtonsHeight = realMetrics.heightPixels - metrics.heightPixels;
}
}
ImageView img_Logo = (ImageView)rootView.findViewById(R.id.img_logo_detalhe);
float logoHeight = 0;
//convertendo na mão tamanho do sponsor
if(img_Logo.getVisibility() != GONE) {
logoHeight = 100 * context.getResources().getDisplayMetrics().density;
}
double sizeInPx = (metrics.heightPixels - titleBarHeight - softButtonsHeight - mActionBarSize - logoHeight) / Math.round(list.size() / 2D);
itensAdapter = new OptionItensAdapter(context, list, (int)sizeInPx);
rvOptions.setAdapter(itensAdapter);
和 OptionItensAdapter构造函数内部:
onBindViewHolder
你有什么想法会让我实现这个目标吗? 提前谢谢。
答案 0 :(得分:5)
答案 1 :(得分:2)
查看此OnBindViewHolder代码并根据您的要求进行更改:D
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
final int pos = position;
try {
//
Resources r = activity.getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()); // i have bottom tabbar so yf you dont have any thing like this just leave 150 to 0.I think in your case height of image view an your top(Pifer)
//this change height of rcv
DisplayMetrics displaymetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.height = (height - px) / 5; //height recycleviewer (there are 5 rows so divide by 5 but i think in your case there are 4 rows so divide by 4)
viewHolder.itemView.setLayoutParams(params);
viewHolder.nameTxt.setText(totalList.get(position).getName());
viewHolder.icon.setImageResource(totalList.get(position).getIcon());
// viewHolder.background.setBackground(ContextCompat.getDrawable(context, totalList.get(position).getBackground()));
} catch (Exception e) {
e.printStackTrace();
}
}
只需发布此viewHolder即可查看所有项目。
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTxt;
public RelativeLayout background;
public ImageView icon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
nameTxt = (TextView) itemLayoutView.findViewById(R.id.menu_label);
background = (RelativeLayout) itemLayoutView.findViewById(R.id.menu_background);
icon = (ImageView) itemLayoutView.findViewById(R.id.menu_icon);
}
答案 2 :(得分:2)
我的建议是使用类似于此的布局而不是RecyclerView,它适用于任何屏幕。布局将自行完成调整,无需任何代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:src="@android:drawable/sym_def_app_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:src="@android:drawable/sym_def_app_icon" />
</LinearLayout>
</LinearLayout>
答案 3 :(得分:1)
此处GridLayout
或Constraint
布局是更好的选择。
RecyclerView
(正如其名称所示)用于回收 - 当你有很多观点/孩子时你应该使用一个,并且需要确保屏幕上只有少数人正在使用内存。
ConstraintLayout
将允许您单独包含每个视图,并定义它们之间的相互关系以创建网格模式。
GridLayout
就像我下面的示例一样,会为您安排这些物品,而无需回收。
<GridLayout android:id="@+id/..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal".
android:orientation="horizontal"
android:columnCount="2"
android:rowCount="4">
<OptionItem ...
android:weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<OptionItem ...
android:weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<OptionItem ...
android:weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
...
</GridLayout>
在您的代码中,您可以更改要隐藏的8个按钮的可见性
button8.setVisibility(View.INVISIBLE); //don't use GONE inside the grid
如果要以编程方式设置项目宽度(或高度),请设置useDefaultMargins="true"
并更改布局参数(根据this答案)
GridLayout.LayoutParams params = (GridLayout.LayoutParams) child.getLayoutParams();
params.width = (parent.getWidth()/parent.getColumnCount()) -params.rightMargin - params.leftMargin;
child.setLayoutParams(params);
答案 4 :(得分:0)
基本上,获得屏幕尺寸,然后相应地调整你的身高,所以它的要点是:
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
if(position == 0) {
holder.itemView.getLayoutParams().width = displayMetrics.width;
holder.itemView.getLayoutParams().height = displayMetrics.height / 8;
} else {
holder.itemView.getLayoutParams().width = displayMetrics.width / 2;
holder.itemView.getLayoutParams().height = displayMetrics.height / 8;
}
答案 5 :(得分:0)
如果您需要将视图修复为屏幕而不需要使用recyclerView。您可以玩重量并使物品适合屏幕。
在您的方案中,您可以按照以下代码
//llContainer main layout in which you want to put 8 values having orientation vertical
llContainer.setWeightSum(numberofRaws); // It will be 4 if you want to put 8 values
for(int i=1; i<=numberofRaws ; i++ ){
//Inflate One LinearLayout which has height width Match Parent
LinearLayout llRaw = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_plain_with_horizontal_orientation, null);
llRaw.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARRENT, 1.0f));
AddTwoViewForFaw(llRaw);
llContainer.AddView(llRaw);
}
public void AddTwoViewForRaw(LinearLayout llRaw){
View v1 = LayoutInflater.from(getContext()).inflate(R.layout.grideLayout, null);
// Here you can set values for grid layout by v1.findViewbyId()
v1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
llRaw.addView(v1);
View v2 = LayoutInflater.from(getContext()).inflate(R.layout.grideLayout, null);
// Here you can set values for grid layout by v2.findViewbyId()
v2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
llRaw.addView(v2);
}
希望它能为你效劳。
答案 6 :(得分:0)
添加自定义网格行并在其中设置大小,然后设置将根据屏幕自动调整的自动调整
答案 7 :(得分:0)
为什么要使用RecyclerView
?
GridLayout
是最好的选择。您可以使用weights
对象。
以下示例说明如何将6 LinearLayout
s放入屏幕
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.waqasansari.hitwithme.main.fragments.Dashboard">
<LinearLayout
android:id="@+id/myMatches"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="0"
app:layout_row="0"
android:background="@drawable/border_gray"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/dashboard_my_matches"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Matches"/>
</LinearLayout>
<LinearLayout
android:id="@+id/requestMatches"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="1"
app:layout_row="0"
android:background="@drawable/border_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/dashboard_match_requests"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Match Requests"/>
</LinearLayout>
<LinearLayout
android:id="@+id/proShop"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="0"
app:layout_row="1"
android:background="@drawable/border_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/dashboard_pro_shop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pro Shops"/>
</LinearLayout>
<LinearLayout
android:id="@+id/rankings"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="1"
app:layout_row="1"
android:background="@drawable/border_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/dashboard_rankings"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rankings"/>
</LinearLayout>
<LinearLayout
android:id="@+id/courtsAndCoaches"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="0"
app:layout_row="2"
android:background="@drawable/border_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/dashboard_courts_coaches"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Courts & Coaches"/>
</LinearLayout>
<LinearLayout
android:id="@+id/inviteFriends"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_column="1"
app:layout_row="2"
android:background="@drawable/border_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/dashboard_invite_friends"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Invite Friends"/>
</LinearLayout>
</android.support.v7.widget.GridLayout>
您可以以类似的方式添加更多项目
答案 8 :(得分:0)
如果您的菜单没有动态更改,即您在API上有菜单设置,那么您不必使用/**
* @var string
*/
private $descripcion;
/**
* @var boolean
*/
private $flag;
public function __toString()
{
return $this->getDescripcion();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set descripcion
*
* @param string $descripcion
* @return categoria
*/
public function setDescripcion($descripcion)
{
$this->descripcion = $descripcion;
return $this;
}
/**
* Get descripcion
*
* @return string
*/
public function getDescripcion()
{
return $this->descripcion;
}
/**
* Set flag
*
* @param boolean $flag
* @return categoria
*/
public function setFlag($flag)
{
$this->flag = $flag;
return $this;
}
/**
* Get flag
*
* @return boolean
*/
public function getFlag()
{
return $this->flag;
}
或Recyclerview
来填充此布局。我希望将GridView
(s)与一些约束相结合来填充静态布局:
LinearLayout
这是结果:
答案 9 :(得分:0)
如果您修复了8个项目,那么您可以将LinearLayout和SDP库用于图标大小,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="android.com.linearlayouthome.MainActivity">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_60sdp"
android:layout_height="@dimen/_60sdp"
android:layout_gravity="center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="4">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp" />
<TextView
android:gravity="center_horizontal"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
使用SDP库,您不需要为不同的屏幕尺寸编写尺寸文件
屏幕截图: Nexus 4 :
Nexus 5X:
Nexus 6:
答案 10 :(得分:0)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.item_list, null);
int height = parent.getMeasuredHeight() / 4;
int width = parent.getMeasuredWidth();
view.setLayoutParams(new RecyclerView.LayoutParams(width, height));
return new ViewHolder(view);
}