如何将数据添加到RecyclerView中

时间:2017-10-20 15:39:38

标签: android android-recyclerview recycler-adapter

在我的申请中,我想将两个recyclerView合并为一个Activity 我希望在点击其中一个recylerView's项目的项目时,将项目添加到另一个recyclerView
我在下面的代码中写下,当点击项目时,只需将持续的项目信息添加到另一个recyclerView我希望 点击每个项目时,将每个项目添加到另一个recyclerView ,而不仅仅是添加持续项目
现在只需添加已删除的项目,但我想点击每个项目添加此项目。

我的活动代码:

public class SuggestFilmActivity extends AppCompatActivity implements SuggestedListener {

    @BindView(R.id.toolbarTitleTxt)
    TextView toolbarTitleTxt;
    @BindView(R.id.suggestFilm_searchEditText)
    EditText suggestFilm_searchEditText;
    @BindView(R.id.suggestFilm_searchBtn)
    ImageView suggestFilm_searchBtn;
    @BindView(R.id.suggestFilm_recyclerView)
    RecyclerView suggestFilm_recyclerView;
    @BindView(R.id.suggestFilm_recyclerViewProgress)
    ProgressBar suggestFilm_recyclerViewProgress;
    @BindView(R.id.newsPageLoadLay)
    RelativeLayout newsPageLoadLay;
    @BindView(R.id.suggestFilm_recyclerViewSendUser)
    RecyclerView suggestFilm_recyclerViewSendUser;
    private Context context;
    private SuggestFilmAdapter suggestFilmAdapter;
    private SuggestFilmUserAdapter suggestFilmUserAdapter;
    private List<Result> model = new ArrayList<>();
    private InterfaceApi api;
    private SharedPrefrencesHandler prefrencesHandler;
    private String token;
    private GridLayoutManager gridLayoutManager;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggest_film);
        //Initialize
        ButterKnife.bind(this);
        context = this;
        prefrencesHandler = new SharedPrefrencesHandler(context);
        api = ApiClient.getClient().create(InterfaceApi.class);
        suggestFilmAdapter = new SuggestFilmAdapter(context, model, this);
        gridLayoutManager = new GridLayoutManager(context, 3);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        //Get token
        token = prefrencesHandler.getFromShared(SharedPrefrencesKeys.TOKEN.name());
        //Set toolbar title
        toolbarTitleTxt.setText(context.getResources().getString(R.string.SuggestToFollowers));
        //Init followers recyclerView
        suggestFilm_recyclerView.setLayoutManager(gridLayoutManager);
        suggestFilm_recyclerView.setHasFixedSize(true);
        //Init send user recyclerView
        suggestFilm_recyclerViewSendUser.setLayoutManager(linearLayoutManager);
        suggestFilm_recyclerViewSendUser.setHasFixedSize(true);
        //Load more
        newsPageLoadLay.setVisibility(View.GONE);
        suggestFilm_recyclerView.setOnScrollListener(new EndlessRecyclerGridPage1(gridLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {

                newsPageLoadLay.setVisibility(View.VISIBLE);

                Call<SeriesWhoWatchedResponse> call = api.getSuggestFilmUsers(token, filmSendData(current_page));

                call.enqueue(new Callback<SeriesWhoWatchedResponse>() {
                    @Override
                    public void onResponse(Call<SeriesWhoWatchedResponse> call, Response<SeriesWhoWatchedResponse> response) {
                        if (response.body().getData() != null && response.body().getStatusCode() != 401
                                && response.body().getStatusCode() != 402) {
                            if (response.body().getData().getResult().size() > 0) {
                                suggestFilmAdapter.addNewItem(response.body().getData().getResult());
                                //Gone no explore
                                newsPageLoadLay.setVisibility(View.GONE);
                            }
                        } else {
                            prefrencesHandler.remove(SharedPrefrencesKeys.TOKEN.name());
                            startActivity(new Intent(context, LoginActivity.class));
                        }

                        newsPageLoadLay.setVisibility(View.GONE);
                    }

                    @Override
                    public void onFailure(Call<SeriesWhoWatchedResponse> call, Throwable t) {
                        newsPageLoadLay.setVisibility(View.GONE);
                    }
                });
            }
        });
        //Get user data
        getUserData();
    }

    private void getUserData() {
        suggestFilm_recyclerViewProgress.setVisibility(View.VISIBLE);
        Call<SeriesWhoWatchedResponse> call = api.getSuggestFilmUsers(token, filmSendData(1));
        call.enqueue(new Callback<SeriesWhoWatchedResponse>() {
            @Override
            public void onResponse(Call<SeriesWhoWatchedResponse> call, Response<SeriesWhoWatchedResponse> response) {
                if (response.body().getData() != null && response.body().getData().getResult().size() > 0
                        && response.body().getStatusCode() != 401 && response.body().getStatusCode() != 402) {
                    model.clear();
                    model.addAll(response.body().getData().getResult());
                    suggestFilmAdapter.notifyDataSetChanged();
                    suggestFilm_recyclerView.setAdapter(suggestFilmAdapter);

                } else {
                    prefrencesHandler.remove(SharedPrefrencesKeys.TOKEN.name());
                    startActivity(new Intent(context, LoginActivity.class));
                }
                suggestFilm_recyclerViewProgress.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<SeriesWhoWatchedResponse> call, Throwable t) {
                suggestFilm_recyclerViewProgress.setVisibility(View.GONE);
            }
        });
    }

    private SuggestFilmSendData filmSendData(int page) {
        SuggestFilmSendData sendData = new SuggestFilmSendData();
        sendData.setKeyword("");
        sendData.setPageIndex(page);
        sendData.setPageSize(10);

        return sendData;
    }

    private ArrayList<SuggestFilmAddUser> prepareData(int id, String name, String image) {

        ArrayList<SuggestFilmAddUser> suggestFilmAddUserList = new ArrayList<>();

        SuggestFilmAddUser suggestFilmAddUser = new SuggestFilmAddUser();
        suggestFilmAddUser.setId(id);
        suggestFilmAddUser.setName(name);
        suggestFilmAddUser.setImage(image);
        suggestFilmAddUserList.add(suggestFilmAddUser);
        return suggestFilmAddUserList;
    }

    @Override
    public void onSend(int Id, String name, String image) {
        ArrayList<SuggestFilmAddUser> suggestFilmAddUserList = prepareData(Id, name, image);
        suggestFilmUserAdapter = new SuggestFilmUserAdapter(context, suggestFilmAddUserList);
        suggestFilm_recyclerViewSendUser.setAdapter(suggestFilmUserAdapter);
    }
}

一个recyclerView适配器并发送数据接口代码:

public class SuggestFilmAdapter extends RecyclerView.Adapter<SuggestFilmAdapter.ViewHolder> {

    private Context context;
    private List<Result> model;
    private SuggestedListener suggestedListener;

    public SuggestFilmAdapter(Context context, List<Result> model, SuggestedListener suggestedListener) {
        this.context = context;
        this.model = model;
        this.suggestedListener = suggestedListener;
    }

    @Override
    public SuggestFilmAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_suggest_film_users_followers, parent, false);

        return new SuggestFilmAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SuggestFilmAdapter.ViewHolder holder, final int position) {
        //Name
        holder.row_suggestFilmProfileName.setText(model.get(position).getName());
        //Image
        Glide.with(context)
                .load(model.get(position).getImageUrl())
                .asBitmap()
                .placeholder(R.drawable.default_image)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .override(300, 300)
                .into(new BitmapImageViewTarget(holder.row_suggestFilmProfileImage) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        if (context == null) return;

                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        holder.row_suggestFilmProfileImage.setImageDrawable(circularBitmapDrawable);
                    }
                });
        //Is Mutual
        if (model.get(position).getIsMutual()) {
            holder.row_suggestFilmIsOk.setVisibility(View.VISIBLE);
        } else {
            holder.row_suggestFilmIsOk.setVisibility(View.GONE);
        }
        holder.row_suggestedLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                suggestedListener.onSend(model.get(position).getUserId(),
                        model.get(position).getName(),
                        model.get(position).getImageUrl());
            }
        });
    }

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

    public void addNewItem(List<Result> newContent) {
        int start = this.model.size();
        int end = newContent.size();
        model.addAll(newContent);
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView row_suggestFilmProfileImage, row_suggestFilmIsOk;
        private TextView row_suggestFilmProfileName;
        private RelativeLayout row_suggestedLay;

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

            row_suggestFilmProfileImage = (ImageView) itemView.findViewById(R.id.row_suggestFilmProfileImage);
            row_suggestFilmIsOk = (ImageView) itemView.findViewById(R.id.row_suggestFilmIsOk);
            row_suggestFilmProfileName = (TextView) itemView.findViewById(R.id.row_suggestFilmProfileName);
            row_suggestedLay = (RelativeLayout) itemView.findViewById(R.id.row_suggestedLay);
        }
    }
}

模特课程:

public class SuggestFilmAddUser {

    private int id;
    private String name;
    private String image;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

我该怎么办?请帮帮我

1 个答案:

答案 0 :(得分:0)

在onSend()中,您需要将每个对象添加到应该全局声明的arraylist中。然后在适配器中调用notifyItemInserted(arraylist.size() - 1)来刷新recyclerview

 public class SuggestFilmActivity ....
 { ArrayList<SuggestFilmAddUser> suggestFilmAddUserList=new ArrayList();
   SuggestFilmUserAdapter suggestFilmUserAdapter;

   ......
   ......
 protected void onCreate(Bundle savedInstance)
 {....
  ....
  ....
  suggestFilmUserAdapter = new SuggestFilmUserAdapter(context, 
 suggestFilmAddUserList);
    suggestFilm_recyclerViewSendUser.setAdapter(suggestFilmUserAdapter);
  .....
  .....
 }

 public void onSend(int Id, String name, String image) {
  SuggestFilmAddUser suggestFilmAddUser = new SuggestFilmAddUser();
    suggestFilmAddUser.setId(id);
    suggestFilmAddUser.setName(name);
    suggestFilmAddUser.setImage(image);
   suggestFilmAddUserList.add(suggestFilmAddUser);
   suggestFilmUserAdapter.notifyItemInserted(suggestFilmAddUserList.size()-1) 
}