如何增加或减少回收视图网格图像大小?

时间:2021-04-06 14:13:02

标签: android layout

我不知道如何增加或减少图像大小以及如何回收它。下面是丝绸给布局。我想知道这个问题的答案有人告诉我。下面丝绸中的编码是你能理解的丝绸。如何增加或减少图像尺寸android。请重新使用Recyleview。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <com.balysv.materialripple.MaterialRippleLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:mrl_rippleAlpha="0.2"
            app:mrl_rippleColor="@color/ripple_color"
            app:mrl_rippleHover="true"
            app:mrl_rippleOverlay="true">
    
            <LinearLayout
                android:id="@+id/lyt_parent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">
    
                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <com.makeramen.roundedimageview.RoundedImageView
                        android:id="@+id/category_image"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/ic_thumbnail"
                        app:layout_constraintDimensionRatio="H,1:1"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:riv_corner_radius="12dp" />
    
                </androidx.constraintlayout.widget.ConstraintLayout>
    
                <TextView
                    android:id="@+id/category_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:padding="6dp"
                    android:text="Category"
                    android:ellipsize="end"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Body2"
                    android:textSize="14sp" />
    
            </LinearLayout>
    
        </com.balysv.materialripple.MaterialRippleLayout>
    
    </RelativeLayout>

适配器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.Tamiltv.newspaper.Config;
import com.Tamiltv.newspaper.R;
import com.Tamiltv.newspaper.models.Category;
import com.Tamiltv.newspaper.utils.SharedPref;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static com.Tamiltv.newspaper.utils.Constant.CHANNEL_GRID_2_COLUMN;
import static com.Tamiltv.newspaper.utils.Constant.CHANNEL_GRID_3_COLUMN;

public class AdapterCategory extends RecyclerView.Adapter<AdapterCategory.ViewHolder> {

    private List<Category> items;

    private Context ctx;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, Category obj, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mOnItemClickListener = mItemClickListener;
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public AdapterCategory(Context context, List<Category> items) {
        this.items = items;
        ctx = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView category_name;
        public ImageView category_image;
        public LinearLayout lyt_parent;

        public ViewHolder(View v) {
            super(v);
            category_name = v.findViewById(R.id.category_name);
            category_image = v.findViewById(R.id.category_image);
            lyt_parent = v.findViewById(R.id.lyt_parent);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // create a new view
        SharedPref sharedPref = new SharedPref(ctx);
        if (sharedPref.getCategoryViewType() == CHANNEL_GRID_2_COLUMN || sharedPref.getCategoryViewType() == CHANNEL_GRID_3_COLUMN) {
            View menuItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lsv_item_category_grid, parent, false);
            return new ViewHolder(menuItemView);
        } else {
            View menuItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lsv_item_category, parent, false);
            return new ViewHolder(menuItemView);
        }

    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final Category c = items.get(position);

        holder.category_name.setText(c.category_name);

        Picasso.get()
                .load(Config.ADMIN_PANEL_URL + "/upload/category/" + c.category_image)
                .placeholder(R.drawable.ic_thumbnail)
                .into(holder.category_image);

        holder.lyt_parent.setOnClickListener(view -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(view, c, position);
            }
        });
    }

    public void setListData(List<Category> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    public void resetListData() {
        this.items = new ArrayList<>();
        notifyDataSetChanged();
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return items.size();
    }

}

0 个答案:

没有答案