values资源之array和RecyclerView的使用
来源:互联网 发布:淘宝车秒贷首付怎么算 编辑:程序博客网 时间:2024/06/05 00:57
一。项目values目录下array.xml
<?xml version="1.0" encoding="utf-8"?><resources> <array name="main_module"> <!--<item>@string/collection</item>--> <item>@string/pre_authorization</item> <item>@string/balance_query</item> <item>@string/transaction_query</item> <item>@string/print</item> <item>@string/sign_logon</item> <item>@string/sign_logout</item> <item>@string/revoke</item> <item>@string/qr_revoke</item> <item>@string/refunds</item> </array>
<!-- 这个没用,只是说明可以添加多个组 --> <array name="pre_authorization"> <item>@string/pre_authorization</item> <item>@string/pre_authorization_revoke</item> <item>@string/pre_authorization_complete</item> <item>@string/pre_authorization_complete_revoke</item> </array></resources>
values目录下string.xml
<resources>
<string name="pre_authorization">预授权</string>
<string name="balance_query">余额查询</string>
<string name="transaction_query">交易查询</string>
<string name="print">打印</string>
<string name="sign_logon">签到</string>
<string name="sign_logout">签退</string>
<string name="revoke">撤销</string>
<string name="qr_revoke">二维码撤销</string>
<string name="refunds">消费退货</string>
</resources>
MainActivity的布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="@dimen/space_2" /></RelativeLayout>
在代码里的调用,使用:
@Bind(R.id.recycler_main)//此处用的注解,也可以findViewById()...RecyclerView recyclerMain;
public void initData() {
}List<IndexEntity> itemList = new ArrayList<>();String[] nameList = getResources().getStringArray(R.array.main_module);for (String name : nameList) { if ((getString(R.string.pre_authorization)).equals(name)) {//icon_pre_authorization是资源图片的名 itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_pre_authorization"), name)); } if (getString(R.string.balance_query).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_balance_query"), name)); } if (getString(R.string.transaction_query).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_transaction_query"), name)); } if (getString(R.string.print).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_print"), name)); } if (getString(R.string.sign_logon).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_pos_logon"), name)); } if (getString(R.string.sign_logout).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_pos_logout"), name)); } if (getString(R.string.revoke).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_revoke"), name)); } if (getString(R.string.qr_revoke).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_qr_revoke"), name)); } if (getString(R.string.refunds).equals(name)) { itemList.add(new IndexEntity(Tools.getDrawableId(getActivity(), "icon_refunds"), name)); }}IndexAdapter adapter = new IndexAdapter(itemList); adapter.setOnItemClickListener(this); GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);// recyclerMain.addItemDecoration(new SpaceItemDecoration(Constants.SPACINGINPIXELS)); recyclerMain.setLayoutManager(manager); recyclerMain.setAdapter(adapter);
public class Tools {
/** * 获取图片地址 */public static int getDrawableId(Context context, String name) { return context.getResources().getIdentifier(name, "drawable", context.getPackageName());//"com.pax.usdk"}
}
public class IndexAdapter extends RecyclerView.Adapter { private List<IndexEntity> itemList; private OnItemClickListener onItemClickListener; public IndexAdapter(List<IndexEntity> itemList) { this.itemList = itemList; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index, parent, false);// int bodyHeight = (int) (Tools.getScreenHeight(parent.getContext()) * (1 - 0.05 - 0.214 - 0.071));// if (view.getLayoutParams() != null)// view.getLayoutParams().height = (int) (bodyHeight / (16.4 * 3 + 17.6) * 16.4); return new PreViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { PreViewHolder viewHolder = (PreViewHolder) holder; IndexEntity entity = itemList.get(position); viewHolder.imvPreImg.setBackgroundResource(entity.getItemImgPath()); viewHolder.tvPreName.setText(entity.getItemName()); viewHolder.itemView.setOnClickListener(v -> { if (onItemClickListener != null) onItemClickListener.onItemClick(position); } ); } @Override public int getItemCount() { return itemList != null ? itemList.size() : 0; } class PreViewHolder extends RecyclerView.ViewHolder { @Bind(R.id.imv_index_image) ImageView imvPreImg; @Bind(R.id.tv_index_name) TextView tvPreName; PreViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } //点击事件接口 public interface OnItemClickListener { void onItemClick(int position); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; }}
public class IndexEntity { private int itemImgPath; private String itemName; public IndexEntity() { } public IndexEntity(int itemImgPath, String itemName) { this.itemImgPath = itemImgPath; this.itemName = itemName; } public int getItemImgPath() { return itemImgPath; } public void setItemImgPath(int itemImgPath) { this.itemImgPath = itemImgPath; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; }}
阅读全文
0 0
- values资源之array和RecyclerView的使用
- values资源之dimens和space
- array资源的使用
- 资源Resource之values
- values资源之styles
- values资源之color
- 清理Android中无用的drawable资源和values资源
- RecyclerView的使用(3)之添加Header和Footer
- RecyclerView的使用(3)之添加Header和Footer
- Android之TabLayout+抽屉式效果和RecyclerView的使用
- RecyclerView和SwipeRefreshLayout的使用
- RecyclerView 的研究和使用
- RecyclerView的使用和样式
- RecyclerView和CardView的使用
- RecyclerView的使用和封装
- RecyclerView的使用和优化
- 如何使用array资源文件中定义的资源数组
- Android res/values/array的用法
- ROS RRT RRTstar的实现
- 基于Java手写web服务器(简易版)
- 一条命令重启挂掉的docker
- VC++6.0 MFC对话框操作MySQL数据库的各种问题
- C语言笔记 指针 数组
- values资源之array和RecyclerView的使用
- tomcat8.5 managerApp 页面加载问题
- BZOJ1041: [HAOI2008]圆上的整点
- 数据结构课程设计——CET-6报名管理系统
- Expression封装
- Linux下安装snort(ubuntu为例)
- easyUI结合的moment.js
- iOS 获取模拟器上的沙盒路径
- spring事务管理默认回滚异常