XRecycleView,多条目展示

亡梦爱人 提交于 2019-11-30 18:37:33

前一篇文章,我给大家简单的写了一下XRecycleView,但是,数据太少了,所以今天再给大家带来一篇完整的

先来看一下最终效果图吧,我是将多条目展示放在了一个Fragment中,但大体上不影响的。

接下来,就给大家具体的走一遍代码:

这个工程中,有XRecycleView、Banner轮播,OkHttp请求数据,

首先,还是敲代码前的准备工作,先导入依赖:(这离依赖有点多,所以我就部分了,全都粘来了,)

    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:mediarouter-v7:25.0.0'
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:recyclerview-v7:25.0.0'
    compile 'com.squareup.okio:okio:1.5.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
    compile 'com.squareup.okio:okio:1.5.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.youth.banner:banner:1.4.9'
    //compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    compile 'com.github.bumptech.glide:glide:3.7.0'
接下来,加入权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
大家不要忘了,数据是从网路上请求下来的,这里我用了OkHttp请求网络数据,大家可以去我前一篇文章,哪里有集体的OkHttp代码(包括三
个工具类)
http://blog.csdn.net/pentablet/article/details/78273444
建一个类API,这里就是把要请求的网址写到里边。:
package utils;
 
/**
 * Created by 笔片 on 2017/10/25.
 */
 
public class API {
    public static final String TYPE_PATH = "http://192.168.28.5/mobile/index.php?act=goods_class";
    public static final String TYPE_HOME="http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=2&size=10&offset=0";
}

GlideImaGlideImageLoader类,主要还是请求网络图片用的,当然这里也可以使用ImageLoader:
package utils;
 
import android.content.Context;
import android.widget.ImageView;
 
import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;
 
/**
 * Created by 笔片 on 2017/10/25.
 */
 
public class GlideImaGlideImageLoader extends ImageLoader {
 
    public void displayImage(Context context, Object path, ImageView imageView) {
        Glide.with(context).load(path).into(imageView);
    }
}

对于我请求的这个借口,还需要一个网络拦截器,这个拦截器是一对一的,一个网络接口对应一个网络拦截器,这个类看大家的了。:
package utils;
 
import android.os.Build;
 
import java.io.IOException;
 
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
 
/**
 * Created by 笔片 on 2017/10/16.
 * 网络拦截器
 */
 
public class LoggingInterceptor implements Interceptor{
    private static final String UA = "User-Agent";
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request()
                .newBuilder()
                .addHeader(UA, makeUA())
                .build();
        return chain.proceed(request);
    }
    private String makeUA() {
        String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
        return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
    }
}
拦截器写完之后,当然还是需要配置的,:在工具类中进行配置:

在布局文件中:fragment1_item.xml:
<com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/xre_xrv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.jcodecraeer.xrecyclerview.XRecyclerView>

在主函数中:这里我也到了fragment中,也一样的:
package fragments;
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
 
import com.bwie.test.yuekaotext.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import adapter.XRAdapter;
import bean.HomeBean;
import okhttp3.Call;
import utils.API;
import utils.GsonObjectCallback;
import utils.OkHttp3Utils;
 
/**
 * Created by 笔片 on 2017/10/25.
 */
 
public class fragment1 extends Fragment{
    private XRecyclerView xr;
    private List<String> list = new ArrayList<>();
    //获取数据的开始
    private int curr;
    private XRAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1_item,container,false);
        //初始化xr
        xr = (XRecyclerView) view.findViewById(R.id.xre_xrv);
        //加布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xr.setLayoutManager(layoutManager);
        xr.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                curr = 0;
                list.clear();
                getData(API.TYPE_HOME, curr);
                xr.refreshComplete();
            }
 
            @Override
            public void onLoadMore() {
             /*   curr++;
                getData(API.TYPE_HOME,curr);
                xr.refreshComplete();*/
            }
        });
        getData(API.TYPE_HOME, 1);
        return view;
    }
    //初始化数据
    private void getData(String url, int curr) {
        OkHttp3Utils.getInstance().doGet(url, new GsonObjectCallback<HomeBean>() {
            @Override
            public void onUi(HomeBean homeBean) {
                List<HomeBean.SongListBean> song_list = homeBean.getSong_list();
                homeBean.getError_code();
                XRAdapter mxradapter = new XRAdapter(song_list,getActivity());
                xr.setAdapter(mxradapter);
            }
 
            @Override
            public void onFailed(Call call, IOException e) {
 
            }
        });
    }
}

接着,就要建适配器了XRAdapter:这个类中就是大家想要的分类了,这里还要导入ImageLoader的Jar包
{
   HomeBean.SongListBean,这个类是我的数据接口封装类,
   MyNews,这个类,是自条目,自己需要展示什么数据,就在里边些什么数据
}
package adapter;
 
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
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 android.widget.Toast;
 
import com.bwie.test.yuekaotext.R;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
 
import java.util.ArrayList;
import java.util.List;
 
import bean.HomeBean;
import bean.MyNews;
import utils.GlideImaGlideImageLoader;
 
/**
 * Created by 笔片 on 2017/10/25.
 */
 
public class XRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<HomeBean.SongListBean> song_list;
    Context mcontext;
    ArrayList mlist;
    ArrayList<MyNews> list_news;
    RecyclelistAdapter adapter;
 
    //枚举类型
    private enum Item_Type {
 
        Typeone, Typetwo
 
    }
 
    public XRAdapter(List<HomeBean.SongListBean> song_list, Context mcontext) {
        this.song_list = song_list;
        this.mcontext = mcontext;
    }
 
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 
        if (viewType == Item_Type.Typeone.ordinal()) {
            View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_banner_item, null);
            ViewHolderA viewHolder = new ViewHolderA(mView);
            return viewHolder;
 
        } else if (viewType == Item_Type.Typetwo.ordinal()) {
 
            View mView = LayoutInflater.from(mcontext).inflate(R.layout.list_item, null);
            ViewHolderB viewHolder = new ViewHolderB(mView);
            return viewHolder;
        }
        return null;
    }
 
    /**
     * 绑定数据:可以直接拿到已经绑定控件的Viewholder对象
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolderA) {
            mlist = new ArrayList();
            for (int i = 0; i<song_list.size(); i++){
                mlist.add(song_list.get(i).getPic_big());
            }
            //设置图片加载器
            ((ViewHolderA) holder).mbanner.setImageLoader(new GlideImaGlideImageLoader());
            ((ViewHolderA) holder).mbanner.setImages(mlist);
            ((ViewHolderA) holder).mbanner.start();
 
        } else if (holder instanceof ViewHolderB) {
//            list_news = new ArrayList<MyNews>();
//            for (int i = 0; i<song_list.size(); i++){
//                list_news.add(new MyNews(song_list.get(i).getPic_big(),song_list.get(i).getAlbum_title()));
//            }
            ((ViewHolderB) holder).tv.setText(song_list.get(position).getAlbum_title());
            String imgURL = song_list.get(position).getPic_big();
            com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
            instance.displayImage(imgURL,((ViewHolderB) holder).img);
 
        }
    }
 
    @Override
    public int getItemCount() {
        return song_list.size();
    }
 
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }
 
    class ViewHolderA extends RecyclerView.ViewHolder {
        public Banner mbanner;
 
        public ViewHolderA(View itemView) {
            super(itemView);
 
            mbanner = (Banner) itemView.findViewById(R.id.banner);
        }
    }
 
 
    class ViewHolderB extends RecyclerView.ViewHolder {
 
        public ImageView img;
        public TextView tv;
 
        public ViewHolderB(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}

MyNews.class:
package bean;
 
/**
 * Created by 笔片 on 2017/10/25.
 */
 
public class MyNews {
    private String imgURL;
    private String title;
 
    @Override
    public String toString() {
        return "MyNews{" +
                "imgURL='" + imgURL + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
 
    public String getImgURL() {
        return imgURL;
    }
 
    public void setImgURL(String imgURL) {
        this.imgURL = imgURL;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public MyNews(String imgURL, String title) {
 
        this.imgURL = imgURL;
        this.title = title;
    }
}
recycle_banner_item:这个就是放置Banner的xml:
<com.youth.banner.Banner
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="200dp"></com.youth.banner.Banner>

list_item:第二种布局的xml:
<ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="woshi"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

这其中,我用ImageLoader请求图片,就要先初始化:MyApp:
记得在清单文件中配置:

package app;
 
import android.app.Application;
 
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
 
/**
 * 1. 类的用途
 * 2. @author forever
 * 3. @date 2017/9/8 12:33
 */
 
public class MyApp extends Application {
    public static MyApp mInstance;
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(getApplicationContext());
        ImageLoader.getInstance().init(configuration);
        mInstance = this;
 
    }
    public static MyApp getInstance() {
        return mInstance;
    }
}

我自己有根据步骤走了一遍,出来了,说明还可以:

————————————————
版权声明:本文为CSDN博主「PenTablet」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/PenTablet/article/details/78345337

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!