Skip to content

Commit 828f72a

Browse files
committed
首次提交
1 parent 0f77ffb commit 828f72a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1419
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
*.iml
2+
*.idea
3+
.gradle
4+
/local.properties
5+
/.idea/caches
6+
/.idea/libraries
7+
/.idea/modules.xml
8+
/.idea/workspace.xml
9+
/.idea/navEditor.xml
10+
/.idea/assetWizardSettings.xml
11+
.DS_Store
12+
/build
13+
/captures
14+
.externalNativeBuild
15+
.cxx
16+
local.properties

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# TagLayout
2+
Android上的标签布局、流式布局,支持单选、多选等操作
3+
4+
### 引入依赖
5+
6+
```kotlin
7+
implementation 'com.yhw.library:taglayout:1.0.0'
8+
```
9+
10+
### 使用方法
11+
12+
#### 布局文件使用
13+
```xml
14+
<com.yhw.taglayout.TagLayout
15+
android:id="@+id/tag_layout_1"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
app:choiceMode="singleChoice">
19+
20+
<TextView
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:padding="10dp"
24+
android:layout_margin="10dp"
25+
android:text="正常" />
26+
27+
<TextView
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:padding="10dp"
31+
android:layout_margin="10dp"
32+
android:text="单选" />
33+
34+
<TextView
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:padding="10dp"
38+
android:layout_margin="10dp"
39+
android:text="多选" />
40+
</com.yhw.taglayout.TagLayout>
41+
```
42+
43+
```xml
44+
<com.yhw.taglayout.TagLayout
45+
android:id="@+id/tag_layout_2"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
app:defaultChoicePosition="0"
49+
app:choiceMode="none" />
50+
```
51+
52+
app:defaultChoicePosition="0"<!--单选时默认选中项-->
53+
app:choiceMode="none" /><!--设置选择模式,支持单选(singleChoice)和多选(multipleChoice) 默认(none)表示不设置选择模式-->
54+
55+
56+
#### 动态添加数据
57+
```kotlin
58+
val list = mutableListOf("陕西西安", "新疆乌鲁木齐", "北京天安门", "西安大雁塔", "秦始皇兵马俑", "内蒙古呼伦贝尔大草原", "山西大同")
59+
tagLayout.setAdapter(MyAdapter(list))
60+
```
61+
#### 其它事件操作
62+
```kotlin
63+
//单击事件
64+
tagLayout2.onItemClickListener = object : TagLayout.OnItemClickListener {
65+
override fun onItemClick(position: Int, view: View) {
66+
Toast.makeText(this@MainActivity, "点击了 $position", Toast.LENGTH_SHORT).show()
67+
}
68+
}
69+
//长按事件
70+
tagLayout2.onItemLongClickListener = object : TagLayout.OnItemLongClickListener {
71+
override fun onItemLongClick(position: Int, view: View) {
72+
Toast.makeText(this@MainActivity, "长按了 $position", Toast.LENGTH_SHORT).show()
73+
}
74+
}
75+
//单选监听
76+
tagLayout2.onSingleCheckedChangeListener =
77+
object : TagLayout.OnSingleCheckedChangeListener {
78+
override fun onCheckedChanged(position: Int) {
79+
Log.i(TAG, "选中了 $position")
80+
}
81+
}
82+
//多选监听
83+
tagLayout2.onMultipleCheckedChangeListener =
84+
object : TagLayout.OnMultipleCheckedChangeListener {
85+
override fun onCheckedChanged(positionList: MutableList<Int>) {
86+
for (position in positionList) {
87+
Log.i(TAG, "选中了 $position")
88+
}
89+
}
90+
}
91+
92+
//获取选中项
93+
getIndexBtn.setOnClickListener{
94+
if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.SingleChoice.choiceMode){
95+
Toast.makeText(this@MainActivity, "单选了 ${tagLayout2.getCheckedPosition()}", Toast.LENGTH_SHORT).show()
96+
}else if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.MultipleChoice.choiceMode){
97+
Toast.makeText(this@MainActivity, "多选了 ${tagLayout2.getCheckedList()}", Toast.LENGTH_SHORT).show()
98+
}else{
99+
Toast.makeText(this@MainActivity, "非选中模式,不能获取", Toast.LENGTH_SHORT).show()
100+
}
101+
}
102+
```
103+
104+
**\*示例代码采用Kotlin语言编写,如果你的项目是Java,用法与Kotlin基本类似**

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdkVersion 30
8+
buildToolsVersion "30.0.3"
9+
10+
defaultConfig {
11+
applicationId "com.yhw.taglayout.sample"
12+
minSdkVersion 16
13+
targetSdkVersion 30
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = '1.8'
32+
}
33+
}
34+
35+
dependencies {
36+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
37+
implementation 'androidx.core:core-ktx:1.3.2'
38+
implementation 'androidx.appcompat:appcompat:1.2.0'
39+
implementation 'com.google.android.material:material:1.3.0'
40+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
41+
implementation project(path: ':library')
42+
testImplementation 'junit:junit:4.+'
43+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
44+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
45+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.yhw.taglayout.sample
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.yhw.taglayout", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.yhw.taglayout.sample">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.TagLayout">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.yhw.taglayout.sample
2+
3+
import android.os.Bundle
4+
import android.util.Log
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import android.widget.Button
9+
import android.widget.TextView
10+
import android.widget.Toast
11+
import androidx.appcompat.app.AppCompatActivity
12+
import com.yhw.taglayout.TagAdapter
13+
import com.yhw.taglayout.TagLayout
14+
15+
const val TAG = "MainActivity"
16+
17+
class MainActivity : AppCompatActivity() {
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
setContentView(R.layout.activity_main)
21+
22+
val tagLayout1 = findViewById<TagLayout>(R.id.tag_layout_1)
23+
val tagLayout2 = findViewById<TagLayout>(R.id.tag_layout_2)
24+
val getIndexBtn = findViewById<Button>(R.id.btn_get_index)
25+
26+
tagLayout1.onItemClickListener = object : TagLayout.OnItemClickListener {
27+
override fun onItemClick(position: Int, view: View) {
28+
when (position) {
29+
0 -> tagLayout2.setChoiceMode(TagLayout.ChoiceMode.None)
30+
1 -> tagLayout2.setChoiceMode(TagLayout.ChoiceMode.SingleChoice)
31+
2 -> tagLayout2.setChoiceMode(TagLayout.ChoiceMode.MultipleChoice)
32+
}
33+
}
34+
}
35+
36+
val list = mutableListOf("陕西西安", "新疆乌鲁木齐", "北京天安门", "西安大雁塔", "秦始皇兵马俑", "内蒙古呼伦贝尔大草原", "山西大同")
37+
tagLayout2.setAdapter(MyAdapter(list))
38+
//单击事件
39+
tagLayout2.onItemClickListener = object : TagLayout.OnItemClickListener {
40+
override fun onItemClick(position: Int, view: View) {
41+
Toast.makeText(this@MainActivity, "点击了 $position", Toast.LENGTH_SHORT).show()
42+
}
43+
}
44+
//长按事件
45+
tagLayout2.onItemLongClickListener = object : TagLayout.OnItemLongClickListener {
46+
override fun onItemLongClick(position: Int, view: View) {
47+
Toast.makeText(this@MainActivity, "长按了 $position", Toast.LENGTH_SHORT).show()
48+
}
49+
}
50+
//单选监听
51+
tagLayout2.onSingleCheckedChangeListener =
52+
object : TagLayout.OnSingleCheckedChangeListener {
53+
override fun onCheckedChanged(position: Int) {
54+
Log.i(TAG, "选中了 $position")
55+
}
56+
}
57+
//多选监听
58+
tagLayout2.onMultipleCheckedChangeListener =
59+
object : TagLayout.OnMultipleCheckedChangeListener {
60+
override fun onCheckedChanged(positionList: MutableList<Int>) {
61+
for (position in positionList) {
62+
Log.i(TAG, "选中了 $position")
63+
}
64+
}
65+
}
66+
67+
//获取选中项
68+
getIndexBtn.setOnClickListener{
69+
if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.SingleChoice.choiceMode){
70+
Toast.makeText(this@MainActivity, "单选了 ${tagLayout2.getCheckedPosition()}", Toast.LENGTH_SHORT).show()
71+
}else if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.MultipleChoice.choiceMode){
72+
Toast.makeText(this@MainActivity, "多选了 ${tagLayout2.getCheckedList()}", Toast.LENGTH_SHORT).show()
73+
}else{
74+
Toast.makeText(this@MainActivity, "非选中模式,不能获取", Toast.LENGTH_SHORT).show()
75+
}
76+
}
77+
}
78+
79+
class MyAdapter(private val dataList: MutableList<String>) : TagAdapter() {
80+
override fun onCreateView(parent: ViewGroup): View {
81+
return LayoutInflater.from(parent.context)
82+
.inflate(R.layout.tag_item_layout, parent, false)
83+
}
84+
85+
override fun onBindView(itemView: View, position: Int) {
86+
val textView: TextView = itemView.findViewById(R.id.tv_title)
87+
textView.text = dataList[position]
88+
}
89+
90+
override fun getItemCount(): Int {
91+
return dataList.size
92+
}
93+
}
94+
95+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
8+
<aapt:attr name="android:fillColor">
9+
<gradient
10+
android:endX="85.84757"
11+
android:endY="92.4963"
12+
android:startX="42.9492"
13+
android:startY="49.59793"
14+
android:type="linear">
15+
<item
16+
android:color="#44000000"
17+
android:offset="0.0" />
18+
<item
19+
android:color="#00000000"
20+
android:offset="1.0" />
21+
</gradient>
22+
</aapt:attr>
23+
</path>
24+
<path
25+
android:fillColor="#FFFFFF"
26+
android:fillType="nonZero"
27+
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
28+
android:strokeWidth="1"
29+
android:strokeColor="#00000000" />
30+
</vector>

0 commit comments

Comments
 (0)