RecycleView如何实现各种尺寸图片展示

发布时间:2021-05-31 12:54:52 作者:小新
来源:亿速云 阅读:131

这篇文章主要介绍RecycleView如何实现各种尺寸图片展示,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

今天才发现,在一个RecycleView里可以展示各种尺寸的图片,贴图如下:

RecycleView如何实现各种尺寸图片展示

代码如下:

package com.hisense.movienow.HorizontalView;
 
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
 
 
import java.util.List;
 
/**
 * Created by wangcm on 2018/11/13.
 */
 
public class SimpleRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = SimpleRecyclerAdapter.class.getSimpleName();
    private LayoutInflater mInflater;
    private List<VideoCateContent> mListOfApps;
    private int currentPosition = 0;
    private Context context;
    public static enum ITEM_TYPE {
        ITEM_TYPE_MOVIE,
        ITEM_TYPE_APP,
        ITEM_TYPE_CHANNEL
    }
    List<MovieCategory> movieCategories;
 
    public SimpleRecyclerAdapter(Context context, List<VideoCateContent> mListOfApps){
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.mListOfApps = mListOfApps;
    }
    public SimpleRecyclerAdapter(Context context, List<VideoCateContent> mListOfApps,List<MovieCategory> movieCategories){
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.mListOfApps = mListOfApps;
        this.movieCategories = movieCategories;
    }
 
 
    @SuppressWarnings("unused")
    public void setData(List<VideoCateContent> mListOfApps){
        this.mListOfApps = mListOfApps;
    }
 
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == ITEM_TYPE.ITEM_TYPE_MOVIE.ordinal()){
            return new ViewHolder(mInflater.inflate(R.layout.movie_item_layout,parent,false));
        }else if(viewType == ITEM_TYPE.ITEM_TYPE_CHANNEL.ordinal()){
            return new ChanneHolder(mInflater.inflate(R.layout.search_channel_item,parent,false));
        }else{
            return new AppViewHolder(mInflater.inflate(R.layout.home_app_item,parent,false));
        }
    }
 
    public int getItemViewType(int position){
        if(mListOfApps != null){
            if(mListOfApps.get(position).getType() != null && mListOfApps.get(position).getType().equalsIgnoreCase("app")){
                return ITEM_TYPE.ITEM_TYPE_APP.ordinal();
            }else if(mListOfApps.get(position).getType() != null && mListOfApps.get(position).getType().equals("Channel")){
                return ITEM_TYPE.ITEM_TYPE_CHANNEL.ordinal();
            }else {
                return ITEM_TYPE.ITEM_TYPE_MOVIE.ordinal();
            }
        }else{
            return ITEM_TYPE.ITEM_TYPE_MOVIE.ordinal();
        }
    }
 
 
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final VideoCateContent videoCateContent = mListOfApps.get(position);
        if(holder instanceof ViewHolder){
            Glide.with(context).load(videoCateContent.getPoster()).into(((ViewHolder)holder).mImageView);
            ((ViewHolder)holder).mTextView.setText(videoCateContent.getTitle());
            ViewGroup.LayoutParams params = ((ViewHolder)holder).mImageView.getLayoutParams();
            ViewGroup.LayoutParams txtParams = ((ViewHolder)holder).mTextView.getLayoutParams();
            if(videoCateContent.getPoster_type() == 0){//竖海报
                params.width = 166;
                txtParams.width = 166;
            }else{
                params.width = 444;
                txtParams.width = 444;
//                RecyclerView.ItemDecoration itemDecoration = ((RecyclerView)holder.itemView.getParent()).getItemDecorationAt(position);
 
            }
            if(videoCateContent.getIs_new() == 1){
                ((ViewHolder)holder).newImage.setVisibility(View.VISIBLE);
            }else{
                ((ViewHolder)holder).newImage.setVisibility(View.GONE);
            }
 
            ((ViewHolder)holder).mImageView.setLayoutParams(params);
            ((ViewHolder)holder).mTextView.setLayoutParams(txtParams);
            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        currentPosition = (int) holder.itemView.getTag();
                        ViewCompat.animate(holder.itemView).scaleX(1.10f).scaleY(1.10f).start();
 
                        int bigPostion = 0;
                        if(movieCategories != null && movieCategories.size() != 0){
                            bigPostion = getCurrentIndex();
                        }
                        mOnItemSelectListener.onItemSelect(holder.itemView, position,bigPostion);
                        ((ViewHolder)holder).mImageView.setBorderColor(context.getResources().getColor(R.color.white),context.getResources().getColor(R.color.black),context.getResources().getColor(R.color.app_text_color));
                        ((ViewHolder)holder).mTextView.setSelected(true);
                        ((ViewHolder)holder).mTextView.setTextColor(context.getResources().getColor(R.color.white));
                        ((ViewHolder)holder).newImage.setPadding(3,3,0,0);
 
                    } else {
                        int bigPostion = 0;
                        if(movieCategories != null && movieCategories.size() != 0){
                            bigPostion = getCurrentIndex();
                        }
                        mOnItemNoSelectListener.onItemNoSelect(holder.itemView,position,bigPostion);
                        ViewCompat.animate(holder.itemView).scaleX(1.0f).scaleY(1.0f).start();
                        ((ViewHolder)holder).mImageView.setBorderColor(context.getResources().getColor(R.color.trans),context.getResources().getColor(R.color.trans),context.getResources().getColor(R.color.trans));
                        ((ViewHolder)holder).mTextView.setSelected(false);
                        ((ViewHolder)holder).mTextView.setTextColor(context.getResources().getColor(R.color.title_color));
                        ((ViewHolder)holder).newImage.setPadding(0,0,0,0);
                    }
                }
            });
 
        }else if(holder instanceof AppViewHolder){
            Glide.with(context).load(videoCateContent.getPoster()).into(((AppViewHolder)holder).mAppImageView);
            ((AppViewHolder)holder).tv_appName.setText(videoCateContent.getTitle());
            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        currentPosition = (int) holder.itemView.getTag();
                        ViewCompat.animate(holder.itemView).scaleX(1.10f).scaleY(1.10f).start();
                      
 
                        int bigPostion = 0;
                        if(movieCategories != null && movieCategories.size() != 0){
                            bigPostion = getCurrentIndex();
                        }
                        mOnItemSelectListener.onItemSelect(holder.itemView, position,bigPostion);
                        ((AppViewHolder)holder).tv_appName.setVisibility(View.VISIBLE);
                        ((AppViewHolder)holder).mAppImageView.setBorderColor(context.getResources().getColor(R.color.white),context.getResources().getColor(R.color.black),context.getResources().getColor(R.color.app_text_color));
                    } else {
                        int bigPostion = 0;
                        if(movieCategories != null && movieCategories.size() != 0){
                            bigPostion = getCurrentIndex();
                        }
                        mOnItemNoSelectListener.onItemNoSelect(holder.itemView,position,bigPostion);
                        ViewCompat.animate(holder.itemView).scaleX(1.0f).scaleY(1.0f).start();
                        ((AppViewHolder)holder).tv_appName.setVisibility(View.GONE);
                        ((AppViewHolder)holder).mAppImageView.setBorderColor(context.getResources().getColor(R.color.trans),context.getResources().getColor(R.color.trans),context.getResources().getColor(R.color.trans));
                    }
                }
            });
        }else if(holder instanceof ChanneHolder){
            Glide.with(context).load(videoCateContent.getPoster()).into(((ChanneHolder)holder).mChannelImg);
            ((ChanneHolder)holder).tv_channelName.setText(videoCateContent.getTitle());
            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        currentPosition = (int) holder.itemView.getTag();
                        ViewCompat.animate(holder.itemView).scaleX(1.10f).scaleY(1.10f).start();
                    
 
                        int bigPostion = 0;
                        if(movieCategories != null && movieCategories.size() != 0){
                            bigPostion = getCurrentIndex();
                        }
                        mOnItemSelectListener.onItemSelect(holder.itemView, position,bigPostion);
                        ((ChanneHolder)holder).tv_channelName.setSelected(true);
                        ((ChanneHolder)holder).mChannelImg.setBorderColor(context.getResources().getColor(R.color.white),context.getResources().getColor(R.color.black),context.getResources().getColor(R.color.app_text_color));
                    } else {
                        int bigPostion = 0;
                        if(movieCategories != null && movieCategories.size() != 0){
                            bigPostion = getCurrentIndex();
                        }
                        mOnItemNoSelectListener.onItemNoSelect(holder.itemView,position,bigPostion);
                        ViewCompat.animate(holder.itemView).scaleX(1.0f).scaleY(1.0f).start();
                        ((ChanneHolder)holder).tv_channelName.setSelected(false);
                        ((ChanneHolder)holder).mChannelImg.setBorderColor(context.getResources().getColor(R.color.trans),context.getResources().getColor(R.color.trans),context.getResources().getColor(R.color.trans));
                    }
                }
            });
        }
 
 
 
        // 设置itemView可以获得焦点
        holder.itemView.setFocusable(true);
        holder.itemView.setTag(position);
 
 
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!CommonUtils.getInstance(context).isNetworkConnected()){
                    Toast.makeText(context,context.getResources().getString(R.string.no_net),Toast.LENGTH_SHORT).show();
                    return;
                }
                mOnItemClickListener.onItemClick(v, position);
                if(videoCateContent.getHas_detail_page() == 1){
                    Intent intent = new Intent(context,DetailActivity.class);
                    intent.putExtra("media_id",videoCateContent.getId());
                    context.startActivity(intent);
                }else if(videoCateContent.getType().equalsIgnoreCase(context.getString(R.string.App))){
 
                    String pkgName = getPackageName(videoCateContent.getTitle());
                    if(pkgName != null){
                        Log.e(TAG,"pkgName======="+pkgName);
//                        if(CommonUtils.getInstance(context).isInstalled(context,pkgName)){
                            CommonUtils.getInstance(context).openApp((MainActivity)context,pkgName);
//                        }
                    }else{
                        String searchStr = videoCateContent.getTitle();
                        Intent intent= new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse("market://search?q="+searchStr));
                        context.startActivity(intent);
                    }
 
                }else{
                    if(!CommonUtils.getInstance(context).isNetworkConnected()){
                        Toast.makeText(context,context.getResources().getString(R.string.no_net),Toast.LENGTH_SHORT).show();
                        return;
                    }
                    HistoryBean historyBean = new HistoryBean();
                    historyBean.setWatchTime(System.currentTimeMillis());
                    historyBean.setVideoInfo(videoCateContent);
                    for(int i = 0; i< Constant.historyBeanList.size(); i++){
                        HistoryBean bean = Constant.historyBeanList.get(i);
                        if(bean.getVideoInfo().getId() == historyBean.getVideoInfo().getId()){
                            Constant.historyBeanList.remove(bean);
                        }
                        if(Constant.historyBeanList.size() >49){
                            if(i > 48){
                                Constant.historyBeanList.remove(i);
                            }
                        }
                    }
                    Constant.historyBeanList.add(0,historyBean);
                    SaveValue.getInstance(context).setDataList("history",Constant.historyBeanList);
 
                    Log.e(TAG,"deep_linking="+videoCateContent.getDeep_linking());
 
                    String deepLinking = videoCateContent.getDeep_linking();
                    if(deepLinking.contains("youtube")){
                        final String packageName = getPackageName("youtube");
                        if(!CommonUtils.getInstance(context).isInstalled(context,packageName)){
                            DialogHelper.getBaseDialogWithTextAndTitle("",context,"",new View.OnClickListener(){
                                @Override
                                public void onClick(View v) {
                                    Intent searchIntent= new Intent(Intent.ACTION_VIEW);
                                    searchIntent.setData(Uri.parse("market://search?q="+packageName));
                                    context.startActivity(searchIntent);
                                }
                            },new View.OnClickListener(){
 
                                @Override
                                public void onClick(View v) {
 
                                }
                            } ).show();
                            return;
                        }
                        String[] param = deepLinking.split("&");
                        Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://************?"+param[param.length-1]));
                        webIntent.setPackage(packageName);
                        context.startActivity(webIntent);
                    }else{
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        Uri uri = Uri.parse(videoCateContent.getDeep_linking());
                        intent.setData(uri);
                        context.startActivity(intent);
                    }
 
                }
            }
        });
 
        holder.itemView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                mOnItemKeyListener.OnItemKey(v, keyCode, event, currentPosition);
                return false;
            }
        });
    }
 
    /*
    *  get app package name
    * */
    public String getPackageName(String title){
        Log.e(TAG,"title="+title.substring(0,4));
        for(int i=0;i<Constant.packageNames.length;i++){
            String packageName = Constant.packageNames[i];
            if(packageName.contains(title.substring(0,4).toLowerCase())){
                return packageName;
            }
        }
        return null;
    }
 
    @Override
    public int getItemCount() {
        return mListOfApps.size();
    }
 
    class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout ll_movieItem;
        BoundsImageView mImageView;
        TextView mTextView;
        ImageView newImage;
 
        ViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.movie_image);
            mTextView = itemView.findViewById(R.id.movie_title);
            newImage = itemView.findViewById(R.id.iv_newFlag);
        }
    }
 
    class AppViewHolder extends RecyclerView.ViewHolder{
        BoundsImageView mAppImageView;
        TextView tv_appName;
        AppViewHolder(View itemView) {
            super(itemView);
            mAppImageView = itemView.findViewById(R.id.iv_app);
            tv_appName = itemView.findViewById(R.id.tv_appName);
        }
    }
 
    class ChanneHolder extends RecyclerView.ViewHolder{
        BoundsImageView mChannelImg;
        TextView tv_channelName;
 
        public ChanneHolder(View itemView) {
            super(itemView);
            mChannelImg = itemView.findViewById(R.id.iv_channel);
            tv_channelName = itemView.findViewById(R.id.tv_channelName);
        }
    }
 
    private OnItemSelectListener mOnItemSelectListener;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private OnItemKeyListener mOnItemKeyListener;
    private OnItemNoSelectListener mOnItemNoSelectListener;
 
    public interface OnItemSelectListener {
        void onItemSelect(View view, int position,int bigPosition);
    }
 
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
 
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }
 
    public interface OnItemKeyListener {
        void OnItemKey(View view, int keyCode, KeyEvent event, int position);
    }
 
    public interface OnItemNoSelectListener {
        void onItemNoSelect(View view, int position,int bigPosition);
    }
    public void setOnItemNoSelectListener(OnItemNoSelectListener listener){
        mOnItemNoSelectListener = listener;
    }
 
    public void setOnItemSelectListener(OnItemSelectListener listener){
        mOnItemSelectListener = listener;
    }
 
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
 
    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }
 
    public void setOnItemKeyListener(OnItemKeyListener mOnItemKeyListener) {
        this.mOnItemKeyListener = mOnItemKeyListener;
    }
 
    public int getCurrentIndex(){
        for(int i=0;i<movieCategories.size();i++){
            MovieCategory movieCategory = movieCategories.get(i);
            if(movieCategory.getMovieItemList().equals(mListOfApps)){
                return i;
            }
        }
        return 0;
    }
 
}

另外一种显示方式:

RecycleView如何实现各种尺寸图片展示

以上是“RecycleView如何实现各种尺寸图片展示”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. RecycleView怎么用
  2. js如何实现3D图片展示效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

recycleview

上一篇:vue如何实现上传组件

下一篇:pytorch读取自制数据集的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》