代码之家  ›  专栏  ›  技术社区  ›  lannyf

horizontal recyclerview的项目cardview底部文本视图部分未完全显示

  •  0
  • lannyf  · 技术社区  · 6 年前

    正在尝试对旋转木马行为使用水平recycleview,但在横向模式下,项目显示不正确。

    同样的行为也可以通过在 https://github.com/AndroidCodility/HorizontalRecyclerview

    enter image description here

    在景观中

    enter image description here

    代码直截了当且简单:

    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>
    

    卡片项目布局:

    <?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="200dp"
            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>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   AskNilesh    6 年前

    改变你身体的高度 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>