wang_wie
wang_wie
采纳率100%
2017-09-02 09:36 阅读 3.3k

Android重新加载recyclerView自定义间隔比上次大一倍

自定义了一个方法,每次重新加载recyclerView都调用这个方法,但是加载之后item间距每次都比原先的大一倍,请问我在调用这个方法的时候该怎么处理?

//名师风采
private void initTeacherData(final String url) {
Log.d("名师风采:", url);
mExTeacherList.clear();
//TODO
HttpUtil.sendOkHttpRequest(url,new okhttp3.Callback(){
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject newsJsonObject = new JSONObject(response.body().string());
JSONArray newsItems = newsJsonObject.getJSONArray("items");
for (int i = 0; i < newsItems.length(); i++) {
String description = newsItems.getJSONObject(i).getString("description");
String title = newsItems.getJSONObject(i).getString("title");
String start_time = newsItems.getJSONObject(i).getString("start_time");
JSONArray newsImageData = newsItems.getJSONObject(i).getJSONArray("pic_describe");
String teacherUrl = newsImageData.getJSONObject(0).getString("pic_path2");
String teacherUrl2="http://192.168.1.247:8081/obpm"+teacherUrl;
Log.d("名师风采:", title);
Log.d("名师风采:", description);
Log.d("名师风采:", start_time);
mExTeacherList.add(new ExTeacher(null,description,teacherUrl2,title,start_time));
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mLayoutManager = new LinearLayoutManager(CampusChildActivity.this, LinearLayoutManager.HORIZONTAL, false);
mAdapter = new ChCampusTeacherAdapter(mExTeacherList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addItemDecoration(new MyDividerItemDecoration(CampusChildActivity.this, LinearLayoutManager.HORIZONTAL));自定义设置间隔
// 设置adapter
mRecyclerView.setAdapter(mAdapter);
mTvCurrentPage.setText("当前页:"+mIntPageCount);

                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onFailure(Call call, IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(CampusChildActivity.this,"二级页面名师风采获取失败",Toast.LENGTH_SHORT).show();
                }
            });
        }
    });

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    lw_zhaoritian 一条不更新的懒狗 2017-09-03 05:44

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.addItemDecoration(new MyDividerItemDecoration(CampusChildActivity.this, LinearLayoutManager.HORIZONTAL));自定义设置间隔
    // 设置adapter
    mRecyclerView.setAdapter(mAdapter);

    这里的代码不需要每次都调用 写在on create 即可

    点赞 2 评论 复制链接分享
  • devmiao devmiao 2017-09-02 15:47
  • renjianbo2016 renjianbo2016 2017-09-05 15:41

    看了你的回答,问题解决,多谢哥们。

    点赞 评论 复制链接分享

相关推荐