#############################################################################
#############################################################################<uses-permission android:name="android.permission.INTERNET" />
#############################################################################
implementation 'org.jsoup:jsoup:1.11.2'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
#####################################################################################################################
package com.example.heca222;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ScrollView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<MoviePicList> mList = new ArrayList<>();
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData();
initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.MoviePicList);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(20));
//recyclerView.addItemDecoration(new GridLayout(30));
//LinearLayoutManager layoutManager = new LinearLayoutManager(this);
GridLayoutManager layoutManager = new GridLayoutManager(this,3);
recyclerView.setLayoutManager(layoutManager);
MoviePicListAdapter adapter = new MoviePicListAdapter(this,mList);
adapter.setLoadMoreDataListener(new MoviePicListAdapter.LoadMoreDataListener() {
@Override
public void loadMoreData() {
//加入null值此时adapter会判断item的type
Log.e("ffffff", "loadMoreData--->");
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (mList.size() < 70) {
for (int i = 0; i < 5; i++) {
//list.add("LOAD MORE DATA---------->" + i);
MoviePicList apple = new MoviePicList("Apple" +i, xiangqing, url,"linkurl"+i);
mList.add(apple);
}
adapter.stopLoadMore();
} else {
adapter.loadComplete();
}
}
}, 500);
}
});
recyclerView.setAdapter(adapter);
}
private String url="https://puui.qpic.cn/newsapp_ls/0/14060954054/0";
private String xiangqing="长津湖";
private void initFruits() {
MoviePicList apple = new MoviePicList("Apple", xiangqing, url,"linkurl1");
mList.add(apple);
MoviePicList banana = new MoviePicList("Banana",xiangqing, url,"linkurl2");
mList.add(banana);
MoviePicList orange = new MoviePicList("Orange",xiangqing, url,"linkurl1");
mList.add(orange);
MoviePicList watermelon = new MoviePicList("Watermelon",xiangqing, url,"linkurl3");
mList.add(watermelon);
MoviePicList pear = new MoviePicList("Pear", xiangqing, url,"linkurl4");
mList.add(pear);
MoviePicList grape = new MoviePicList("Grape", xiangqing, url,"linkurl5");
mList.add(grape);
}
private void getData() {
new Thread(new Runnable(){
@Override
public void run() {
Document doc = null;
try {
doc = Jsoup.connect("http://m.f4yy.com/vod-type-id-1.html").get();
} catch (IOException e) {
e.printStackTrace();
}
//Log.e("ooooooooooo",doc.toString());
Elements videoList = doc.select("ul.list_tab_img").select("li");
for (Element videoElement : videoList) {
String name = videoElement.select("a").first().attr("title");
String link = videoElement.select("a").first().attr("href");
String pic = videoElement.select("img").attr("data-original");
if (pic.length() > 5) {
String head = pic.substring(0, 5);
if (!head.contains("http")) {
pic = "http:" + pic;
}
}
String score = videoElement.select("label.score").text();
String title = videoElement.select("label.title").text();
String actor = videoElement.select("p").text();
MoviePicList m=new MoviePicList(name,actor,pic,link);
mList.add(m);
}
//Log.e("GGGGGGGGGGGGGG","name:::"+link+"link::::"+pic+"p::::"+p);
}
}).start();
}
}
#####################################################################################################################?
package com.example.heca222;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MoviePicListAdapter extends RecyclerView.Adapter<MoviePicListAdapter.ViewHolder>{
private List<MoviePicList> mList;
private Context context;
private static final int STATE_NORMAL = 0;
private static final int STATE_LOADING = 1;
private static final int STATE_LOAD_COMPLETE = 2;
private int mState = STATE_NORMAL;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView pic;
TextView title;
TextView xiangqing;
public ViewHolder (View view)
{
super(view);
pic = (ImageView) view.findViewById(R.id.pic);
title = (TextView) view.findViewById(R.id.title);
xiangqing = (TextView) view.findViewById(R.id.xiangqing);
}
}
public MoviePicListAdapter (Context context,List <MoviePicList> fruitList){
this.context = context;
this.mList = fruitList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movieiclist,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position){
MoviePicList fruit = mList.get(position);
//holder.pic.setImageResource(fruit.getImageUrl());
//holder.pic.setImageBitmap(bitmap);
Glide.with(context).load(fruit.getImageUrl()).into(holder.pic);
holder.title.setText(fruit.getTitle());
holder.xiangqing.setText(fruit.getXiangqing());
holder.pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "click " + fruit.getLinkUrl(), Toast.LENGTH_SHORT).show();
context.startActivity(new Intent(context,xiangQing.class));
}
});
}
@Override
public int getItemCount(){
return mList.size();
}
private LoadMoreDataListener mLoadMoreDataListener;
public void setLoadMoreDataListener(LoadMoreDataListener mLoadMoreDataListener) {
this.mLoadMoreDataListener = mLoadMoreDataListener;
}
public interface LoadMoreDataListener {
void loadMoreData();
}
public void stopLoadMore() {
mState = STATE_NORMAL;
notifyDataSetChanged();
}
public void loadComplete() {
mState = STATE_LOAD_COMPLETE;
notifyItemChanged(getItemCount()-1);
}
}
#####################################################################################################################?
package com.example.heca222;
public class MoviePicList {
private String title;
private String xiangqing;
private String imageUrl;
private String linkUrl;
public MoviePicList(String title,String xiangqing, String imageUrl,String linkUrl){
this.title = title;
this.xiangqing=xiangqing;
this.imageUrl = imageUrl;
this.linkUrl = linkUrl;
}
public String getTitle() {
return title;
}
public String getXiangqing() {
return xiangqing;
}
public String getImageUrl() {
return imageUrl;
}
public String getLinkUrl() {
return linkUrl;
}
}
#####################################################################################################################?
package com.example.heca222;
import android.graphics.Rect;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;
/**
* GridLayoutManager(网格布局)设置item的间隔
*
* 作者: 周旭 on 2017年7月20日 0020.
* 邮箱:374952705@qq.com
* 博客:http://www.jianshu.com/u/56db5d78044d
*/
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public GridSpacingItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//不是第一个的格子都设一个左边和底部的间距
outRect.left = space;
outRect.bottom = space;
//由于每行都只有3个,所以第一个都是3的倍数,把左边距设为0
if (parent.getChildLayoutPosition(view) %3==0) {
outRect.left = 0;
}
}
}
#####################################################################################################################?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/MoviePicList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
#####################################################################################################################?
<?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="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="17dp"
/>
<TextView
android:id="@+id/xiangqing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="14dp"
/>
</LinearLayout>
#####################################################################################################################?
|