Я делаю трут, как приложение для просмотра карт. Я использую SwipeFlingAdapterView (для пролистывания) с ArrayAdapter (создание элементов). Проблема в том, что я не знаю, как заставить работать видео. Я попробовал playerview и videoview. Я просто получаю черный экран.
Код отлично работает с 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
}
}
}
Надеюсь, кто-то может понять, что я делаю неправильно.