如何更改视图属性编程方式从我的片段,我recyclerView的看法?

问题描述 投票:1回答:3

我在我的项目文件的XML设置视图(卡片视图)的保证金,这个XML文件的项目将使用在我recyclerView适配器。

正如你可以在下面我的XML看,我已经给保证金顶部,底部,开始和结束。我想保证金从我的片段改变

这里是我的xml文件,item_category_list.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
        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="wrap_content"
        app:cardCornerRadius="8dp"
        app:cardElevation="4dp"
        android:id="@+id/cardView_item_category_list" android:layout_marginStart="8dp" android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp" android:layout_marginTop="8dp">

    <androidx.constraintlayout.widget.ConstraintLayout
            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="wrap_content"
            android:background="@android:color/background_light">

        <ImageView
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:srcCompat="@drawable/logo_apps"
                android:id="@+id/categoryImageView_Item"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="24dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="24dp"
                app:layout_constraintDimensionRatio="w,1:1" android:scaleType="centerCrop"/>

        <TextView
                android:text="@string/Category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/categoryName_textView_item"
                app:layout_constraintTop_toBottomOf="@+id/categoryImageView_Item"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginStart="4dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="4dp"
                android:textAlignment="center"
                android:minLines="1"
                android:maxLines="2"
                app:autoSizeTextType="uniform"
                app:autoSizeMinTextSize="10sp"
                app:autoSizeMaxTextSize="15sp"
                app:autoSizeStepGranularity="1sp"
                android:layout_marginBottom="24dp"
                android:layout_marginTop="24dp"/>


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

这里是适配器:

class CategoryAdapter(val context: Context, val categories: List<Category>) : RecyclerView.Adapter<CategoryAdapter.ViewHolderCategory>() {

    private lateinit var mListener : CategoryAdapterListener

    interface CategoryAdapterListener {
        fun onItemClick(position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderCategory {

        val layoutInflater = LayoutInflater.from(parent.context)
        val itemView = layoutInflater.inflate(R.layout.item_category_list,parent, false)
        return ViewHolderCategory(itemView,mListener)
    }

    override fun getItemCount(): Int {
        return categories.size
    }

    override fun onBindViewHolder(holder: ViewHolderCategory, position: Int) {

        val category = categories[position]

        holder.categoryNameTextView.text = category.name
        Glide
            .with(context)
            .load(category.getFormattedImageURL())
            .into(holder.categoryImageView)

    }

    inner class ViewHolderCategory(itemView: View, listener: CategoryAdapterListener) : RecyclerView.ViewHolder(itemView) {

        val categoryImageView = itemView.findViewById<ImageView>(R.id.categoryImageView_Item)
        val categoryNameTextView = itemView.findViewById<TextView>(R.id.categoryName_textView_item)
        val cardView = itemView.findViewById<CardView>(R.id.cardView_item_category_list)

        init {

            itemView.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(position)
                }
            }

        }

    }

    fun setCategoryAdapterListener(listener: CategoryAdapterListener) {
        mListener = listener

    }
}

而在片段,我适配器设置为回收站观点:

val categoryAdapter = CategoryAdapter(mContext,parentCategory)
val layoutManager = GridLayoutManager(mContext,4,RecyclerView.VERTICAL,false)

recyclerViewParentCategory.adapter = categoryAdapter
recyclerViewParentCategory.layoutManager = layoutManager
recyclerViewParentCategory.setHasFixedSize(true)

我想通过编程修改卡认为,利润率在我item_category_list.xml在我的Java /科特林文件(在我的片段文件),这样我就可以将利润率从我的片段而改变。

那么,如何实现呢?的Java /科特林任何语言者优先。

android android-recyclerview recycler-adapter recyclerview-layout
3个回答
0
投票

首先,它是一个很长的路要走。所以我只是建议的方式。

  1. 首先 。在您的片段时,一些动作发生,你需要在适配器列表项XML改变cardview大小。 所以。你需要一个接口(假设interface ChangeMargin)。在片段创建界面并实现在适配器中像这样的接口

class CategoryAdapter(val context: Context, val categories: List<Category>):RecyclerView.Adapter<CategoryAdapter.ViewHolderCategory>(),ChangeMargin()

对于如何创建接口,以便可以顺利通过this

  1. 现在,在该界面你需要得到cardview和分配新保证金。

 @Override
         public void ChangeMargin() {
         val linear_params=LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
                 linear_params.setMargins(leftMargin,topmargin,rightMargin,bottomMargin)
                 cardView?.layoutParams=linear_params
         }

不要忘记通知适配器


0
投票

你可以做这样的事情。让cardview的ID,在适配器中创建它的实例,并做到这一点代码

ViewGroup.MarginLayoutParams的LayoutParams =(ViewGroup.MarginLayoutParams)myCardView.getLayoutParams(); layoutParams.setMargins(10,10,10,10);

myCardView.requestLayout();

0
投票

要设置页边距为cardView,你必须创建layoutParams,设置页边距,然后将其设置为cardView的LayoutParams这样的:

inner class ViewHolderCategory(itemView: View, listener: CategoryAdapterListener) : RecyclerView.ViewHolder(itemView) {

        val categoryImageView = itemView.findViewById<ImageView>(R.id.categoryImageView_Item)
        val categoryNameTextView = itemView.findViewById<TextView>(R.id.categoryName_textView_item)
        val cardView = itemView.findViewById<CardView>(R.id.cardView_item_category_list)
        //Main code here
        val lparams=LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
        lparams.setMargins(leftMargin,topmargin,rightMargin,bottomMargin)
        cardView?.layoutParams=lparams
}
© www.soinside.com 2019 - 2024. All rights reserved.