欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Studio 2 - 6 RecyclerView多布局的实现

程序员文章站 2024-03-21 13:44:52
...

Android Studio 2 - 6 RecyclerView多布局的实现

布局

主布局

<?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=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

小布局

<?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"
    android:orientation="vertical">

    <!--android:scaleType="fitXY"-->
    <ImageView
        android:id="@+id/iv_item_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

主类

package com.example.day07recyclerview;

import android.content.Intent;
import android.media.audiofx.DynamicsProcessing;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.day07recyclerview.adapter.AdapterRV;
import com.example.day07recyclerview.entity.Bean;
import com.example.day07recyclerview.inter.MyItemOnClickListener;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private String urlStr = "https://www.apiopen.top/satinApi?type=1&page=2";

    private RecyclerView recyclerView_main;
    private List<Bean.DataBean> beanList = new ArrayList<>();
    private AdapterRV adapterRV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        OkGo.<String>get(urlStr).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String json = response.body();
                Log.i(TAG, "onSuccess: " + json);
                List<Bean.DataBean> data = new Gson().fromJson(json, Bean.class).getData();
                beanList.addAll(data);
                adapterRV.notifyDataSetChanged();
            }
        });
    }

    private void initView() {
        recyclerView_main = (RecyclerView) findViewById(R.id.recyclerView_main);
        adapterRV = new AdapterRV(this, beanList);
        recyclerView_main.setAdapter(adapterRV);
        //记得设置一种样式 不然为空白
//        recyclerView_main.setLayoutManager(new LinearLayoutManager(this));//线性布局
        recyclerView_main.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));//瀑布流

        adapterRV.setMyItemOnClickListener(new MyItemOnClickListener() {
            @Override
            public void ItemOnClick(int i) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                intent.putExtra("video",beanList.get(i).getVideouri());
                startActivity(intent);
            }
        });

    }
}

第二页

package com.example.day07recyclerview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.VideoView;

public class Main2Activity extends AppCompatActivity {

    private VideoView vvMain2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Intent intent = getIntent();
        String video = intent.getStringExtra("video");
        Toast.makeText(this, ""+video, Toast.LENGTH_SHORT).show();

        vvMain2 = (VideoView) findViewById(R.id.vv_main2);

        //设置资源
        vvMain2.setVideoPath(video); //网络或者本地职员
        //准备
        vvMain2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                //播放
                vvMain2.start();
            }
        });

    }
}

接口

package com.example.day07recyclerview.inter;

public interface MyItemOnClickListener {
    void ItemOnClick(int i);
}

适配器

package com.example.day07recyclerview.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.day07recyclerview.Main2Activity;
import com.example.day07recyclerview.R;
import com.example.day07recyclerview.entity.Bean;
import com.example.day07recyclerview.inter.MyItemOnClickListener;

import java.util.List;

//TODO 泛型加入自己的ViewHoder

public class AdapterRV extends RecyclerView.Adapter<AdapterRV.ViewHoderRV> {

    private Context context;
    private List<Bean.DataBean> beanList;
    private MyItemOnClickListener myItemOnClickListener;

    public void setMyItemOnClickListener(MyItemOnClickListener myItemOnClickListener) {
        this.myItemOnClickListener = myItemOnClickListener;
    }

    public AdapterRV(Context context, List<Bean.DataBean> beanList) {
        this.context = context;
        this.beanList = beanList;
    }

    //创建Viewhoder

    @NonNull
    @Override
    public ViewHoderRV onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_item, parent, false);
        return new ViewHoderRV(view);
    }

    //绑定数据

    @Override
    public void onBindViewHolder(@NonNull ViewHoderRV holder, final int position) {
        holder.textView.setText(beanList.get(position).getText());
        Glide.with(context).load(beanList.get(position).getImage1()).into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(context, Main2Activity.class);
//                context.startActivity(intent);
                myItemOnClickListener.ItemOnClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }


    //TODO 创建视图持有者
    class ViewHoderRV extends RecyclerView.ViewHolder{

        TextView textView;
        ImageView imageView;

        public ViewHoderRV(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_item_title);
            imageView = itemView.findViewById(R.id.iv_item_image);
        }
    }

}