我尝试使用方形图像制作网格布局.我认为必须有可能GridLayoutManager通过操纵onMeasure来操纵
super.onMeasure(recycler, state, widthSpec, widthSpec);
Run Code Online (Sandbox Code Playgroud)
代替
super.onMeasure(recycler, state, widthSpec, heightSpec);
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这没有用.
有任何想法吗?
我有一个RecyclerView在里面CardView.该CardView有500dp的高度,但我想缩短这个高度,如果RecyclerView是较小的.所以我想知道RecyclerView在第一次完成放置项目时是否有任何监听器被调用,从而可以将RecyclerView高度设置为CardView高度(如果小于500dp).
我想在回收站视图中为每个项目设置不同的高度。通常我们每行都得到相同的高度。但是我需要不同的高度,例如,如果前三行的高度为 70,那么我需要其余的高度为 0。我的代码如下。在这段代码中,它没有正确设置。请建议我编辑
public class MainActivity extends AppCompatActivity {
private RecyclerView rvListProfiles;
private ListProfileAdapter listProfileAdapter;
private String[] list = new String[]{"ABC","DEF","GHI","JKL","MNO"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvListProfiles = findViewById(R.id.rvListProfiles);
listProfileAdapter = new ListProfileAdapter(list, this);
RecyclerView.LayoutManager mLayoutManager = new
LinearLayoutManager(getApplicationContext());
rvListProfiles.setLayoutManager(mLayoutManager);
rvListProfiles.setItemAnimator(new DefaultItemAnimator());
rvListProfiles.setAdapter(listProfileAdapter);
String listString = Arrays.toString(list);
System.out.println("List "+listString);
}
}
Run Code Online (Sandbox Code Playgroud)
项目xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:id="@+id/parentLayout">
<ImageView
android:id="@+id/profileImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:src="@mipmap/love"
android:transitionName="imageTransition" />
<TextView
android:id="@+id/profileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我有一个recyclerview只是一个水平按钮列表。我正在尝试创建以下行为:
如果所有按钮组合的总宽度小于屏幕总宽度,则将每个按钮的宽度设置为(屏幕宽度/按钮数)。
我正在测试一些基本代码来获取/设置按钮宽度,就像在我的adapter课堂上一样:
override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
Log.d("TEST-APP","Button Width: " + holder.button.width.toString())
holder.button.width = 1080
Log.d("VED-APP","New Button Width: " + holder.button.width.toString())
holder.button.text = items[position].first
holder.button.setOnClickListener {
(context as MainActivity).updateCurrentImageLayout(items[position].second)
}
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,虽然这段代码确实将按钮的宽度更改为 1080,但Log在设置按钮宽度之前/之后两个函数的输出都是 0。
如何获得按钮的实际宽度?
这是我的布局,它定义了我的一列recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:id="@+id/tabButton"
android:layout_width="match_parent"
android:minWidth="120dp"
android:layout_height="match_parent"
android:text="Unset-Button-Text"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)