【问题标题】:Fragment recyclerView adapter notifyDataSetChanged() from MainActivity来自 MainActivity 的片段 recyclerView 适配器 notifyDataSetChanged()
【发布时间】:2017-11-18 09:04:59
【问题描述】:

请找我解决这个问题,我在json api 中有一个图像列表url,列表 url api 是从后台任务调用的,在编译调用的通知方法后它没有显示

Recyclerview 不会刷新 - 只有当我返回并重新打开活动时 - 才会显示新数据。

那么如何通知适配器有新数据要显示呢?

1.WallpaperFragmentAdapter.java

public class WallpaperFragmentAdapter extends Fragment {

   List<WallpaperMain> wallpaperList = new ArrayList<WallpaperMain>();
   WallPaperAdapter wallPaperAdapter;

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private int tabId ;



    public void settabId(int tabId){
        this.tabId=tabId;
    }

    public static Fragment newInstance(Context context) {

        WallpaperFragmentAdapter f = new WallpaperFragmentAdapter();
        return f;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.content_main, null);
        setUpView(root);
        return root;
    }

    void setUpView(ViewGroup root){
        ButterKnife.bind(this, root);
        setUPList();
    }

    void setUPList(){

        new WallPaperListDownlaoderTask().execute("http://localhost:8080/api/motivation/wallpaper/wallpaper");

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),3);
        wallPaperAdapter = new WallPaperAdapter(wallpaperList,getActivity());
        recyclerView.setAdapter(wallPaperAdapter);
        recyclerView.setLayoutManager(gridLayoutManager);
        System.out.println(wallPaperAdapter);
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {

                Intent intent;
                intent = new Intent(getActivity(), WallpaperDeatilsActivity.class);
                startActivity(intent);
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));


    }

     class WallPaperListDownlaoderTask extends AsyncTask<String,Integer,JSONArray> {

        OkHttpClient client = new OkHttpClient();

         @Override
         protected void onPreExecute() {
             super.onPreExecute();

         }

        @Override
        protected JSONArray doInBackground(String... params) {

            String url =params[0];

            Request request = new Request.Builder()
                    .url(url)
                    .build();
            try{
                Response response = client.newCall(request).execute();
                String apiResponseString=response.body().string();
                System.out.println("response.body().toString() ---- "+apiResponseString );

                JSONArray jsonArray = new  JSONArray(apiResponseString);

                List<WallpaperMain> wallpaperMains = new ArrayList<>();

                for(int index =0;index<jsonArray.length();index++){

                    try {
                        JSONObject wallpaperMainsJsonObj = jsonArray.getJSONObject(index);
                        WallpaperMain wallpaperMain = new WallpaperMain();
                        wallpaperMain.setId(wallpaperMainsJsonObj.getInt("id"));
                        Wallpaper wallpaper = new Wallpaper();

                        JSONObject wallpaperJsonObj = wallpaperMainsJsonObj.getJSONObject("wallpaper");
                        wallpaper.setLarge(wallpaperJsonObj.getString("large"));
                        wallpaperMain.setWallpaper(wallpaper);
                        wallpaperMains.add(wallpaperMain);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                wallpaperList =wallpaperMains;

                return null;

            }catch (IOException e){
                Log.v("",e.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(JSONArray jsonArray) {

           super.onPostExecute(jsonArray);
            System.out.println(wallPaperAdapter);
            wallPaperAdapter.notifyDataSetChanged();
            recyclerView.setAdapter(wallPaperAdapter);
            System.out.println(wallpaperList.size());
        }
    }

}

2.WallPaperAdapter.java

public class WallPaperAdapter extends RecyclerView.Adapter<WallPaperAdapter.MyViewHolder>{


    private List<WallpaperMain> wallpaperMains;
    private Context context;

    public WallPaperAdapter(List<WallpaperMain> wallpaperMains,Context context){
        this.wallpaperMains =wallpaperMains;
        this.context=context;
    }

    @Override
    public WallPaperAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.wallpaper_item, null);
        WallPaperAdapter.MyViewHolder viewHolder = new WallPaperAdapter.MyViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Glide.with(context)
                .load("http://localhost:8080/api/motivation/wallpaper/large/37l")
                .into(holder.waIlpapermageView);

       // holder.waIlpapermageView.setImageResource(R.drawable.name);
    }

    @Override
    public int getItemCount() {
        return wallpaperMains.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {


        ImageView waIlpapermageView;
        public MyViewHolder(View itemView) {
            super(itemView);
            waIlpapermageView =(ImageView) itemView.findViewById(R.id.wallpaperItem_imageView);
        }
    }
}

输出我在咆哮

【问题讨论】:

  • 尝试在执行后重新初始化适配器。壁纸壁纸 = 新壁纸();

标签: android json android-fragments android-recyclerview


【解决方案1】:

在您的 doInBackground 方法中,而不是

wallpaperList = wallpaperMains;

使用

wallpaperList.clear();
wallpaperList.addAll(wallpaperMains);

onPostExecute()

 @Override
   protected void onPostExecute(JSONArray jsonArray) {

 super.onPostExecute(jsonArray);
            System.out.println(wallPaperAdapter);
            wallPaperAdapter.notifyDataSetChanged();
            System.out.println(wallpaperList.size());
        }

【讨论】:

    【解决方案2】:

    试试这个...

    onPreExecute()方法上添加值wallpaperList.clear();之前,请清除您的Arraylist

     recyclerView.setAdapter(wallPaperAdapter);
            wallPaperAdapter.notifyDataSetChanged();
    

    每当您的ArrayList 值发生变化时,usenotifyDataSetChanged(); 都会刷新您的Recylierview

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多