Skip to content

Commit ebb03dc

Browse files
author
杨宏伟
committed
修复列表使用时,布局错乱的问题
增加列表演示
1 parent 13421da commit ebb03dc

File tree

11 files changed

+199
-12
lines changed

11 files changed

+199
-12
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ dependencies {
4242
testImplementation 'junit:junit:4.+'
4343
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
4444
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
45+
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
4546
}

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/Theme.TagLayout">
12+
<activity
13+
android:name=".ListActivity"
14+
android:exported="false"
15+
android:label="列表标签演示"/>
1216
<activity android:name=".MainActivity">
1317
<intent-filter>
1418
<action android:name="android.intent.action.MAIN" />
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.yhw.taglayout.sample
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.TextView
8+
import androidx.appcompat.app.AppCompatActivity
9+
import androidx.recyclerview.widget.RecyclerView
10+
import androidx.recyclerview.widget.RecyclerView.Adapter
11+
import androidx.recyclerview.widget.RecyclerView.ViewHolder
12+
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
13+
import com.yhw.taglayout.TagAdapter
14+
import com.yhw.taglayout.TagLayout
15+
16+
/**
17+
* 列表
18+
*/
19+
class ListActivity : AppCompatActivity() {
20+
private lateinit var mRecyclerView: RecyclerView
21+
private lateinit var mRefreshLayout: SwipeRefreshLayout
22+
private var mAdapter: ListAdapter? = null
23+
private val list1 =
24+
mutableListOf("陕西西安", "新疆乌鲁木齐", "北京天安门", "西安大雁塔", "秦始皇兵马俑", "内蒙古呼伦贝尔大草原", "山西大同")
25+
private val list2 = mutableListOf("红色", "黄色", "绿色", "紫色", "蓝色")
26+
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
setContentView(R.layout.activity_list)
30+
mRecyclerView = findViewById(R.id.recycler_view)
31+
mRefreshLayout = findViewById(R.id.refresh_layout)
32+
33+
mRefreshLayout.setOnRefreshListener {
34+
mRefreshLayout.isRefreshing = true
35+
getData()
36+
}
37+
getData()
38+
}
39+
40+
private fun getData() {
41+
val list = mutableListOf<Bean>()
42+
for (i in 0..100) {
43+
val tagList = mutableListOf<String>()
44+
if (i % 2 == 0) {
45+
for (s in list1) {
46+
tagList.add(s)
47+
}
48+
} else {
49+
for (s in list2) {
50+
tagList.add(s)
51+
}
52+
}
53+
val bean = Bean("", tagList)
54+
bean.name = "$i."
55+
bean.tagList = tagList
56+
list.add(bean)
57+
}
58+
if (mAdapter == null) {
59+
mAdapter = ListAdapter(list)
60+
mRecyclerView.adapter = mAdapter
61+
} else {
62+
mAdapter?.notifyDataSetChanged()
63+
}
64+
mRefreshLayout.isRefreshing = false
65+
}
66+
67+
class ListAdapter(private val list: List<Bean>) : Adapter<MyViewHolder>() {
68+
69+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
70+
val itemView = LayoutInflater.from(parent.context)
71+
.inflate(R.layout.list_item_layout, parent, false)
72+
return MyViewHolder(itemView)
73+
}
74+
75+
override fun getItemCount(): Int = list.size
76+
77+
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
78+
holder.textView.text = list[position].name
79+
holder.tagLayout.setAdapter(MyTagAdapter(list[position].tagList))
80+
}
81+
82+
}
83+
84+
class MyViewHolder(itemView: View) : ViewHolder(itemView) {
85+
val tagLayout: TagLayout = itemView.findViewById(R.id.tag_layout)
86+
val textView: TextView = itemView.findViewById(R.id.tv_text)
87+
}
88+
89+
class MyTagAdapter(private val dataList: List<String>) : TagAdapter() {
90+
override fun onCreateView(parent: ViewGroup): View {
91+
return LayoutInflater.from(parent.context)
92+
.inflate(R.layout.tag_item_layout, parent, false)
93+
}
94+
95+
override fun onBindView(itemView: View, position: Int) {
96+
val textView: TextView = itemView.findViewById(R.id.tv_title)
97+
textView.text = dataList[position]
98+
}
99+
100+
override fun getItemCount(): Int {
101+
return dataList.size
102+
}
103+
}
104+
105+
data class Bean(var name: String, var tagList: List<String>)
106+
}

app/src/main/java/com/yhw/taglayout/sample/MainActivity.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.yhw.taglayout.sample
22

3+
import android.content.Intent
34
import android.os.Bundle
45
import android.util.Log
5-
import android.view.LayoutInflater
6-
import android.view.View
7-
import android.view.ViewGroup
6+
import android.view.*
87
import android.widget.Button
98
import android.widget.TextView
109
import android.widget.Toast
@@ -100,4 +99,16 @@ class MainActivity : AppCompatActivity() {
10099
}
101100
}
102101

102+
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
103+
menuInflater.inflate(R.menu.menu_main, menu)
104+
return super.onCreateOptionsMenu(menu)
105+
}
106+
107+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
108+
if (item.itemId == R.id.list) {
109+
startActivity(Intent(this, ListActivity::class.java))
110+
}
111+
return super.onOptionsItemSelected(item)
112+
}
113+
103114
}
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+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".ListActivity">
8+
9+
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
10+
android:id="@+id/refresh_layout"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent">
13+
14+
<androidx.recyclerview.widget.RecyclerView
15+
android:id="@+id/recycler_view"
16+
android:layout_width="match_parent"
17+
android:layout_height="match_parent"
18+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
19+
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
20+
21+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/activity_main.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,24 @@
1818
android:layout_height="wrap_content"
1919
android:padding="10dp"
2020
android:layout_margin="10dp"
21-
android:text="正常" />
21+
android:text="正常"
22+
android:background="@drawable/tag_selector_bg"/>
2223

2324
<TextView
2425
android:layout_width="wrap_content"
2526
android:layout_height="wrap_content"
2627
android:padding="10dp"
2728
android:layout_margin="10dp"
28-
android:text="单选" />
29+
android:text="单选"
30+
android:background="@drawable/tag_selector_bg"/>
2931

3032
<TextView
3133
android:layout_width="wrap_content"
3234
android:layout_height="wrap_content"
3335
android:padding="10dp"
3436
android:layout_margin="10dp"
35-
android:text="多选" />
37+
android:text="多选"
38+
android:background="@drawable/tag_selector_bg"/>
3639
</com.yhw.taglayout.TagLayout>
3740

3841
<com.yhw.taglayout.TagLayout
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:padding="12dp">
7+
8+
<TextView
9+
android:id="@+id/tv_text"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
app:layout_constraintTop_toTopOf="parent"
13+
app:layout_constraintLeft_toLeftOf="parent"
14+
app:layout_constraintRight_toRightOf="parent"/>
15+
16+
<com.yhw.taglayout.TagLayout
17+
android:id="@+id/tag_layout"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:layout_marginTop="10dp"
21+
app:layout_constraintTop_toBottomOf="@id/tv_text"
22+
app:layout_constraintBottom_toBottomOf="parent"/>
23+
24+
<View
25+
android:layout_width="match_parent"
26+
android:layout_height="1dp"
27+
app:layout_constraintTop_toBottomOf="@id/tag_layout"
28+
android:background="#e0e0e0"/>
29+
30+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/tag_item_layout.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
android:layout_margin="10dp"
66
android:clickable="true"
77
android:focusable="true"
8+
android:background="@drawable/tag_selector_bg"
89
android:orientation="vertical">
910

1011
<TextView
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
<item
5+
android:id="@+id/list"
6+
android:title="列表"
7+
app:showAsAction="always" />
8+
</menu>

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish {
77
userOrg = 'yanghongwei'
88
groupId = 'com.yhw.library'
99
artifactId = 'taglayout'
10-
publishVersion = '1.0.1'
10+
publishVersion = '1.0.2'
1111
repoName="TagLayout"
1212
desc = 'Android TagLayout'
1313
website = 'https://github.com/LuckyCodeer/TagLayout'
@@ -19,8 +19,8 @@ android {
1919
defaultConfig {
2020
minSdkVersion 16
2121
targetSdkVersion 30
22-
versionCode 1
23-
versionName "1.0"
22+
versionCode 102
23+
versionName "1.0.2"
2424

2525
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2626
consumerProguardFiles "consumer-rules.pro"

0 commit comments

Comments
 (0)