Намерение не работает при нажатии, фрагменты к активности - PullRequest
0 голосов
/ 02 мая 2020

Может кто-нибудь мне помочь, проблема в том, что когда я использую намерения во фрагментах для перемещения действий, в коде вообще нет ошибок, но при нажатии он не перемещается к следующему действию, а остается в фрагменте фильма, а не движется вообще, в logcat нет ошибки

FilmFragment.kt

class FilmFragment : Fragment(){

private lateinit var filmViewModel: FilmViewModel

private lateinit var adapter: Adapter

companion object {

    @JvmStatic
    fun newInstance() =
        FilmFragment().apply {
            arguments = Bundle().apply {
                // putInt(ARG_COLUMN_COUNT, columnCount)
            }
        }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    arguments?.let {
        // columnCount = it.getInt(ARG_COLUMN_COUNT)
    }
}


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    filmViewModel =
        ViewModelProviders.of(this).get(FilmViewModel::class.java)
    val root = inflater.inflate(R.layout.fragment_film, container, false)
    val linearLayout: LinearLayout = root.findViewById(R.id.linear_home)
    filmViewModel.text.observe(viewLifecycleOwner, Observer {
        linearLayout.background
    })


    return root
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    val items = listOf(
        Model("Start Wars", "blalalal", R.drawable.film_1),
        Model("Conjuring", "Film bla bla", R.drawable.film_1),
        Model("Content 03", "Source", R.drawable.film_1),
        Model("Content 04", "Source", R.drawable.film_1),
        Model("Content 05", "Source", R.drawable.film_1),
        Model("Content 06", "Source", R.drawable.film_1),
        Model("Content 07", "Source", R.drawable.film_1),
        Model("Content 08", "Source", R.drawable.film_1),
        Model("Content 09", "Source", R.drawable.film_1),
        Model("Content 10", "Source", R.drawable.film_1)
    )

    adapter = Adapter()
    adapter.replaceItems(items)
    recylerViewFilm.adapter = adapter
}

Adapter.kt

class Adapter : RecyclerView.Adapter<Adapter.ViewHolder>(){
private var items = listOf<Model>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context)
        .inflate(R.layout.row_film, parent, false)
    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val item = items[position]


    holder.titleTv.text = item.title
    holder.descriptionTv.text = item.desc
    holder.imageTv.setImageResource(item.image)

    holder.itemView.apply {
        val model = items.get(position)
        var gTitle : String = model.title
        var gDescription : String = model.desc
        var gImageView : Int = model.image

        val intent = Intent(context, AnotherActivity::class.java)
        intent.putExtra("title",gTitle)
        intent.putExtra("desc",gDescription)
        intent.putExtra("image",gImageView)

        setOnClickListener {
            context.startActivity(intent)
        }
    }
}

fun replaceItems(items: List<Model>) {
    this.items = items
    notifyDataSetChanged()
}

override fun getItemCount(): Int = items.size

inner class ViewHolder(override val containerView: View) :
    RecyclerView.ViewHolder(containerView),
    LayoutContainer

}

film_fragment. xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/linear_home"
tools:context=".ui.film.FilmFragment"
android:paddingBottom="70dp">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recylerViewFilm"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</androidx.recyclerview.widget.RecyclerView>

Activity_another. xml

<RelativeLayout 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"
android:padding="16dp"
android:gravity="center"
tools:context=".ui.isifilm.AnotherActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="120dp"
    android:layout_height="150dp"
    android:paddingStart="0dp"
    android:scaleType="fitXY"
    android:src="@mipmap/ic_launcher"
    android:layout_centerHorizontal="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/a_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Title"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="22sp"
    android:textStyle="bold"
    android:layout_below="@id/imageView"/>

<TextView
    android:id="@+id/a_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/a_title"
    android:layout_marginTop="10dp"
    android:text="Description"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="18sp" />

AnotherActivity.kt

class AnotherActivity : AppCompatActivity(){

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_another)

    val actionBar : ActionBar? =supportActionBar
    actionBar!!.setDisplayHomeAsUpEnabled(true)
    actionBar!!.setDisplayShowHomeEnabled(true)

    val intent = intent
    val aTitle = intent.getStringExtra("title")
    val aDescription = intent.getStringExtra("desc")
    val aImageView = intent.getIntExtra("image",0)

    actionBar.setTitle(aTitle)
    a_title.text = aTitle
    a_description.text = aDescription
    imageView.setImageResource(aImageView)
}
...