XRecycleView,多條目展現

前一篇文章,我給你們簡單的寫了一下XRecycleView,可是,數據太少了,因此今天再給你們帶來一篇完整的php

先來看一下最終效果圖吧,我是將多條目展現放在了一個Fragment中,但大致上不影響的。java

接下來,就給你們具體的走一遍代碼:android

這個工程中,有XRecycleView、Banner輪播,OkHttp請求數據,git

首先,仍是敲代碼前的準備工做,先導入依賴:(這離依賴有點多,因此我就部分了,全都粘來了,)github

    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";
}api

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;
    }
}
攔截器寫完以後,固然仍是須要配置的,:在工具類中進行配置:app

在佈局文件中: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>ide

在主函數中:這裏我也到了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

相關文章
相關標籤/搜索