android仿空间短视频播放

rxjava+retrofit+节操


依赖:

    

//网络请求框架Retrofit
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
//RecyclerView
compile 'com.android.support:recyclerview-v7:27.1.1'
//图片加载框架glide
compile 'com.github.bumptech.glide:glide:3.7.0'
//节操第三方
implementation 'fm.jiecao:jiecaovideoplayer:5.5'
//图片加载框架fresco
compile 'com.facebook.fresco:fresco:0.12.0'


public class Api {
    public static final String GETAD = "https://www.apiopen.top/";
}

public interface MyServer {
    //@GET("satinApi")
   // Call<UserBean> getPicData(@Query("type")int type, @Query("page") int num);
    @GET("satinApi")
    Call<VideoBean> getVideoData(@Query("type")int type, @Query("page") int num);
}




public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {
    private List<VideoBean.DataBean> data;
    private Context context;


    public VideoAdapter(List<VideoBean.DataBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mTextPic.setText(data.get(position).getText());

        RoundingParams roundingParams = RoundingParams.fromCornersRadius(15f);
        roundingParams.setRoundAsCircle(true);
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder.setFadeDuration(300).setRoundingParams(roundingParams).build();
        holder.mImageHead.setHierarchy(hierarchy);
        holder.mImageHead.setImageURI(data.get(position).getProfile_image());
        holder.mTextName.setText(data.get(position).getScreen_name());
        holder.mTextChen.setText(data.get(position).getTheme_name());
        holder.jiecao_Player.setUp(data.get(position).getVideouri(),holder.jiecao_Player.SCREEN_LAYOUT_NORMAL,"视频标题");

    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextPic;
        private SimpleDraweeView mImageHead;
        private TextView mTextChen;
        private TextView mTextName;
        private JCVideoPlayerStandard jiecao_Player;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextPic = itemView.findViewById(R.id.text_pic);
            mImageHead = itemView.findViewById(R.id.image_head);
            mTextChen = itemView.findViewById(R.id.text_chen);
            mTextName = itemView.findViewById(R.id.text_name);
            jiecao_Player=itemView.findViewById(R.id.jiecao_Player);
        }
    }
}


public class VideoShow extends Fragment {
    private RecyclerView recy_video;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.videoshow, container, false);
        Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Api.GETAD).build();
        MyServer mySession = retrofit.create(MyServer.class);
        Call<VideoBean> videoData = mySession.getVideoData(4, 1);
        videoData.enqueue(new Callback<VideoBean>() {
            @Override
            public void onResponse(Call<VideoBean> call, Response<VideoBean> response) {
                List<VideoBean.DataBean> data = response.body().getData();
                VideoAdapter videoAdapter = new VideoAdapter(data, getActivity());
                recy_video.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
                recy_video.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
                recy_video.setAdapter(videoAdapter);
            }

            @Override
            public void onFailure(Call<VideoBean> call, Throwable t) {

            }
        });


        initView(view);
        return view;
    }

    private void initView(View view) {
        recy_video = (RecyclerView) view.findViewById(R.id.recy_video);
    }
}

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 文字
     */
    private RadioButton mHome;
    /**
     * 图片
     */
    private RadioButton mCla;
    /**
     * 视频
     */
    private RadioButton mCar;
    /**
     * 预留
     */
    private RadioButton mFind;
    /**
     * 推荐
     */
    private RadioButton mMy;
    private FrameLayout mContainer;
    private FragmentManager fragmentManager;

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

    private void initView() {
        mHome = (RadioButton) findViewById(R.id.home);
        mHome.setOnClickListener(this);
        mCla = (RadioButton) findViewById(R.id.cla);
        mCla.setOnClickListener(this);
        mCar = (RadioButton) findViewById(R.id.car);
        mCar.setOnClickListener(this);
        mFind = (RadioButton) findViewById(R.id.find);
        mFind.setOnClickListener(this);
        mMy = (RadioButton) findViewById(R.id.my);
        mMy.setOnClickListener(this);
        mContainer = (FrameLayout) findViewById(R.id.container);
        mContainer.setOnClickListener(this);
        fragmentManager = getSupportFragmentManager();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                fragmentManager.beginTransaction().replace(R.id.container, new ImageShow()).commit();
                break;
            case R.id.home://文字
                fragmentManager.beginTransaction().replace(R.id.container, new TextShow()).commit();
                break;
            case R.id.cla://图片
                fragmentManager.beginTransaction().replace(R.id.container, new ImageShow()).commit();
                break;
            case R.id.car://视频
                fragmentManager.beginTransaction().replace(R.id.container, new VideoShow()).commit();
                break;
            case R.id.find://预留
                fragmentManager.beginTransaction().replace(R.id.container, new Myined()).commit();
                break;
            case R.id.my://推荐
                fragmentManager.beginTransaction().replace(R.id.container, new HomePager()).commit();
                break;
        }
    }
}

public class MyApp extends Application {
    public static Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        Fresco.initialize(context);
    }
}


activity_main.xml

    

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">
    <TextView
        android:id="@+id/class_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/class_img"
        android:layout_below="@+id/class_title"
        android:textColor="#f00"/>
    <ImageView
        android:id="@+id/class_img"
        android:layout_width="200dp"
        android:layout_centerHorizontal="true"
        android:layout_height="200dp"
        />
    <TextView
        android:id="@+id/class_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/class_img"
        />

</RelativeLayout>
 
 


video_layout.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/image_head"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:src="@mipmap/ic_launcher" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:orientation="vertical">

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

            <TextView
                android:id="@+id/text_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp" />
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/text_pic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
        android:id="@+id/jiecao_Player"
        android:layout_width="match_parent"
        android:layout_height="200dp">

    </fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard>


</LinearLayout>



videoshow.xml

    

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

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

猜你喜欢

转载自blog.csdn.net/qq_40788686/article/details/80725586
今日推荐