recycler

主界面

package com.example.ap;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.example.ap.adapter.MyRecyclerAdapter;
import com.example.ap.bean.MyBean;
import com.example.ap.listener.MyListener;
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 RecyclerView rv;
    private MyRecyclerAdapter recyclerAdapter;
    private List<MyBean.DataBean> list;
    private MyListener listener;

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

        rv=findViewById(R.id.main_recyclerView);
        initView();
        initData();
    }

    private void initData() {
        OkGo.<String>get("https://www.apiopen.top/satinApi?type=1&page=3")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        MyBean myBean = new Gson().fromJson(response.body(), MyBean.class);
                        list.addAll(myBean.getData());
                        recyclerAdapter.notifyDataSetChanged();
                        recyclerAdapter.setListener(new MyListener() {
                            @Override
                            public void message(int position) {
                                Intent it = new Intent(MainActivity.this, PlayActivity.class);
                                it.putExtra("url",list.get(position).getVideouri());
                                startActivity(it);
                            }
                        });
                    }
                });
    }

    private void initView() {
        list=new ArrayList<>();
        recyclerAdapter=new MyRecyclerAdapter(this,list);
        rv.setAdapter(recyclerAdapter);
        rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
    }
}

对应的布局

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/main_recyclerView"
        >

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

视频播放界面

package com.example.ap;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.VideoView;

import java.util.Timer;
import java.util.TimerTask;

public class PlayActivity extends AppCompatActivity {
    private VideoView videoView;
    private SeekBar sb;
    private int duration;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        videoView=findViewById(R.id.play_videoView);
        sb=findViewById(R.id.play_seekBar);

        final String video = getIntent().getStringExtra("url");
        videoView.setVideoPath(video);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                videoView.start();
                duration= videoView.getDuration();
                sb.setMax(duration);
            }
        });

        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if( fromUser){
                    videoView.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if( videoView.getCurrentPosition()==videoView.getDuration()){
                    timer.cancel();
                }else{
                    sb.setProgress(videoView.getCurrentPosition());
                }
            }
        },0,300);
    }
}

对应的布局

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".PlayActivity">
    <VideoView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:id="@+id/play_videoView"
        />
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:id="@+id/play_seekBar"
        />
</RelativeLayout>

自定义接口实现接口回调

package com.example.ap.listener;

public interface MyListener {
    void message(int position);
}

自定义多布局适配器

package com.example.ap.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.ap.R;
import com.example.ap.bean.MyBean;
import com.example.ap.listener.MyListener;

import java.util.List;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder>{
    private Context context;
    private List<MyBean.DataBean> list;
    private MyListener listener;

    public void setListener(MyListener listener) {
        this.listener = listener;
    }

    public MyRecyclerAdapter(Context context, List<MyBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.my_layout,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
        Glide.with(context).load(list.get(i).getImage1()).into(myViewHolder.img);
        myViewHolder.text.setText(list.get(i).getText());
        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.message(i);
            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView text;
        private ImageView img;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            text=itemView.findViewById(R.id.layout_text);
            img=itemView.findViewById(R.id.layout_image);
        }
    }
}

发布了52 篇原创文章 · 获赞 3 · 访问量 1213
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览