美文网首页
RecyclerView 的切换横竖两种布局文件

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

作者: sssssss_ | 来源:发表于2021-07-14 17:23 被阅读0次

需求

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

代码

1、project_history_grid_item.xml

九宫格item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="88dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/zhd_theme_margin_10"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <FrameLayout
            android:id="@+id/prj_item_file_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true">
            <!--文件夹图-->
            <ImageView
                android:id="@+id/prj_item_file_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_file_documents" />
            <!--选中-->
            <ImageView
                android:id="@+id/prj_item_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:src="@drawable/ic_uav_radio_f" />
        </FrameLayout>

        <!--文件夹名称-->
        <TextView
            android:id="@+id/prj_item_filename"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/prj_item_file_layout"
            android:gravity="center_horizontal"
            android:text="1234"
            android:textColor="@color/dark_gray"
            android:textSize="12sp" />

    </RelativeLayout>
</LinearLayout>

2、project_history_list_item

列表Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <!--文件夹图-->
    <ImageView
        android:id="@+id/prj_item_file_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_file_documents" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/prj_info_layout"
            android:layout_width="wrap_content"
            android:layout_height="56dp"
            android:layout_alignParentStart="true"
            android:layout_marginStart="@dimen/zhd_theme_margin_10"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <!--文件夹名称-->
            <TextView
                android:id="@+id/prj_item_filename"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/dark_gray"
                android:textSize="14sp" />

            <!--最近使用时间-->
            <TextView
                android:id="@+id/prj_item_use_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textColor="@color/light_gray"
                android:textSize="12sp" />

        </LinearLayout>

        <!--剩余空间-->
        <TextView
            android:id="@+id/prj_item_size"
            android:layout_width="wrap_content"
            android:layout_height="56dp"
            android:layout_marginStart="@dimen/zhd_theme_margin_20"
            android:layout_toEndOf="@+id/prj_info_layout"
            android:gravity="center_vertical"
            android:textColor="@color/light_gray"
            android:textSize="12sp" />

        <!--选中-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="56dp"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="16dp"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/prj_item_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical" />

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

3、Adapter

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

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


    public HistoryProjectListAdapter(List<File> 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 的切换横竖两种布局文件

      本文链接:https://www.haomeiwen.com/subject/cbgypltx.html