From 302c6fe6b5c06c45f77f477515f102cf7b487590 Mon Sep 17 00:00:00 2001 From: AranAndroid009 <1321289731@qq.com> Date: Fri, 20 Aug 2021 11:26:11 +0800 Subject: [PATCH] =?UTF-8?q?fragment=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aranandroid/customview/MainActivity.kt | 4 +- .../customview/ui/ScrollActivity.kt | 36 +++++++++++++ app/src/main/res/layout/activity_scroll.xml | 8 +++ .../res/layout/activity_square_moreview.xml | 1 + .../customview/fragment/FragmentBottom.kt | 5 +- .../customview/fragment/FragmentTop.kt | 5 +- ...ViewPager.kt => ScrollControlViewPager.kt} | 10 ++-- .../scrollview/ScrollLinearLayout.kt | 50 +++++++++++++++++++ .../src/main/res/layout/fragment_bottom.xml | 2 +- .../src/main/res/layout/fragment_top.xml | 4 +- customview/src/main/res/values/attrsbase.xml | 5 +- customview/src/main/res/values/attrscroll.xml | 17 +++++++ 12 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/aranandroid/customview/ui/ScrollActivity.kt create mode 100644 app/src/main/res/layout/activity_scroll.xml rename customview/src/main/java/com/aranandroid/customview/fragment/{NoScrollViewPager.kt => ScrollControlViewPager.kt} (76%) create mode 100644 customview/src/main/java/com/aranandroid/customview/scrollview/ScrollLinearLayout.kt create mode 100644 customview/src/main/res/values/attrscroll.xml diff --git a/app/src/main/java/com/aranandroid/customview/MainActivity.kt b/app/src/main/java/com/aranandroid/customview/MainActivity.kt index a486875..ac6eda1 100644 --- a/app/src/main/java/com/aranandroid/customview/MainActivity.kt +++ b/app/src/main/java/com/aranandroid/customview/MainActivity.kt @@ -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.* @@ -16,12 +17,13 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) val listView = findViewById(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)) } } diff --git a/app/src/main/java/com/aranandroid/customview/ui/ScrollActivity.kt b/app/src/main/java/com/aranandroid/customview/ui/ScrollActivity.kt new file mode 100644 index 0000000..3578608 --- /dev/null +++ b/app/src/main/java/com/aranandroid/customview/ui/ScrollActivity.kt @@ -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) { + + } + } + + } + + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_scroll.xml b/app/src/main/res/layout/activity_scroll.xml new file mode 100644 index 0000000..d548e41 --- /dev/null +++ b/app/src/main/res/layout/activity_scroll.xml @@ -0,0 +1,8 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_square_moreview.xml b/app/src/main/res/layout/activity_square_moreview.xml index dd7f8a2..e502036 100644 --- a/app/src/main/res/layout/activity_square_moreview.xml +++ b/app/src/main/res/layout/activity_square_moreview.xml @@ -97,6 +97,7 @@ Unit = {group, checkedId -> } @@ -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) { diff --git a/customview/src/main/java/com/aranandroid/customview/fragment/FragmentTop.kt b/customview/src/main/java/com/aranandroid/customview/fragment/FragmentTop.kt index 1c87cd8..3be56c8 100644 --- a/customview/src/main/java/com/aranandroid/customview/fragment/FragmentTop.kt +++ b/customview/src/main/java/com/aranandroid/customview/fragment/FragmentTop.kt @@ -24,7 +24,7 @@ class FragmentTop(context: Context?, attrs: AttributeSet?) : LinearLayout(contex var radio:RadioGroup - var viewPager: ViewPager + var viewPager: ScrollControlViewPager var fragments: LinkedHashMap? = null set(value) { @@ -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){ diff --git a/customview/src/main/java/com/aranandroid/customview/fragment/NoScrollViewPager.kt b/customview/src/main/java/com/aranandroid/customview/fragment/ScrollControlViewPager.kt similarity index 76% rename from customview/src/main/java/com/aranandroid/customview/fragment/NoScrollViewPager.kt rename to customview/src/main/java/com/aranandroid/customview/fragment/ScrollControlViewPager.kt index cf17fb7..5b3269d 100644 --- a/customview/src/main/java/com/aranandroid/customview/fragment/NoScrollViewPager.kt +++ b/customview/src/main/java/com/aranandroid/customview/fragment/ScrollControlViewPager.kt @@ -4,6 +4,7 @@ import android.content.Context import android.util.AttributeSet import android.view.MotionEvent import androidx.viewpager.widget.ViewPager +import com.aranandroid.customview.R /** * 描述: @@ -11,17 +12,16 @@ import androidx.viewpager.widget.ViewPager * 创建人: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) } diff --git a/customview/src/main/java/com/aranandroid/customview/scrollview/ScrollLinearLayout.kt b/customview/src/main/java/com/aranandroid/customview/scrollview/ScrollLinearLayout.kt new file mode 100644 index 0000000..3fc000d --- /dev/null +++ b/customview/src/main/java/com/aranandroid/customview/scrollview/ScrollLinearLayout.kt @@ -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() + } +} \ No newline at end of file diff --git a/customview/src/main/res/layout/fragment_bottom.xml b/customview/src/main/res/layout/fragment_bottom.xml index 9ca5672..6738355 100644 --- a/customview/src/main/res/layout/fragment_bottom.xml +++ b/customview/src/main/res/layout/fragment_bottom.xml @@ -16,7 +16,7 @@ app:layout_constraintBottom_toBottomOf="parent" tools:ignore="MissingConstraints"> - - - + \ No newline at end of file diff --git a/customview/src/main/res/values/attrsbase.xml b/customview/src/main/res/values/attrsbase.xml index 46904f4..9096971 100644 --- a/customview/src/main/res/values/attrsbase.xml +++ b/customview/src/main/res/values/attrsbase.xml @@ -46,8 +46,11 @@ + - + + + diff --git a/customview/src/main/res/values/attrscroll.xml b/customview/src/main/res/values/attrscroll.xml new file mode 100644 index 0000000..c84423d --- /dev/null +++ b/customview/src/main/res/values/attrscroll.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file