IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> RecyclerView(一) -> 正文阅读

[移动开发]RecyclerView(一)

#############################################################################
#############################################################################<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>

#####################################################################################################################?

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-10-25 12:38:54  更:2021-10-25 12:39:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 0:50:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码