项目作者: RobertApikyan

项目描述 :
Android SegmentedControl + multi row support
高级语言: Java
项目地址: git://github.com/RobertApikyan/SegmentedControl.git
创建时间: 2017-09-14T14:33:56Z
项目社区:https://github.com/RobertApikyan/SegmentedControl

开源协议:Apache License 2.0

下载


Android SegmentedControl + multi row support + multi selection

minSdk API 14+

N|Solid

Demo App, Play store link

Or try demo App online !


License

Segmented control for Android, with a lot of customization properties

ScreenShots







Download

Gradle

Add to project level build.gradle

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Add dependency to app module level build.gradle

  1. dependencies {
  2. implementation 'com.github.RobertApikyan:SegmentedControl:1.2.0'
  3. }

Maven

  1. <repositories>
  2. <repository>
  3. <id>jitpack.io</id>
  4. <url>https://jitpack.io</url>
  5. </repository>
  6. </repositories>

Add dependency

  1. <dependency>
  2. <groupId>com.github.RobertApikyan</groupId>
  3. <artifactId>SegmentedControl</artifactId>
  4. <version>1.1.3</version>
  5. </dependency>

Done.

Simple usage in XML

  1. <segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControl
  2. android:id="@+id/segmented_control"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:layout_margin="8dp"
  6. app:columnCount="3"
  7. app:distributeEvenly="true"
  8. app:textVerticalPadding="6dp"
  9. app:radius="12dp"
  10. app:segments="@array/your_array_data" ></segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControl>

Attributes

  1. <attr name="supportedSelectionsCount" format="boolean" ></attr> setSupportedSelectionsCount(int)
  2. <attr name="reselectionEnabled" format="boolean" ></attr> setDistributeEvenly(boolean)
  3. <attr name="distributeEvenly" format="boolean" ></attr> setDistributeEvenly(boolean)
  4. <attr name="columnCount" format="integer" ></attr> setColumnCount(int)
  5. <attr name="segments" format="reference" ></attr> addSegments(Object[]), addSegments(List)
  6. <attr name="selectedStrokeColor" format="color" ></attr> setSelectedStrokeColor(int)
  7. <attr name="unSelectedStrokeColor" format="color" ></attr> setUnSelectedStrokeColor(int)
  8. <attr name="strokeWidth" format="dimension" / setStrokeWidth(int)
  9. <attr name="selectedBackgroundColor" format="color" ></attr> setSelectedBackgroundColor(int)
  10. <attr name="unSelectedBackgroundColor" format="color" ></attr> setUnSelectedBackgroundColor(int)
  11. <attr name="selectedTextColor" format="color"></attr> setSelectedTextColor(int)
  12. <attr name="unSelectedTextColor" format="color"></attr> setUnSelectedTextColor(int)
  13. <attr name="textSize" format="dimension"></attr> setTextSize(int)
  14. <attr name="selectionAnimationDuration" format="integer"></attr>
  15. <attr name="focusedBackgroundColor" format="color"></attr>
  16. <attr name="textHorizontalPadding" format="dimension"></attr> setTextHorizontalPadding(int)
  17. <attr name="textVerticalPadding" format="dimension"></attr> setTextVerticalPadding(int)
  18. <attr name="segmentVerticalMargin" format="dimension"></attr> setSegmentVerticalMargin(int)
  19. <attr name="segmentHorizontalMargin" format="dimension"></attr> setSegmentHorizontalMargin(int)
  20. <attr name="radius" format="dimension"></attr> setRadius(int)
  21. <attr name="topLeftRadius" format="dimension"></attr> setTopLeftRadius(int)
  22. <attr name="topRightRadius" format="dimension"></attr> setTopRightRadius(int)
  23. <attr name="bottomRightRadius" format="dimension"></attr> setBottomRightRadius(int)
  24. <attr name="bottomLeftRadius" format="dimension"></attr> setBottomLeftRadius(int)
  25. <attr name="radiusForEverySegment" format="boolean"></attr> setRadiusForEverySegment(boolean)
  26. <attr name="fontAssetPath" format="string"></attr> setTypeFace(TypeFace)

Note: After every configuration change call segmentedControl.notifyConfigIsChanged() method

Example

  1. segmentedControl.setStrokeWidth(width.intValue());
  2. segmentedControl.setColumnCount(columnCount);
  3. segmentedControl.notifyConfigIsChanged();

SegmentedControl uses SegmentAdapter and SegmentViewHolder for displaying segments. There are allready exist a default implementations for SegmentAdapter (SegmentAdapterImpl) and SegmentViewHolder (SegmentViewHolderImpl), but if you want to make your custom implementation… well here is the steps

  1. Define segment_item.xml inside layouts folder

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/text_view"
    7. android:layout_width="match_parent"
    8. android:layout_height="match_parent"
    9. android:layout_gravity="center"
    10. android:layout_margin="2dp"
    11. android:background="@color/colorPrimary"
    12. android:gravity="center"
    13. android:textColor="@color/colorAccent"
    14. android:textSize="14sp" ></TextView>
    15. </LinearLayout>
  2. Create a SegmentViewHolder instance (AppSegmentViewHolder) (here I define the segment generic data type as a String)

    1. public class AppSegmentViewHolder extends SegmentViewHolder<String> {
    2. TextView textView;
    3. public AppSegmentViewHolder(@NonNull View sectionView) {
    4. super(sectionView);
    5. textView = (TextView) sectionView.findViewById(R.id.text_view);
    6. }
    7. @Override
    8. protected void onSegmentBind(String segmentData) {
    9. textView.setText(segmentData);
    10. }
    11. }
  3. Create a SegmentAdapter instance

    1. public class AppSegmentAdapter extends SegmentAdapter<String, AppSegmentViewHolder> {
    2. @NonNull
    3. @Override
    4. protected AppSegmentViewHolder onCreateViewHolder(@NonNull LayoutInflater layoutInflater, ViewGroup viewGroup, int i) {
    5. return new AppSegmentViewHolder(layoutInflater.inflate(R.layout.item_segment, null));
    6. }
    7. }
  4. Pass the adapter to the segmentedControl

    1. segmentedControl = (SegmentedControl) findViewById(R.id.segmented_control);
    2. segmentedControl.setAdapter(new AppSegmentAdapter());
  5. Finally add segments data.

    1. segmentedControl.addSegments(getResources().getStringArray(R.array.segments));

    That’s it :)

For custom implementation use SegmentedControlUtils helper class in order to define segment background type and background radius.

View Robert Apikyan profile on LinkedIn

License

  1. Copyright 2017 Robert Apikyan
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.