最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

XrecyclerView實現(xiàn)加載數(shù)據(jù)和切換不同布局

 更新時間:2018年12月17日 09:16:05   作者:wittybread  
這篇文章主要為大家詳細介紹了XrecyclerView實現(xiàn)加載數(shù)據(jù)、切換不同布局功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了XrecyclerView實現(xiàn)加載數(shù)據(jù)和切換不同布局,供大家參考,具體內(nèi)容如下

compile 'com.jcodecraeer:xrecyclerview:1.3.2'//XrecyclerView

顯示界面

<?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="match_parent"
 android:orientation="vertical"
 tools:context="wangxuewei.bwie.com.wangxuewei1510c2071219.MainActivity">

 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_vertical">

  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:gravity="center"
   android:text="搜索商品" />

  <ImageView
   android:id="@+id/cutImg"
   android:layout_width="30dp"
   android:layout_height="30dp"
   android:layout_alignParentRight="true"
   android:layout_margin="7dp"
   android:src="@drawable/grid_icon" />

 </RelativeLayout>

 <TextView
  android:layout_width="match_parent"
  android:layout_height="2dp"
  android:background="#c0c0c0" />

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

  <EditText
   android:id="@+id/editKey"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:hint="請輸入關(guān)鍵字" />

  <Button
   android:id="@+id/btnSearch"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="搜索" />

 </LinearLayout>

 <TextView
  android:layout_width="match_parent"
  android:layout_height="2dp"
  android:background="#c0c0c0" />

 <com.jcodecraeer.xrecyclerview.XRecyclerView
  android:id="@+id/xrecyclerview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"></com.jcodecraeer.xrecyclerview.XRecyclerView>

</LinearLayout>

網(wǎng)格布局條目顯示樣式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:orientation="vertical">

 <ImageView
  android:id="@+id/GoodsIcon"
  android:layout_width="80dp"
  android:layout_height="80dp" />

 <TextView
  android:id="@+id/title"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_margin="10dp"
  android:text="asdasd" />


 <TextView
  android:id="@+id/price"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="asdasd" />

 <TextView
  android:id="@+id/bargainPrice"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="5dp"
  android:text="asdasd"
  android:textColor="#f00" />
</LinearLayout>

列表布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center_vertical"
 android:orientation="horizontal">

 <ImageView
  android:id="@+id/GoodsIcon"
  android:layout_width="80dp"
  android:layout_height="80dp" />

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

  <TextView
   android:id="@+id/title"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:text="asdasd" />

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:orientation="horizontal">

   <TextView
    android:id="@+id/price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="asdasd" />

   <TextView
    android:id="@+id/bargainPrice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:text="asdasd"
    android:textColor="#f00" />
  </LinearLayout>

 </LinearLayout>

</LinearLayout>

主界面

public class MainActivity extends AppCompatActivity implements ShopSearchViewAPI, View.OnClickListener {

 private ImageView cutImg;
 private Button btnSearch;
 private List<GoodsBean.DataBean> list = new ArrayList<>();
 private XRecyclerView xR;
 private EditText editKey;
 private int flag = 1;
 private MyAdapter myAdapter;
 private int i = 1;
 private String string = "手機";
 private String name;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //加載控件
  initView();
  getData("手機", "1");

  //設(shè)置可上拉
  xR.setPullRefreshEnabled(true);
  xR.setLoadingMoreEnabled(true);
  //設(shè)置上拉下拉樣式
  xR.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
  xR.setLaodingMoreProgressStyle(ProgressStyle.BallClipRotate);

  clickListener();

  xR.setLoadingListener(new XRecyclerView.LoadingListener() {
   @Override
   public void onRefresh() {
    i = 1;
    list.clear();
    getData(string, "" + i);
    xR.refreshComplete();
   }

   @Override
   public void onLoadMore() {
    i++;
    getData(string, "" + i);
    xR.loadMoreComplete();
   }
  });


 }

 public void getData(String key, String page) {
  ShopSearchPresenter shopSearchPresenter = new ShopSearchPresenter(this, this);
  shopSearchPresenter.getGoodsData("http://120.27.23.105/product/searchProducts", key, page);
 }

 private void clickListener() {
  cutImg.setOnClickListener(this);
  btnSearch.setOnClickListener(this);
 }

 private void initView() {
  cutImg = (ImageView) findViewById(R.id.cutImg);
  btnSearch = (Button) findViewById(R.id.btnSearch);
  xR = (XRecyclerView) findViewById(R.id.xrecyclerview);
  editKey = (EditText) findViewById(R.id.editKey);
 }

 @Override
 public void getSuccess(Object o) {
  GoodsBean o1 = (GoodsBean) o;
  List<GoodsBean.DataBean> data = o1.getData();
  list.addAll(data);
  setMyAdapter(flag);
 }

 @Override
 public void getFailed(Exception e) {

 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.cutImg:
    if (flag == 1) {
     cutImg.setImageResource(R.drawable.lv_icon);
     flag = 2;
    } else {
     cutImg.setImageResource(R.drawable.grid_icon);
     flag = 1;
    }
    setMyAdapter(flag);
    break;
   case R.id.btnSearch:
    list.clear();
    name = editKey.getText().toString();
    string = name;
    getData(string, "1");
    break;
  }
 }

 public void setMyAdapter(int f) {
  if (f == 1) {
   // 線性布局管理器 VERTICAL默認樣式/豎向顯示  第三個參數(shù)是數(shù)據(jù)是否到過來顯示
   LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
   //添加布局管理器
   xR.setLayoutManager(manager);
   myAdapter = new MyAdapter(list, this, f);
   xR.setAdapter(myAdapter);
  } else if (f == 2) {
   // 線性布局管理器 VERTICAL默認樣式/豎向顯示  第三個參數(shù)是數(shù)據(jù)是否到過來顯示
   GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
   //添加布局管理器
   xR.setLayoutManager(gridLayoutManager);
   myAdapter = new MyAdapter(list, this, f);
   xR.setAdapter(myAdapter);
  }
 }
}

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
 private List<GoodsBean.DataBean> list;
 private Context context;
 private int flag = 1;
 private View inflate;

 public MyAdapter(List<GoodsBean.DataBean> list, Context context, int flag) {
  this.list = list;
  this.context = context;
  this.flag = flag;
 }

 @Override
 public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  if (flag == 1) {
   inflate = LayoutInflater.from(context).inflate(R.layout.lvitem, parent, false);
  } else if (flag == 2) {
   inflate = LayoutInflater.from(context).inflate(R.layout.griditem, parent, false);
  }

  MyViewHolder myViewHolder = new MyViewHolder(inflate);

  return myViewHolder;
 }

 @Override
 public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {

  String images = list.get(position).getImages();
  String[] split = images.split("\\|");
  Glide.with(context).load(split[0]).into(holder.icon);
  holder.title.setText(list.get(position).getTitle());
  holder.bargainPrice.setText("折扣價:" + list.get(position).getBargainPrice() + "");
  holder.price.setText("原價:" + list.get(position).getPrice());
  holder.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

 }

 @Override
 public int getItemCount() {
  return list != null ? list.size() : 0;
 }

 class MyViewHolder extends RecyclerView.ViewHolder {

  private ImageView icon;
  private TextView title;
  private TextView bargainPrice;
  private TextView price;

  public MyViewHolder(View itemView) {
   super(itemView);

   icon = (ImageView) itemView.findViewById(R.id.GoodsIcon);
   title = (TextView) itemView.findViewById(R.id.title);
   bargainPrice = (TextView) itemView.findViewById(R.id.bargainPrice);
   price = (TextView) itemView.findViewById(R.id.price);

  }
 }

}

ShopSearchModle

public class ShopSearchModle {


 public void getData(String url, Map<String, String> map, final ShopSearchPresenterAPI shopSearchPresenterAPI) {
  HttpUtils.getInstance().get(url, map, new CallBack() {
   @Override
   public void onSuccess(Object o) {
    shopSearchPresenterAPI.success(o);
   }

   @Override
   public void onFailed(Exception e) {
    shopSearchPresenterAPI.failed(e);
   }
  }, GoodsBean.class);
 }


}

ShopSearchPresenter

public class ShopSearchPresenter {

 private ShopSearchViewAPI shopSearchViewAPI;
 private Context context;
 private final ShopSearchModle shopSearchModle;

 public ShopSearchPresenter(ShopSearchViewAPI shopSearchViewAPI, Context context) {
  this.shopSearchViewAPI = shopSearchViewAPI;
  this.context = context;
  shopSearchModle = new ShopSearchModle();
 }

 public void getGoodsData(String url, String keywords, String page) {
  Map<String, String> map = new HashMap<>();
  map.put("keywords", keywords);
  map.put("page", page);
  shopSearchModle.getData(url, map, new ShopSearchPresenterAPI() {
   @Override
   public void success(Object o) {
    shopSearchViewAPI.getSuccess(o);
   }

   @Override
   public void failed(Exception e) {
    shopSearchViewAPI.getFailed(e);
   }
  });
 }


}

ShopSearchPresenterAPI

public interface ShopSearchPresenterAPI {

 void success(Object o);

 void failed(Exception e);


}

重點內(nèi)容

package wangxuewei.bwie.com.wangxuewei1510c2071219;

/**
 * Created by jim on 2017/12/19.
 */

public interface ShopSearchViewAPI {

 void getSuccess(Object o);

 void getFailed(Exception e);


}

HttpUtils

public class HttpUtils {

 private static volatile HttpUtils instance;

 private static Handler handler = new Handler();

 private HttpUtils() {

 }

 public static HttpUtils getInstance() {
  if (instance == null) {
   synchronized (HttpUtils.class) {
    if (instance == null) {
     instance = new HttpUtils();
    }
   }
  }
  return instance;
 }

 //get請求
 public void get(String url, Map<String, String> map, final CallBack callBack, final Class c) {
  //對url和參數(shù)做拼接處理
  StringBuffer stringBuffer = new StringBuffer();
  stringBuffer.append(url);
  //判斷是否存在? if中是存在
  if (stringBuffer.indexOf("?") != -1) {
   //判斷?是否在最后一位 if中是不在最后一位
   if (stringBuffer.indexOf("?") != stringBuffer.length() - 1) {
    stringBuffer.append("&");
   }
  } else {
   stringBuffer.append("?");
  }
  for (Map.Entry<String, String> entry : map.entrySet()) {
   stringBuffer.append(entry.getKey())
     .append("=")
     .append(entry.getValue())
     .append("&");
  }
  //判斷是否存在& if中是存在
  if (stringBuffer.indexOf("&") != -1) {
   stringBuffer.deleteCharAt(stringBuffer.lastIndexOf("&"));
  }


  //1:創(chuàng)建OkHttpClient對象
  OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Logger()).build();
  //2:創(chuàng)建Request對象
  final Request request = new Request.Builder()
    .get()
    .url(stringBuffer.toString())
    .build();
  //3:創(chuàng)建Call對象
  Call call = okHttpClient.newCall(request);
  //4:請求網(wǎng)絡(luò)
  call.enqueue(new Callback() {
   //請求失敗
   @Override
   public void onFailure(Call call, final IOException e) {
    handler.post(new Runnable() {
     @Override
     public void run() {
      callBack.onFailed(e);
     }
    });
   }

   //請求成功
   @Override
   public void onResponse(Call call, Response response) throws IOException {
    String result = response.body().string();
    //拿到數(shù)據(jù)解析
    final Object o = new Gson().fromJson(result, c);
    //當前是在子線程,回到主線程中
    handler.post(new Runnable() {
     @Override
     public void run() {
      //回調(diào)
      callBack.onSuccess(o);
     }
    });
   }
  });

 }

 //post請求
 public void post(String url, Map<String, String> map, final CallBack callBack, final Class c) {
  //1:創(chuàng)建OkHttpClient對象
  OkHttpClient okHttpClient = new OkHttpClient();
  //2:提供post請求需要的body對象
  FormBody.Builder builder = new FormBody.Builder();
  for (Map.Entry<String, String> entry : map.entrySet()) {
   builder.add(entry.getKey(), entry.getValue());
  }
  FormBody body = builder.build();
  //3:創(chuàng)建Request對象
  final Request request = new Request.Builder()
    .post(body)
    .url(url)
    .build();
  //4:創(chuàng)建Call對象
  Call call = okHttpClient.newCall(request);
  //5:請求網(wǎng)絡(luò)
  call.enqueue(new Callback() {
   //請求失敗
   @Override
   public void onFailure(Call call, final IOException e) {
    handler.post(new Runnable() {
     @Override
     public void run() {
      callBack.onFailed(e);
     }
    });
   }

   //請求成功
   @Override
   public void onResponse(Call call, Response response) throws IOException {
    String result = response.body().string();
    //拿到數(shù)據(jù)解析
    final Object o = new Gson().fromJson(result, c);
    //當前是在子線程,回到主線程中
    handler.post(new Runnable() {
     @Override
     public void run() {
      //回調(diào)
      callBack.onSuccess(o);
     }
    });
   }
  });
 }

}

重點內(nèi)容

public interface CallBack {

 void onSuccess(Object o);

 void onFailed(Exception e);

}

Logger

public class Logger implements Interceptor {
 @Override
 public Response intercept(Chain chain) throws IOException {
  Request original = chain.request();
  HttpUrl url = original.url().newBuilder()
    .addQueryParameter("source", "android")
    .build();
  //添加請求頭
  Request request = original.newBuilder()
    .url(url)
    .build();
  return chain.proceed(request);
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio引入FFmpeg的方法

    Android Studio引入FFmpeg的方法

    這篇文章主要介紹了Android Studio引入FFmpeg的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android 訪問文件權(quán)限的四種模式介紹

    Android 訪問文件權(quán)限的四種模式介紹

    這篇文章主要介紹了Android 訪問文件權(quán)限的四種模式介紹的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • android布局優(yōu)化的一些實用建議

    android布局優(yōu)化的一些實用建議

    這篇文章主要給大家介紹了關(guān)于android布局優(yōu)化的一些實用建議,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Android UI設(shè)計與開發(fā)之實現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面

    Android UI設(shè)計與開發(fā)之實現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面

    這篇文章主要為大家詳細介紹了Android UI設(shè)計與開發(fā)之實現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android自定義view實現(xiàn)滑動解鎖九宮格控件

    Android自定義view實現(xiàn)滑動解鎖九宮格控件

    這篇文章主要介紹了Android自定義view實現(xiàn)滑動解鎖九宮格控件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • Android動畫之TranslateAnimation用法案例詳解

    Android動畫之TranslateAnimation用法案例詳解

    這篇文章主要介紹了Android動畫之TranslateAnimation用法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 深入Understanding Android ContentProvider詳解

    深入Understanding Android ContentProvider詳解

    本篇文章是對Android ContentProvider進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Android支付寶支付封裝代碼

    Android支付寶支付封裝代碼

    這篇文章主要介紹了Android支付寶支付封裝代碼,Android支付的時候肯定會使用支付寶進行支付,封裝可以簡化操作步驟,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android 如何實現(xiàn)亮度自動調(diào)節(jié)

    Android 如何實現(xiàn)亮度自動調(diào)節(jié)

    這篇文章主要介紹了Android 如何實現(xiàn)亮度自動調(diào)節(jié),幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android開發(fā)使用RecyclerView添加點擊事件實例詳解

    Android開發(fā)使用RecyclerView添加點擊事件實例詳解

    這篇文章主要為大家介紹了Android開發(fā)使用RecyclerView添加點擊事件實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08

最新評論

江油市| 潢川县| 老河口市| 万载县| 元氏县| 高唐县| 申扎县| 灵丘县| 公主岭市| 扎鲁特旗| 恩施市| 临邑县| 湖口县| 家居| 于都县| 日照市| 龙口市| 浮山县| 本溪| 海晏县| 惠来县| 仪陇县| 六盘水市| 鄯善县| 鄂州市| 黑山县| 南丰县| 望谟县| 芜湖县| 永平县| 太原市| 临颍县| 新巴尔虎右旗| 安图县| 荣昌县| 巧家县| 临海市| 泸州市| 清徐县| 巫溪县| 肇庆市|