参考博客:https://www.jianshu.com/p/b4bb52cdbeb7
非常感谢上面这位博主的博客给了我非常好的灵感!!!
下面我就先贴一下效果图:
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="20dp"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="充值账户" /><EditTextandroid:id="@+id/edt_phone"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="充值号码"android:inputType="phone"android:maxLength="11" /></LinearLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_recharge"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:background="#fff" /><Buttonandroid:id="@+id/btn_recharge"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="充值"android:textSize="20sp" />
</LinearLayout>
activity代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private int[] ints = new int[]{1, 5, 10, 20, 50, 100};private ItemAdapter itemAdapter;private EditText edt_phone;private RecyclerView rv_recharge;private Button btn_recharge;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();GridLayoutManager layoutManager = new GridLayoutManager(this, 3);rv_recharge.setLayoutManager(layoutManager);itemAdapter = new ItemAdapter(ints);rv_recharge.setAdapter(itemAdapter);}private void initView() {edt_phone = (EditText) findViewById(R.id.edt_phone);rv_recharge = (RecyclerView) findViewById(R.id.rv_recharge);btn_recharge = (Button) findViewById(R.id.btn_recharge);btn_recharge.setOnClickListener(this);}private void submit() {// validateString phone = edt_phone.getText().toString().trim();if (TextUtils.isEmpty(phone)) {Toast.makeText(this, "充值号码不能为空", Toast.LENGTH_SHORT).show();return;}// TODO validate success, do somethingint index = itemAdapter.getPositionIndex();String money = ints[index] + "元";Toast.makeText(this, "充值账户:" + phone + " 充值金额:" + money, Toast.LENGTH_SHORT).show();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_recharge:submit();break;}}
}
adapter代码:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {private int[] ints;private int positionIndex = -1;public ItemAdapter(int[] ints) {this.ints = ints;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);final ViewHolder holder = new ViewHolder(view);holder.tv_item.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int position = holder.getAdapterPosition();positionIndex = position;Toast.makeText(view.getContext(), ints[position] + "元", Toast.LENGTH_SHORT).show();notifyDataSetChanged();}});return holder;}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {holder.tv_item.setText(ints[position] + "元");if (position == positionIndex) {//选中holder.tv_item.setTextColor(Color.WHITE);holder.tv_item.setBackgroundColor(Color.DKGRAY);} else {//未选中holder.tv_item.setTextColor(Color.BLACK);holder.tv_item.setBackgroundColor(Color.WHITE);}}@Overridepublic int getItemCount() {return ints.length;}static class ViewHolder extends RecyclerView.ViewHolder {View view;TextView tv_item;public ViewHolder(@NonNull View itemView) {super(itemView);this.view = itemView;tv_item = (TextView) view.findViewById(R.id.tv_item);}}//返回选中的idpublic int getPositionIndex() {return positionIndex;}
}
代码做的比较简洁,如有不合理的地方。欢迎指正,请多多指教
下面放了demo地址,因为考虑到有时候会有一些网路问题。所以就放了一个码云地址
GitHub demo地址:https://github.com/shI-215/RechargeApplication.git
码云demo地址:https://gitee.com/oji/RechargeApplication.git