Skip to content

Commit

Permalink
fragment优化
Browse files Browse the repository at this point in the history
  • Loading branch information
AranAndroid009 committed Aug 20, 2021
1 parent 3db932c commit 302c6fe
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 12 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/aranandroid/customview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.aranandroid.customview.ui.ScrollActivity
import com.aranandroid.customview.ui.SquareActivity
import com.aranandroid.customview.ui.SquareMoreActivity
import kotlinx.android.synthetic.main.item_main.*
Expand All @@ -16,12 +17,13 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
val listView = findViewById<RecyclerView>(R.id.listview)
listView.layoutManager = LinearLayoutManager(this)
val mainAdapter = MainAdapter(arrayListOf("ShapeTextView","SquareMoreView"))
val mainAdapter = MainAdapter(arrayListOf("ShapeTextView","SquareMoreView","ScrollView"))
mainAdapter.setOnItemClickListener { adapter, view, position ->
val item = mainAdapter.getItem(position)
when(item){
"ShapeTextView" -> startActivity(Intent(this,SquareActivity::class.java))
"SquareMoreView" -> startActivity(Intent(this, SquareMoreActivity::class.java))
"ScrollView" -> startActivity(Intent(this, ScrollActivity::class.java))
}

}
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/com/aranandroid/customview/ui/ScrollActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.aranandroid.customview.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.RadioButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.aranandroid.customview.R
import com.aranandroid.customview.squareview.SquareTextView
import kotlinx.android.synthetic.main.activity_square_textview.*
import kotlinx.android.synthetic.main.item_main.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class ScrollActivity : AppCompatActivity() {
@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scroll)



GlobalScope.launch {
repeat(89) {

}
}

}


}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_scroll.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_square_moreview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@

<com.aranandroid.customview.fragment.FragmentTop
android:id="@+id/fragment_top"
app:scroll_control1="false"
android:layout_width="match_parent"
android:layout_height="500dp">
<com.aranandroid.customview.squareview.SquareRadioButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FragmentBottom(context: Context?, attrs: AttributeSet?) : LinearLayout(con

var radio: RadioGroup

var viewPager: ViewPager
var viewPager: ScrollControlViewPager


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

constructor(context: Context?) : this(context, null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FragmentTop(context: Context?, attrs: AttributeSet?) : LinearLayout(contex

var radio:RadioGroup

var viewPager: ViewPager
var viewPager: ScrollControlViewPager

var fragments: LinkedHashMap<Int,Fragment>? = null
set(value) {
Expand Down Expand Up @@ -70,6 +70,9 @@ class FragmentTop(context: Context?, attrs: AttributeSet?) : LinearLayout(contex
view = LayoutInflater.from(context).inflate(R.layout.fragment_top, this, true)
radio = view.findViewById(R.id.radio)
viewPager = view.findViewById(R.id.pager)

val obtainStyledAttributes = context?.obtainStyledAttributes(attrs, R.styleable.FragmentTop)
obtainStyledAttributes?.let { viewPager.DISABLE = it.getBoolean(R.styleable.FragmentTop_scroll_control1,true)}
}

constructor(context: Context?):this(context,null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import com.aranandroid.customview.R

/**
* 描述:
* -
* 创建人:ybr
* 创建时间:2021
*/
class NoScrollViewPager : ViewPager {
val DISABLE = false
class ScrollControlViewPager : ViewPager {
var DISABLE = false

constructor(context: Context?) : super(context!!) {}
constructor(context: Context?, attrs: AttributeSet?) : super(
context!!,
attrs
) {
}
) {}

override fun onInterceptTouchEvent(arg0: MotionEvent): Boolean {
override fun onInterceptTouchEvent(arg0: MotionEvent): Boolean {
return DISABLE && super.onInterceptTouchEvent(arg0)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.aranandroid.customview.scrollview

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Path
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.LinearLayout
import androidx.annotation.Nullable
import androidx.annotation.RequiresApi


class ScrollLinearLayout (
@Nullable context: Context?,
@Nullable attrs: AttributeSet?,
defStyleAttr: Int
) :
LinearLayout(context!!, attrs, defStyleAttr) {


private var lastX = 0
private var lastY = 0

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
override fun onTouchEvent(event: MotionEvent): Boolean {
val x = event.x.toInt()
val y = event.y.toInt()
when (event.action) {
MotionEvent.ACTION_DOWN -> {
lastX = x
lastY = y
}
MotionEvent.ACTION_UP -> {
val offX: Int = x - lastX
val offY: Int = y - lastY
animationScroll(getX() + offX, getY() + offY)
}
}
return true
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
fun animationScroll(dx: Float, dy: Float) {
val path = Path()
path.moveTo(dx, dy)
val objectAnimator = ObjectAnimator.ofFloat(this, "x", "y", path)
objectAnimator.start()
}
}
2 changes: 1 addition & 1 deletion customview/src/main/res/layout/fragment_bottom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints"></RadioGroup>

<androidx.viewpager.widget.ViewPager
<com.aranandroid.customview.fragment.ScrollControlViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
Expand Down
4 changes: 2 additions & 2 deletions customview/src/main/res/layout/fragment_top.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
android:orientation="horizontal"
tools:ignore="MissingConstraints">
</RadioGroup>
<androidx.viewpager.widget.ViewPager
<com.aranandroid.customview.fragment.ScrollControlViewPager
android:id="@+id/pager"
app:layout_constraintTop_toBottomOf="@+id/radio"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp">
</androidx.viewpager.widget.ViewPager>
</com.aranandroid.customview.fragment.ScrollControlViewPager>

</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 4 additions & 1 deletion customview/src/main/res/values/attrsbase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@
<!-- 是否单行-->
<attr name="single" format="boolean" />

<attr name="scroll_control1" format="boolean" />

<!-- 分组4:-->


<!-- 分组4:-->
<attr name="gravity_key" format="integer">
<!-- Push object to the top of its container, not changing its size. -->
<flag name="top" value="0x30" />
Expand Down
17 changes: 17 additions & 0 deletions customview/src/main/res/values/attrscroll.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<resources>




<declare-styleable name="FragmentTop">
<!-- 左边文字大小-->
<attr name="scroll_control1" />
</declare-styleable>

<declare-styleable name="FragmentBottom">
<!-- 左边文字大小-->
<attr name="scroll_control1" />
</declare-styleable>

</resources>

0 comments on commit 302c6fe

Please sign in to comment.