Android商城開發系列(十四)—— 設置監聽RecyclerView的位置

  在前面的博客中有講到過點擊一個圖片按鈕控制RecyclerView的滾動到頂部位置的效果,可是那個圖片按鈕一直處在一個顯示的狀態,今天咱們來改造一下那個地方,咱們要實現的效果是:一開始打開的時候看不到這個圖片按鈕,當去滑動界面的時候才能去顯示這個圖片按鈕。android

  要實現這個效果也很簡單,咱們監聽RecyclerView的LayoutManager的SpanSizeLookup事件便可,在HomeFragment類中的processData方法去完善,以下圖:json

  

  只須要在這一塊去添加這樣的邏輯就能夠了,總體的代碼以下所示:ide

  1 package com.nyl.shoppingmall.home.fragment;
  2 
  3 import android.support.v7.widget.GridLayoutManager;
  4 import android.support.v7.widget.RecyclerView;
  5 import android.util.Log;
  6 import android.view.View;
  7 import android.widget.ImageView;
  8 import android.widget.TextView;
  9 import android.widget.Toast;
 10 
 11 import com.alibaba.fastjson.JSON;
 12 import com.nyl.shoppingmall.R;
 13 import com.nyl.shoppingmall.base.BaseFragment;
 14 import com.nyl.shoppingmall.home.adapter.HomeFragmentAdapter;
 15 import com.nyl.shoppingmall.home.bean.ResultBeanData;
 16 import com.nyl.shoppingmall.utils.Constants;
 17 import com.zhy.http.okhttp.OkHttpUtils;
 18 import com.zhy.http.okhttp.callback.StringCallback;
 19 
 20 import okhttp3.Call;
 21 
 22 /**
 23  * 首頁Fragment
 24  */
 25 public class HomeFragment extends BaseFragment implements View.OnClickListener {
 26 
 27     private final static String TAG = HomeFragment.class.getSimpleName();
 28 
 29     private TextView tv_search_home;
 30     private TextView tv_message_home;
 31     private RecyclerView rv_home;
 32     private ImageView ib_top;
 33     private HomeFragmentAdapter adapter;
 34     //返回的數據
 35     private ResultBeanData.ResultBean resultBean;
 36 
 37     @Override
 38     public View initView() {
 39         Log.e(TAG,"主頁面的Fragment的UI被初始化了");
 40         View view = View.inflate(mContext,R.layout.fragment_home,null);
 41         //初始化佈局控件
 42         tv_search_home = (TextView) view.findViewById(R.id.tv_search_home);
 43         tv_message_home = (TextView) view.findViewById(R.id.tv_message_home);
 44         rv_home = (RecyclerView) view.findViewById(R.id.rv_home);
 45         ib_top = (ImageView) view.findViewById(R.id.ib_top);
 46 
 47         //設置點擊事件
 48         ib_top.setOnClickListener(this);
 49         tv_search_home.setOnClickListener(this);
 50         tv_message_home.setOnClickListener(this);
 51         return view;
 52     }
 53 
 54 
 55     @Override
 56     public void initData() {
 57         super.initData();
 58         Log.e(TAG,"主頁面的Fragment的數據被初始化了");
 59 
 60         //聯網請求首頁數據
 61         getDataFromNet();
 62     }
 63 
 64     private void getDataFromNet() {
 65        // String url = Constants.HOME_URL;
 66         OkHttpUtils
 67                 .get()
 68                 .url(Constants.HOME_URL)
 69                 .build()
 70                 .execute(new StringCallback()
 71                 {
 72 
 73                     /**
 74                      * 請求失敗的時候回調
 75                      * @param call
 76                      * @param e
 77                      * @param id
 78                      */
 79                     @Override
 80                     public void onError(Call call, Exception e, int id) {
 81 
 82                         Log.e(TAG,"首頁請求失敗=="+e.getMessage());
 83                     }
 84 
 85                     /**
 86                      * 當聯網成功的時候回調
 87                      * @param response 請求成功數據
 88                      * @param id
 89                      */
 90                     @Override
 91                     public void onResponse(String response, int id) {
 92 
 93                         Log.e(TAG,"首頁請求成功=="+response);
 94                         //解析數據
 95                         processData(response);
 96                     }
 97                 });
 98     }
 99 
100     /**
101      * 解析數據
102      * @param json
103      */
104     private void processData(String json) {
105         //使用FastJson去解析數據,將Json字符串轉換成一個ResultBeanData對象
106         ResultBeanData resultBeanData = JSON.parseObject(json,ResultBeanData.class);
107         resultBean = resultBeanData.getResult();
108 
109         if (resultBean != null){
110             //有數據就設置適配器
111             adapter = new HomeFragmentAdapter(mContext,resultBean);
112             rv_home.setAdapter(adapter);
113 
114             GridLayoutManager manager = new GridLayoutManager(mContext,1);
115             //設置跨度大小監聽
116             manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
117                 @Override
118                 public int getSpanSize(int position) {
119                     if (position <= 3){
120                         //隱藏
121                         ib_top.setVisibility(View.GONE);
122                     }else {
123                         //顯示
124                         ib_top.setVisibility(View.VISIBLE);
125                     }
126                     //只能返回1
127                     return 1;
128                 }
129             });
130             //設置佈局管理者
131             rv_home.setLayoutManager(manager);
132         }else {
133             //沒有數據
134 
135         }
136 
137         Log.e(TAG,"解析成功=="+resultBean.getHot_info().get(0).getName());
138     }
139 
140     @Override
141     public void onClick(View view) {
142         switch (view.getId()){
143             case R.id.ib_top: //置頂的監聽
144                 rv_home.scrollToPosition(0);
145                 break;
146             case R.id.tv_search_home:  //搜索的監聽
147                 Toast.makeText(mContext,"搜索",Toast.LENGTH_SHORT).show();
148                 break;
149             case R.id.tv_message_home: //消息監聽
150                 Toast.makeText(mContext,"進入消息中心",Toast.LENGTH_SHORT).show();
151                 break;
152         }
153     }
154 }

  還有最重要的一步,以前fragment_home.xml的佈局中的ImageButton設置的是顯示按鈕圖片的,以下圖所示:佈局

  

  這個是要顯示的意思,把它改爲隱藏的就能夠了,以下圖所示:ui

  

   經過這樣改造以後,就能夠實現:一開始打開的時候看不到這個圖片按鈕,當去滑動界面的時候才能去顯示這個圖片按鈕的效果了。this

  好了,本篇博客的內容就這麼多了。url

相關文章
相關標籤/搜索