values资源之array和RecyclerView的使用

一。项目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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值