美文网首页
kotlin解决Viewpager删除item后不能及时刷新

kotlin解决Viewpager删除item后不能及时刷新

作者: 凌子_b86d | 来源:发表于2018-08-27 11:24 被阅读0次

package com.rrd.ucredit.viewpager

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.view.ViewGroup

import android.support.v4.view.PagerAdapter

import android.view.View

import android.widget.LinearLayout

import android.widget.TextView

import android.support.v4.view.ViewPager

import android.util.Log

import android.widget.Toast

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(),View.OnClickListener {

var viewPagerView:ViewPager?=null

    private var adapter:ViewAdapter?=null

    var viewList=ArrayList()

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

initViewList()

initViewPager()

initEvent()

}

fun initEvent(){

tv_add.setOnClickListener(this)

tv_del.setOnClickListener(this)

}

fun initViewPager(){

vp_test.offscreenPageLimit=3

        adapter=ViewAdapter(viewList);

vp_test.adapter=adapter

    }

fun initViewList(){

var x=0

        viewList.clear()

//初始化4张图片

        while(x<=3)

{

viewList.add(createView(x))

x++

}

}

fun createView(index:Int) :View{

var view1:LinearLayout= LinearLayout(this)

view1.orientation=LinearLayout.VERTICAL

      if(index==0)

{

view1.setBackgroundColor(resources.getColor(R.color.color1))

}else if(index==1)

{

view1.setBackgroundColor(resources.getColor(R.color.color4))

}else if(index==2)

{

view1.setBackgroundColor(resources.getColor(R.color.color7))

}else if(index==3)

{

view1.setBackgroundColor(resources.getColor(R.color.color8))

}else if(index==4)

{

view1.setBackgroundColor(resources.getColor(R.color.color15))

}else{

view1.setBackgroundColor(resources.getColor(R.color.color17))

}

view1.layoutParams=LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)

var textView:TextView=TextView(this)

textView.text="第"+index+"张图"

      textView.setTextColor(resources.getColor(R.color.color6))

textView.textSize=13f

      view1.addView(textView)

return view1

}

override fun onClick(p0: View?) {

when(p0?.id)

{

R.id.tv_add->{

viewList.add(createView(viewList.size))

adapter?.notifyDataSetChanged()

}

R.id.tv_del->{

var index :Int=if(viewList.size>0)viewList.size-1 else -1

                if(index==-1)

{

Toast.makeText(this,"已经没有view只能添加了",Toast.LENGTH_LONG).show()

}else{

viewList.removeAt(index)

adapter?.notifyDataSetChanged()

}

}

}

}

internal inner class ViewAdapter(private val datas: List) : PagerAdapter() {

override fun getCount(): Int {

Log.e("MainActiviy","getCount"+datas.size)

return if (datas !=null)datas.size else 0

        }

override fun isViewFromObject(view: View, item: Any): Boolean {

var isView:Boolean=view === item;

Log.e("MainActiviy","iisViewFromObject"+isView)

return isView

}

override fun instantiateItem(container: ViewGroup, position: Int): Any {

val view =datas[position]

container.addView(view)

Log.e("MainActiviy","instantiateItem位置"+position)

return view

}

//高能预警 解决删除item不能刷新问题

        override fun destroyItem(container: ViewGroup, position: Int, item: Any) {

container.removeView(itemas View?)

Log.e("MainActiviy","destroyItem位置"+position)

}

//高能预警 解决删除item不能刷新问题

        override fun getItemPosition(itemView: Any?): Int {

if(datas.contains(itemView))

{

return datas.indexOf(itemView)

}else{

return POSITION_NONE

            }

}

}

}

布局文件

    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"

    android:orientation="vertical"

    tools:context="com.rrd.ucredit.viewpager.MainActivity">

        android:id="@+id/vp_test"

        android:layout_width="match_parent"

        android:layout_height="300dp">

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="30dp"

        android:orientation="horizontal">

            android:id="@+id/tv_add"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:background="@color/colorAccent"

            android:gravity="center"

            android:padding="20dp"

            android:text="添加viewPage"

            android:textColor="#ffffff"

            android:textSize="14sp" />

            android:id="@+id/tv_del"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_marginLeft="20dp"

            android:layout_weight="1"

            android:background="@color/colorAccent"

            android:gravity="center"

            android:padding="20dp"

            android:text="删除viewpager"

            android:textColor="#ffffff"

            android:textSize="14sp" />

相关文章

网友评论

      本文标题:kotlin解决Viewpager删除item后不能及时刷新

      本文链接:https://www.haomeiwen.com/subject/jtunyftx.html