我想为一个网站开发Android应用程序。对于加载数据我使用RecyclerView
,我希望当用户滑动 Recyclerview
更新数据时。我写这段代码,但我不知道写更新代码并在onRefresh
方法中使用!
我的适配器代码:
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
private List<MainDataModel> mDateSet;
private Context mContext;
public MainAdapter(Context context) {
this.mContext = context;
this.mDateSet = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.post_card_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.main_post_title.setText(mDateSet.get(position).getTitle());
Glide.with(mContext)
.load(mDateSet.get(position).getThumbnail())
.placeholder(R.drawable.post_image)
.into(holder.main_post_image);
}
@Override
public int getItemCount() {
return mDateSet.size();
}
public void remove(int position){
mDateSet.remove(position);
notifyItemRemoved(position);
}
public void add(List<MainDataModel> models){
mDateSet.addAll(models);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView main_post_title;
private ImageView main_post_image;
public ViewHolder(View itemView) {
super(itemView);
main_post_title = (TextView) itemView.findViewById(R.id.post_content_title);
main_post_image = (ImageView) itemView.findViewById(R.id.post_picture_image);
}
}
}
主要活动代码:
public class Main_page extends AppCompatActivity {
private static final long RIPPLE_DURATION = 250;
private Toolbar toolbar;
private RelativeLayout root;
private ImageView menu_image;
private SuperRecyclerView main_recyclerView;
private MainAdapter mAdaper;
private List<MainDataModel> dataModels;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
toolbar = (Toolbar) findViewById(R.id.main_toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
}
// RecyclerView and setData
main_recyclerView = (SuperRecyclerView) findViewById(R.id.main_recycler);
//main_recyclerView.setHasFixedSize(true);
main_recyclerView.setLayoutManager(new LinearLayoutManager(this));
MainDataInfo dataInfo = new MainDataInfo();
dataInfo.getMainDataInfo(this);
mAdaper = new MainAdapter(this);
main_recyclerView.setAdapter(mAdaper);
main_recyclerView.setupMoreListener(new OnMoreListener() {
@Override
public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
}
}, 3);
main_recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh () {
}});
}
@Subscribe
public void onEvent(List<MainDataModel> mainInfoModels) {
mAdaper.add(mainInfoModels);
}
}
如何在Adapter中编写更新方法并使用它onRefresh
方法?
注意:请不要给我负面积分,我是业余爱好者,我真的需要你!谢谢所有&lt; 3
答案 0 :(得分:3)
我建议您开始做一些让RecyclerView正常工作的事情:
mAdapter.notifyItemChanged(...)
,mAdapter.notifyItemInserted(...)
和mAdapter.notifyItemRemoved(...)
。这些会导致关联的ViewHolder重新绑定其视图数据。所以你的代码应该是这样的:
的活动强>
// I recommend calling this class MainPageActivity instead for consistency's sake
public class Main_page extends AppCompatActivity {
MainAdapter mAdapter;
List<MainDataModel> mItems = new ArrayList<MainDataModel>();
// Use this to change an item in the list
public void setItem(int index,MainDataModel item)
{
mItems.set(index, item);
mAdapter.notifyItemChanged(index);
}
// Use this to add an item
public void addItem(MainDataModel item)
{
mItems.add(item);
mAdapter.notifyItemInserted(mItems.size() - 1);
}
// Use this to remove an item
public void removeItem(int index)
{
mItems.remove(index);
mAdapter.notifyItemRemoved(index);
}
// ... The rest of your code
}
<强>适配器强>
class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>
{
private Context mContext;
private List<MainDataModel> mDateSet;
public MainAdapter(Context context, List<MainDataModel> dataSet)
{
this.mContext = context;
this.mDataSet = dataSet;
}
// ... The rest of your code
}
答案 1 :(得分:2)
您只需更改数据集中的项目并在适配器中调用相关的通知方法。
在您的示例中,您有mAdaper
作为适配器,dataModels
作为数据集。
更改说,dataModels
的索引3。 dataModels.get(2).changeSomething();
然后您需要tell the adapter更改该索引。 mAdaper.notifyItemChanged(2)
。
顺便提一下,我建议您在编写代码时坚持使用一种代码样式标准(并进行一些拼写检查)。例如,在某些地方,您的前缀为m
,有时您不会这样做。