사용자 정의 View 의 GridView 단일 선택 금액 선택 Layout-Choose Money Layout
생각:
외부 컨트롤 은 GridView 를 사용 합 니 다.그 안에 아 이 템 마다 FrameLayout 를 놓 습 니 다.FrameLayout 에는 Checkbox 와 ImageView 가 있 습 니 다.chechBox 는 background 를 추가 하여 선택 효 과 를 실현 합 니 다.선택 배경 은 투명 하고 item 의 체크 아이콘 을 표시 합 니 다.checkbox 를 선택 하지 않 으 면 배경 이 있 고 선택 한 체크 를 가 립 니 다.GridView 를 다시 쓰 고 감청 과 데이터 가 잘 어 울 리 며 선택 한 데 이 터 를 인터페이스 로 되 돌려 줍 니 다.
코드:
ChooseMoneyLayout.java
public class ChooseMoneyLayout extends GridView {
private int[] moneyList = {}; //
private LayoutInflater mInflater;
private MyAdapter adapter; //
int defaultChoose = 0; //
public ChooseMoneyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setData();
}
public void setData() {
mInflater = LayoutInflater.from(getContext());
//
adapter = new MyAdapter();
setAdapter(adapter);
}
/**
* ,
* @param defaultChoose
*/
public void setDefaultPositon(int defaultChoose) {
this.defaultChoose = defaultChoose;
adapter.notifyDataSetChanged();
}
/**
*
* @param moneyData
*/
public void setMoneyData(int[] moneyData){
this.moneyList = moneyData;
}
class MyAdapter extends BaseAdapter {
private CheckBox checkBox;
@Override
public int getCount() {
return moneyList.length;
}
@Override
public Object getItem(int position) {
return moneyList[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
MyViewHolder holder;
if (convertView == null) {
holder = new MyViewHolder();
convertView = mInflater.inflate(R.layout.item_money_pay, parent, false);
holder.moneyPayCb = (CheckBox) convertView.findViewById(R.id.money_pay_cb);
convertView.setTag(holder);
} else {
holder = (MyViewHolder) convertView.getTag();
}
holder.moneyPayCb.setText(getItem(position) + " ");
holder.moneyPayCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//
buttonView.setTextColor(getResources().getColor(R.color.light_color_blue));
//
if (checkBox != null) {
checkBox.setChecked(false);
}
checkBox = (CheckBox) buttonView;
} else {
checkBox = null;
//
buttonView.setTextColor(getResources().getColor(R.color.darkgray));
}
//
listener.chooseMoney(position, isChecked, (Integer) getItem(position));
}
});
if (position == defaultChoose) {
defaultChoose = -1;
holder.moneyPayCb.setChecked(true);
checkBox = holder.moneyPayCb;
}
return convertView;
}
private class MyViewHolder {
private CheckBox moneyPayCb;
}
}
/**
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
private onChoseMoneyListener listener;
public void setOnChoseMoneyListener(onChoseMoneyListener listener) {
this.listener = listener;
}
public interface onChoseMoneyListener {
/**
*
*
* @param position gridView
* @param isCheck
* @param moneyNum
*/
void chooseMoney(int position, boolean isCheck, int moneyNum);
}
}
item_money_pay.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:descendantFocusability="blocksDescendants">
<!-- -->
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="right|bottom"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:maxHeight="9dp"
android:maxWidth="9dp"
android:scaleType="fitCenter"
android:src="@drawable/money_pay_type_choose" />
<CheckBox
android:id="@+id/money_pay_cb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/money_pay_selector"
android:button="@null"
android:gravity="center"
android:paddingBottom="2.5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="2.5dp"
android:textSize="20sp"
android:textColor="#ff777777"
/>
</FrameLayout>
CheckBox 의 background:moneypay_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/blue_border_noback_drawable"/>
<item android:state_selected="true" android:drawable="@drawable/blue_border_noback_drawable"/>
<item android:state_checked="true" android:drawable="@drawable/blue_border_noback_drawable"/>
<item >
<shape>
<solid android:color="#ffffffff"/>
<corners android:radius="5dp"/>
<stroke android:color="#ffbfbfbf"
android:width="1dp"/>
</shape>
</item>
</selector>
activity xml:
<com.minstone.view.ChooseMoneyLayout
android:id="@+id/money_chose_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:horizontalSpacing="17dp"
android:numColumns="3"
android:verticalSpacing="20dp" />
activity 코드:
private ChooseMoneyLayout moneyChoseMoney;
private int money; //
private void initData() {
//
moneyChoseMoney = (ChooseMoneyLayout)findViewById(R.id.money_chose_money);
//
moneyChoseMoney.setMoneyData(new int[]{30, 50, 100, 200, 300, 500,1000});
//
moneyChoseMoney.setDefaultPositon(3);
//
moneyChoseMoney.setOnChoseMoneyListener(new ChooseMoneyLayout.onChoseMoneyListener() {
@Override
public void chooseMoney(int position,boolean isCheck, int moneyNum) {
if(isCheck){
money = moneyNum;
ToastUtil.showCustomToast(PayActivity.this,money+"");
}else{
money = 0;
}
}
});
}
이 편 은 사용자 정의 View 의 GridView 단일 선택 금액 을 선택 하여 Layout-Choose Money Layout 를 선택 하 는 것 이 바로 편집장 이 여러분 에 게 공유 하 는 모든 내용 입 니 다.참고 하 시기 바 랍 니 다.여러분 의 많은 응원 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.