Skip to content

Commit 302c6fe

Browse files
fragment优化
1 parent 3db932c commit 302c6fe

File tree

12 files changed

+135
-12
lines changed

12 files changed

+135
-12
lines changed

app/src/main/java/com/aranandroid/customview/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.os.Bundle
66
import android.widget.LinearLayout
77
import androidx.recyclerview.widget.LinearLayoutManager
88
import androidx.recyclerview.widget.RecyclerView
9+
import com.aranandroid.customview.ui.ScrollActivity
910
import com.aranandroid.customview.ui.SquareActivity
1011
import com.aranandroid.customview.ui.SquareMoreActivity
1112
import kotlinx.android.synthetic.main.item_main.*
@@ -16,12 +17,13 @@ class MainActivity : AppCompatActivity() {
1617
setContentView(R.layout.activity_main)
1718
val listView = findViewById<RecyclerView>(R.id.listview)
1819
listView.layoutManager = LinearLayoutManager(this)
19-
val mainAdapter = MainAdapter(arrayListOf("ShapeTextView","SquareMoreView"))
20+
val mainAdapter = MainAdapter(arrayListOf("ShapeTextView","SquareMoreView","ScrollView"))
2021
mainAdapter.setOnItemClickListener { adapter, view, position ->
2122
val item = mainAdapter.getItem(position)
2223
when(item){
2324
"ShapeTextView" -> startActivity(Intent(this,SquareActivity::class.java))
2425
"SquareMoreView" -> startActivity(Intent(this, SquareMoreActivity::class.java))
26+
"ScrollView" -> startActivity(Intent(this, ScrollActivity::class.java))
2527
}
2628

2729
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.aranandroid.customview.ui
2+
3+
import android.annotation.SuppressLint
4+
import android.app.Activity
5+
import android.os.Bundle
6+
import android.os.PersistableBundle
7+
import android.widget.RadioButton
8+
import android.widget.Toast
9+
import androidx.appcompat.app.AppCompatActivity
10+
import com.aranandroid.customview.R
11+
import com.aranandroid.customview.squareview.SquareTextView
12+
import kotlinx.android.synthetic.main.activity_square_textview.*
13+
import kotlinx.android.synthetic.main.item_main.*
14+
import kotlinx.coroutines.GlobalScope
15+
import kotlinx.coroutines.delay
16+
import kotlinx.coroutines.launch
17+
import kotlinx.coroutines.runBlocking
18+
19+
class ScrollActivity : AppCompatActivity() {
20+
@SuppressLint("WrongViewCast")
21+
override fun onCreate(savedInstanceState: Bundle?) {
22+
super.onCreate(savedInstanceState)
23+
setContentView(R.layout.activity_scroll)
24+
25+
26+
27+
GlobalScope.launch {
28+
repeat(89) {
29+
30+
}
31+
}
32+
33+
}
34+
35+
36+
}
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+
<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=".MainActivity">
8+
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797

9898
<com.aranandroid.customview.fragment.FragmentTop
9999
android:id="@+id/fragment_top"
100+
app:scroll_control1="false"
100101
android:layout_width="match_parent"
101102
android:layout_height="500dp">
102103
<com.aranandroid.customview.squareview.SquareRadioButton

customview/src/main/java/com/aranandroid/customview/fragment/FragmentBottom.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class FragmentBottom(context: Context?, attrs: AttributeSet?) : LinearLayout(con
2525

2626
var radio: RadioGroup
2727

28-
var viewPager: ViewPager
28+
var viewPager: ScrollControlViewPager
2929

3030

3131
var changeItme : (group: RadioGroup, checkedId: Int) -> Unit = {group, checkedId -> }
@@ -76,6 +76,9 @@ class FragmentBottom(context: Context?, attrs: AttributeSet?) : LinearLayout(con
7676
view = LayoutInflater.from(context).inflate(R.layout.fragment_bottom, this, true)
7777
radio = view.findViewById(R.id.radio)
7878
viewPager = view.findViewById(R.id.pager)
79+
val obtainStyledAttributes = context?.obtainStyledAttributes(attrs, R.styleable.FragmentBottom)
80+
obtainStyledAttributes?.let { viewPager.DISABLE = it.getBoolean(R.styleable.FragmentBottom_scroll_control1,true)}
81+
obtainStyledAttributes?.recycle()
7982
}
8083

8184
constructor(context: Context?) : this(context, null) {

customview/src/main/java/com/aranandroid/customview/fragment/FragmentTop.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class FragmentTop(context: Context?, attrs: AttributeSet?) : LinearLayout(contex
2424

2525
var radio:RadioGroup
2626

27-
var viewPager: ViewPager
27+
var viewPager: ScrollControlViewPager
2828

2929
var fragments: LinkedHashMap<Int,Fragment>? = null
3030
set(value) {
@@ -70,6 +70,9 @@ class FragmentTop(context: Context?, attrs: AttributeSet?) : LinearLayout(contex
7070
view = LayoutInflater.from(context).inflate(R.layout.fragment_top, this, true)
7171
radio = view.findViewById(R.id.radio)
7272
viewPager = view.findViewById(R.id.pager)
73+
74+
val obtainStyledAttributes = context?.obtainStyledAttributes(attrs, R.styleable.FragmentTop)
75+
obtainStyledAttributes?.let { viewPager.DISABLE = it.getBoolean(R.styleable.FragmentTop_scroll_control1,true)}
7376
}
7477

7578
constructor(context: Context?):this(context,null){

customview/src/main/java/com/aranandroid/customview/fragment/NoScrollViewPager.kt renamed to customview/src/main/java/com/aranandroid/customview/fragment/ScrollControlViewPager.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import android.content.Context
44
import android.util.AttributeSet
55
import android.view.MotionEvent
66
import androidx.viewpager.widget.ViewPager
7+
import com.aranandroid.customview.R
78

89
/**
910
* 描述:
1011
* -
1112
* 创建人:ybr
1213
* 创建时间:2021
1314
*/
14-
class NoScrollViewPager : ViewPager {
15-
val DISABLE = false
15+
class ScrollControlViewPager : ViewPager {
16+
var DISABLE = false
1617

1718
constructor(context: Context?) : super(context!!) {}
1819
constructor(context: Context?, attrs: AttributeSet?) : super(
1920
context!!,
2021
attrs
21-
) {
22-
}
22+
) {}
2323

24-
override fun onInterceptTouchEvent(arg0: MotionEvent): Boolean {
24+
override fun onInterceptTouchEvent(arg0: MotionEvent): Boolean {
2525
return DISABLE && super.onInterceptTouchEvent(arg0)
2626
}
2727

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.aranandroid.customview.scrollview
2+
3+
import android.animation.ObjectAnimator
4+
import android.content.Context
5+
import android.graphics.Path
6+
import android.os.Build
7+
import android.util.AttributeSet
8+
import android.view.MotionEvent
9+
import android.widget.LinearLayout
10+
import androidx.annotation.Nullable
11+
import androidx.annotation.RequiresApi
12+
13+
14+
class ScrollLinearLayout (
15+
@Nullable context: Context?,
16+
@Nullable attrs: AttributeSet?,
17+
defStyleAttr: Int
18+
) :
19+
LinearLayout(context!!, attrs, defStyleAttr) {
20+
21+
22+
private var lastX = 0
23+
private var lastY = 0
24+
25+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
26+
override fun onTouchEvent(event: MotionEvent): Boolean {
27+
val x = event.x.toInt()
28+
val y = event.y.toInt()
29+
when (event.action) {
30+
MotionEvent.ACTION_DOWN -> {
31+
lastX = x
32+
lastY = y
33+
}
34+
MotionEvent.ACTION_UP -> {
35+
val offX: Int = x - lastX
36+
val offY: Int = y - lastY
37+
animationScroll(getX() + offX, getY() + offY)
38+
}
39+
}
40+
return true
41+
}
42+
43+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
44+
fun animationScroll(dx: Float, dy: Float) {
45+
val path = Path()
46+
path.moveTo(dx, dy)
47+
val objectAnimator = ObjectAnimator.ofFloat(this, "x", "y", path)
48+
objectAnimator.start()
49+
}
50+
}

customview/src/main/res/layout/fragment_bottom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
app:layout_constraintBottom_toBottomOf="parent"
1717
tools:ignore="MissingConstraints"></RadioGroup>
1818

19-
<androidx.viewpager.widget.ViewPager
19+
<com.aranandroid.customview.fragment.ScrollControlViewPager
2020
android:id="@+id/pager"
2121
android:layout_width="match_parent"
2222
android:layout_height="0dp"

customview/src/main/res/layout/fragment_top.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
android:orientation="horizontal"
1515
tools:ignore="MissingConstraints">
1616
</RadioGroup>
17-
<androidx.viewpager.widget.ViewPager
17+
<com.aranandroid.customview.fragment.ScrollControlViewPager
1818
android:id="@+id/pager"
1919
app:layout_constraintTop_toBottomOf="@+id/radio"
2020
app:layout_constraintBottom_toBottomOf="parent"
2121
android:layout_width="match_parent"
2222
android:layout_height="0dp">
23-
</androidx.viewpager.widget.ViewPager>
23+
</com.aranandroid.customview.fragment.ScrollControlViewPager>
2424

2525
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)