尝试对转盘行为使用水平recycleview,但是在横向模式下该项目显示不正确。
通过使用https://github.com/AndroidCodility/HorizontalRecyclerview(以及其他一些github示例)中的项目,可以看到相同的行为
风景
借用此处显示问题的代码(贷记给Govind)。
代码简单明了:
const SomeComponent = () => (<div> Some Component Page </div>)
和适配器:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (Utility.isOnline(this)) {
displayList()
} else {
Toast.makeText(this, R.string.no_internet, Toast.LENGTH_SHORT).show()
}
}
private fun displayList() {
val version = ArrayList<Version>()
version.addAll(Version.getList())
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.HORIZONTAL, false)
recyclerView.adapter = MyAdapter(version)
}
}
主要布局:
/**
* Created by Govind on 3/7/2018.
*/
class MyAdapter(private val versionList: ArrayList<Version>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItems(versionList[position])
}
override fun getItemCount(): Int {
return versionList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(version: Version) {
val textView = itemView.findViewById<TextView>(R.id.tvName)
val imageView = itemView.findViewById<ImageView>(R.id.imageView)
textView.text = version.name
Glide.with(itemView.context).load(version.url).into(imageView)
}
}
}
卡片项目的布局:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.codility.horizontalrecyclerview.MainActivity">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="@string/applicant_name"
android:textColor="@color/colorPrimaryDark"
android:textSize="40sp" />
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/about"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />
</LinearLayout>
答案 0 :(得分:1)
在卡片项目布局
中将LinearLayout
的高度更改为android:layout_height="wrap_content"
尝试一下
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@color/colorAccent"
card_view:cardCornerRadius="8dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher_round" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
编辑
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:fillViewport="true"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.codility.horizontalrecyclerview.MainActivity">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="@string/applicant_name"
android:textColor="@color/colorPrimaryDark"
android:textSize="40sp" />
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:orientation="horizontal"
android:padding="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/about"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>