RecyclerView 的切换横竖两种布局文件

需求

除了修改LayoutManager的布局设置,还有修改资源文件的判断

代码

1、project_history_grid_item.xml

九宫格item



    

        
            
            
            
            
        

        
        

    

2、project_history_list_item

列表Item



    
    

    

        

            
            

            
            

        

        
        

        
        

            

        

    


3、Adapter

public class HistoryProjectListAdapter extends RecyclerView.Adapter {

    // 项目显示模式:网格/列表
    public static final int GRID = 0;
    public static final int LIST = 1;
    // 当前选中项
    private int mSelectIndex = -1;
    // 默认为网格显示
    private int mType = LIST;
    // 待显示的项目
    private List mProjects;
    // 项目点击响应
    private ImplProjectListItemListener mClickListener;


    public HistoryProjectListAdapter(List projects, ImplProjectListItemListener itemListener) {
        this.mProjects = projects;
        this.mClickListener = itemListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder holder;
        final View view;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        switch (mType) {
        default:
        case GRID:
            view = inflater.inflate(R.layout.project_history_grid_item, viewGroup, false);
            holder = new GridViewHolder(view);
            break;
        case LIST:
            view = inflater.inflate(R.layout.project_history_list_item, viewGroup, false);
            holder = new ListViewHolder(view);
            break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        File file = mProjects.get(position);
        final int pos = position;

        if (holder instanceof ListViewHolder) {// 列表显示
            ListViewHolder listHolder = (ListViewHolder) holder;
            listHolder.name.setText(file.getName());
            listHolder.time.setText(U.getTime(new Date(file.lastModified())));
            listHolder.size.setText(FileHelper.getFileSize(file));
            if (mSelectIndex == position) {
                listHolder.tag.setBackgroundResource(R.drawable.ic_uav_radio_f);
            } else {
                listHolder.tag.setBackgroundResource(R.drawable.ic_uav_radio_d);
            }
            listHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickListener.onItemClick(pos);
                }
            });

        } else if (holder instanceof GridViewHolder) {// 格网显示
            GridViewHolder gridHolder = (GridViewHolder) holder;
            gridHolder.name.setText(file.getName());
            if (mSelectIndex == position) {
                gridHolder.tag.setVisibility(View.VISIBLE);
            } else {
                gridHolder.tag.setVisibility(View.GONE);
            }
            gridHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickListener.onItemClick(pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mProjects.size();
    }

    /**
     * 设置显示形式
     * @param type 类型
     */

    public void setViewType(int type) {
        mType = type;
    }

    /**
     * 获取显示形式
     * @return 类型
     */
    public int getViewType() {
        return mType;
    }

    /**
     * 设置选择项
     * @param selectIndex 索引
     */
    public void setSelectIndex(int selectIndex) {
        mSelectIndex = selectIndex;
        this.notifyDataSetChanged();
    }

    /**
     * 删除某个项目
     * @param position 索引
     */
    public void removeItem(int position) {
        mProjects.remove(position);
        notifyItemChanged(position);
    }

    /**
     * 列表显示子项布局
     */
    private class ListViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView time;
        TextView size;
        ImageView tag;// 选中标记
        View view;

        public ListViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            name = itemView.findViewById(R.id.prj_item_filename);
            time = itemView.findViewById(R.id.prj_item_use_time);
            size = itemView.findViewById(R.id.prj_item_size);
            tag = itemView.findViewById(R.id.prj_item_image);
        }
    }

    /**
     * 格网显示子项布局
     */
    private class GridViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        ImageView tag;// 选中标记
        View view;

        public GridViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            name = itemView.findViewById(R.id.prj_item_filename);
            tag = itemView.findViewById(R.id.prj_item_image);
        }
    }
}

4、Java文件调用

    private void setProjectViewType(int type) {
        // 这里加一句配置文件记录用户习惯
        // ...
        mAdapter.setViewType(type);
        mRecyclerView.setAdapter(mAdapter);
        switch (type) {
        case GRID:
            mFormatBtn.setBackgroundResource(R.drawable.ic_grid_format_view_svg);
            mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
            break;
        case LIST:
            mFormatBtn.setBackgroundResource(R.drawable.ic_list_format_view_svg);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            break;
        }
    }

你可能感兴趣的:(RecyclerView 的切换横竖两种布局文件)