需求
除了修改LayoutManager的布局設置,還有修改資源文件的判斷
代碼
1怒见、project_history_grid_item.xml
<?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
<?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> {
// 項目顯示模式:網(wǎng)格/列表
public static final int GRID = 0;
public static final int LIST = 1;
// 當前選中項
private int mSelectIndex = -1;
// 默認為網(wǎng)格顯示
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) {// 格網(wǎng)顯示
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);
}
}
/**
* 格網(wǎng)顯示子項布局
*/
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;
}
}