[android] Implemented interactive counter view for rooms/guests picker

This commit is contained in:
alexzatsepin 2020-08-16 22:18:16 +03:00 committed by Tatiana Yan
parent ece28ddca8
commit fc1ac24b99
3 changed files with 208 additions and 2 deletions

View file

@ -1,4 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout
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"></RelativeLayout>
android:layout_height="wrap_content"
android:padding="@dimen/margin_base">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
tools:src="@drawable/ic_dropdown" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/margin_double"
android:layout_toEndOf="@id/icon"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?fontBody1"
tools:text="Title" />
<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?fontBody2"
tools:text="Subtitle" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_base_plus"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageButton
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:src="@drawable/ic_search" />
<TextView
android:id="@+id/counter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textAppearance="?fontBody1"
tools:text="1" />
<ImageButton
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:src="@drawable/ic_search" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

View file

@ -107,4 +107,11 @@
<attr name="showSale" format="boolean" />
<attr name="progressColor" />
</declare-styleable>
<declare-styleable name="InteractiveCounterView">
<attr name="android:src" />
<attr name="minValue" format="integer" />
<attr name="maxValue" format="integer" />
<attr name="title" format="string" />
<attr name="subtitle" format="string" />
</declare-styleable>
</resources>

View file

@ -0,0 +1,135 @@
package com.mapswithme.maps.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;
public class InteractiveCounterView extends RelativeLayout
{
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private View mMinusView;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private View mPlusView;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private TextView mCounterView;
@Nullable
private String mTitle;
@Nullable
private String mSubtitle;
private int mMinValue;
private int mMaxValue;
@DrawableRes
private int mIconRes;
@NonNull
private final OnClickListener mPlusClickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
int value = getCurrentValue();
mCounterView.setText(String.valueOf(++value));
updateConstraints();
}
};
@NonNull
private final OnClickListener mMinusClickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
int value = getCurrentValue();
mCounterView.setText(String.valueOf(--value));
updateConstraints();
}
};
public InteractiveCounterView(Context context)
{
super(context);
}
public InteractiveCounterView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs);
}
public InteractiveCounterView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(attrs);
}
public InteractiveCounterView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init(@Nullable AttributeSet attrs)
{
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.InteractiveCounterView);
try
{
mMinValue = a.getInt(R.styleable.InteractiveCounterView_minValue, 0);
mMaxValue = a.getInt(R.styleable.InteractiveCounterView_maxValue, Integer.MAX_VALUE);
mIconRes = a.getResourceId(R.styleable.InteractiveCounterView_android_src, UiUtils.NO_ID);
mTitle = a.getString(R.styleable.InteractiveCounterView_title);
mSubtitle = a.getString(R.styleable.InteractiveCounterView_subtitle);
LayoutInflater.from(getContext()).inflate(R.layout.interactive_counter, this, true);
}
finally
{
a.recycle();
}
}
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
ImageView iconView = findViewById(R.id.icon);
iconView.setImageResource(mIconRes);
TextView titleView = findViewById(R.id.title);
UiUtils.showIf(!TextUtils.isEmpty(mTitle), titleView);
titleView.setText(mTitle);
TextView subtitleView = findViewById(R.id.subtitle);
UiUtils.showIf(!TextUtils.isEmpty(mSubtitle), subtitleView);
subtitleView.setText(mSubtitle);
mMinusView = findViewById(R.id.minus);
mMinusView.setOnClickListener(mMinusClickListener);
mPlusView = findViewById(R.id.plus);
mPlusView.setOnClickListener(mPlusClickListener);
mCounterView = findViewById(R.id.counter);
mCounterView.setText(String.valueOf(mMinValue));
updateConstraints();
}
public int getCurrentValue()
{
return Integer.parseInt(mCounterView.getText().toString());
}
private void updateConstraints()
{
int value = getCurrentValue();
mMinusView.setEnabled(value != mMinValue);
mPlusView.setEnabled(value != mMaxValue);
}
}