有什么方法可以在(custom)ArrayAdapter中初始化videoview或playerview?如果没有其他方法?

时间:2019-06-02 17:47:34

标签: android kotlin

我正在制作像Cardview应用一样的火种。我正在将SwipeFlingAdapterView(用于滑动)与ArrayAdapter(生成项)一起使用。问题是我不知道如何制作视频。我尝试了播放器视图和视频视图。我刚黑屏。

该代码在viewpager上正常工作。但是我不喜欢滑动,所以我决定实现swipeFlingAdapterView。除视频部分外,其他所有内容(TextViews,图像)都可以正常工作。

这是videoView的一部分

xml文件

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
        tools:context=".ui.dating.DatingFragment1">
    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <androidx.viewpager.widget.ViewPager
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        <com.lorentzos.flingswipe.SwipeFlingAdapterView
                android:id="@+id/frame"
                android:background="#ffeee9e2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:rotation_degrees="15.5"
                tools:context=".MyActivity"/>


        <LinearLayout
                android:layout_width="match_parent"
                android:layout_marginTop="450dp"

                android:layout_height="200dp">

            <Button
                    android:id="@+id/dislike_btn"
                    android:layout_weight="1"
                    android:text="Dislike"
                    android:layout_margin="2dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            <Button
                    android:id="@+id/friend_btn"
                    android:text="Friend"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            <Button
                    android:id="@+id/like_btn"
                    android:text="Like"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>


        </LinearLayout>

    </FrameLayout>

</layout>

子文件

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <LinearLayout
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="40sp"
            android:paddingRight="40sp"
            android:paddingTop="20sp"
            android:paddingBottom="20sp"
            android:clipToPadding="false">


        <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:cardCornerRadius="4dp"
                android:elevation="2dp"
                android:id="@+id/cardView">
            <LinearLayout
                    android:layout_gravity="center"
                    android:layout_width="300dp"
                    android:layout_height="350dp"
                    android:orientation="vertical">
                <VideoView android:layout_width="match_parent"
                           android:layout_height="250dp"
                           android:id="@+id/video_player"
                />

                <TextView
                        android:id="@+id/name"
                        android:textSize="30sp"
                        android:paddingLeft="20sp"
                        android:textColor="@android:color/black"
                        android:gravity="center|left"
                        tools:text="hello"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"/>

            </LinearLayout>
        </androidx.cardview.widget.CardView>


    </LinearLayout>
</layout>

片段

class DatingFragment1 : Fragment() {
    private var arrayAdapter: ArrayAdapter<UserDating>? = null
    private var i: Int = 0
    private var users = mutableListOf<UserDating>()
    var thiscontext: Context? = null


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        thiscontext = container?.context
        val binding =
            DataBindingUtil.inflate<FragmentDating1Binding>(inflater, R.layout.fragment_dating1, container, false)

        val url7 = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4"
        users.add(UserDating("Z", url7, 25, "u9"))

        arrayAdapter = context?.let { ArrayAdapter2(it, R.layout.item, users) }

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {


        var flingContainer: SwipeFlingAdapterView = frame

        flingContainer!!.setAdapter(arrayAdapter)
        flingContainer!!.setFlingListener(object : SwipeFlingAdapterView.onFlingListener {
            override fun removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!")
                users!!.removeAt(0)
                arrayAdapter!!.notifyDataSetChanged()
            }

            override fun onLeftCardExit(dataObject: Any) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                Toast.makeText(context, "To the Left", Toast.LENGTH_SHORT).show()
            }

            override fun onRightCardExit(dataObject: Any) {
                Toast.makeText(context, "To the Right", Toast.LENGTH_SHORT).show()
            }

            override fun onAdapterAboutToEmpty(itemsInAdapter: Int) {
                // Ask for more data here
                // al!!.add("XML $i")
                arrayAdapter!!.notifyDataSetChanged()
                Log.d("LIST", "notified")
                i++
            }

            override fun onScroll(scrollProgressPercent: Float) {
            }

        })

    }


    class ArrayAdapter2(context: Context, resourceId: Int, items: List<UserDating>) :
        ArrayAdapter<UserDating>(context, resourceId, items) {

        internal var context: Context? = null
        private var playerView: PlayerView? = null
        private var player: PlayerManager? = null


        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var convertView = convertView
            val dating_item = getItem(position) as UserDating

            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false)
            }

            Log.d("LIST", "notified" + dating_item.url)
            val name = convertView!!.findViewById<View>(R.id.name)as?TextView
            var videoView = convertView.findViewById<View>(R.id.player_view)as?VideoView
            videoView?.setVideoPath(dating_item.url)
            videoView?.start()


            var link = dating_item.url
            println(dating_item.naam)
            Log.d("LIST", "notified")
            name?.text = dating_item.naam
            //player = PlayerManager(getContext(),link)
            //context?.let { player!!.init(it, playerView!!) }
            //        Picasso.get()
            //             .load("https://i.pinimg.com/736x/e2/3d/91/e23d917512db42d340300f6d899624b9.jpg")
            //            .into(image);
            return convertView
        }
    }
}

希望有人能弄清楚我在做什么错。

0 个答案:

没有答案