2 wang wie wang_wie 于 2017.09.02 17:36 提问

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
lw_zhaoritian   2017.09.03 13:44
已采纳

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

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

wang_wie
wang_wie 谢谢。。。刚开始学习Android,不是很懂。
3 个月之前 回复
devmiao
devmiao   Ds   Rxr 2017.09.02 23:47
renjianbo2016
renjianbo2016   2017.09.05 23:41

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

Csdn user default icon
上传中...
上传图片
插入图片