Kotlin实现的获取数据

interface ApiService {
    //http://blog.zhaoliang5156.cn/api/movie/in_theaters.json
    @GET("api/movie/in_theaters.json")
    fun getList():Observable<JavaBean>
}
class MyApp:Application() {
    override fun onCreate() {
        super.onCreate()
        Fresco.initialize(this)
    }

}

//网络工具类

object OkHttpUtils {
    val retrofit=Retrofit.Builder()
        .baseUrl("http://blog.zhaoliang5156.cn/")
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .client(OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().apply {
                setLevel(HttpLoggingInterceptor.Level.BODY)
            }).build())
        .build()
    val apiService= retrofit.create(ApiService::class.java)

}

//适配器

class Zzsy_Adapter(val subjects: List<Subject>) :RecyclerView.Adapter<Zzsy_Adapter.ViewHolder>(){
    class ViewHolder(itemView : View) :RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)=ViewHolder(View.inflate(parent.context,
        R.layout.zzsy_item,null))
    override fun getItemCount()=subjects.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        subjects[position].let {
            holder.itemView.apply {
                textView_Name.text=it.original_title
                textView_SHI.text=it.mainland_pubdate
                textView_LX.text=it.subtype
                simple_id.setImageURI(it.images.small)
                simple_01.setImageURI(it.casts[0].avatars.small)
                simple_02.setImageURI(it.casts[1].avatars.small)
                simple_03.setImageURI(it.casts[2].avatars.small)


            }
        }


    }


}

//正在上映页面数据

class ZzsyFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_zzsy, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        recyclerView_Zzsy.layoutManager=LinearLayoutManager(context)
        OkHttpUtils.apiService.getList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                recyclerView_Zzsy.adapter=Zzsy_Adapter(it.subjects)
            }


    }

    companion object{
        @JvmStatic
        fun newInstance()=ZzsyFragment()
    }
}

//MainActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val zzsyFragment=ZzsyFragment()
        val jjsyFragment= JjsyFragment()
        val list= listOf(zzsyFragment,jjsyFragment)
        viewPager_id.adapter=ViewpageAdapter(list,supportFragmentManager)
        radioGroup_id.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.radioButton_Zzsy->viewPager_id.setCurrentItem(0)
                R.id.radioButton_Jjsy->viewPager_id.setCurrentItem(1)
            }

        }


        viewPager_id.setOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {}
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) =radioGroup_id.check(radioGroup_id.getChildAt(position).id)

        })


    }

    class ViewpageAdapter(var fmlist:List<Fragment>,fm: FragmentManager?):
        FragmentStatePagerAdapter(fm!!){
        override fun getItem(position: Int): Fragment =fmlist[position]
        override fun getCount(): Int =fmlist.size

    }


}


//页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".view.activity.MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="电影-北京"
        android:gravity="center"

        />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:id="@+id/radioGroup_id"
        >
        <RadioButton
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:button="@null"
            android:text="正在上映"
            android:gravity="center"
            android:id="@+id/radioButton_Zzsy"
            />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:button="@null"
            android:text="即将上映"
            android:gravity="center"
            android:id="@+id/radioButton_Jjsy"
            />

    </RadioGroup>

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager_id"
        />



</LinearLayout>

在这里插入代码片



<?xml version="1.0" encoding="utf-8"?>

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView_Zzsy"
    />
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <com.facebook.drawee.view.SimpleDraweeView
            android:layout_width="100dp"
            android:layout_height="200dp"
            app:placeholderImage="@mipmap/ic_launcher"
            app:failureImage="@mipmap/ic_launcher"
            android:id="@+id/simple_id"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="电影名称"
                android:id="@+id/textView_Name"
                android:layout_marginTop="20dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="电影类型"
                android:id="@+id/textView_LX"
                android:layout_marginTop="20dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="上映时间"
                android:id="@+id/textView_SHI"
                android:layout_marginTop="20dp"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="主演"
                    android:id="@+id/textView_ZY"
                    android:layout_marginTop="20dp"
                    />
                <com.facebook.drawee.view.SimpleDraweeView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    app:placeholderImage="@mipmap/ic_launcher"
                    app:failureImage="@mipmap/ic_launcher"
                    app:roundAsCircle="true"
                    android:id="@+id/simple_01"
                    android:layout_marginLeft="20dp"
                    />

                <com.facebook.drawee.view.SimpleDraweeView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    app:placeholderImage="@mipmap/ic_launcher"
                    app:failureImage="@mipmap/ic_launcher"
                    app:roundAsCircle="true"
                    android:id="@+id/simple_02"
                    android:layout_marginLeft="20dp"
                    />

                <com.facebook.drawee.view.SimpleDraweeView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    app:placeholderImage="@mipmap/ic_launcher"
                    app:failureImage="@mipmap/ic_launcher"
                    app:roundAsCircle="true"
                    android:id="@+id/simple_03"
                    android:layout_marginLeft="20dp"
                    />

            </LinearLayout>
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值