我的应用程序在插入项目后无法更新视图。我目前正在使用MVP来构建我的应用程序。我遵循了this指南。作者没有提供任何有关从列表中插入或删除项目的进一步说明。
我已经将RecyclerView更新到最新版本(recyclerview-v7:28.0.0)。我已经检查了布局并向下滚动。在最近的工作中,我知道列表已成功添加,但是我的getItemCount
尚未更新。我将模型插入模型中,然后在Activity(notifyItemInserted)中通知适配器。我尝试使用notifyDataSetChanged
,但没有任何效果。
public class MainActivity extends AppCompatActivity implements MVPInterface.viewActivity{
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState){
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
adapter = new WordAdapter(this);
recyclerView.setAdapter(adapter);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
mPresenter = new ActivityPresenter(this);
addButton = (FloatingActionButton) findViewById(R.id.fab);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPresenter.onAddList();
}
});
}
@Override
public void AddSuccess(int size){
Log.d("HELLO","createthis "+size);
recyclerView.getAdapter().notifyItemInserted(size);
recyclerView.smoothScrollToPosition(size);
recyclerView.getAdapter().notifyDataSetChanged();
}
}
class WordAdapter extends RecyclerView.Adapter<WordAdapter.WordViewHolder>{
@NonNull
@Override
public WordViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Log.d("CREATE","create");
android.view.View mItemView = mInflater.inflate(R.layout.content_main, viewGroup, false);
return new WordViewHolder(mItemView, this);
}
@Override
public void onBindViewHolder(@NonNull WordViewHolder viewHolder, int position) {
Log.d("CREATE1","bind");
mPresenter.onSetView(viewHolder, position);
}
@Override
public int getItemCount() {
int size = mPresenter.getListSize();
Log.d("item", ""+size);
return mPresenter.getListSize();
}
public class WordViewHolder extends RecyclerView.ViewHolder implements MVPInterface.WordHolder, android.view.View.OnClickListener {
public TextView wordItemView;
final WordAdapter mAdapter;
public WordViewHolder(android.view.View mItemView, WordAdapter wordAdapter) {
super(mItemView);
wordItemView = (TextView) mItemView.findViewById(R.id.word);
this.mAdapter = wordAdapter;
mItemView.setOnClickListener(this);
}
@Override
public void setText(String string) {
wordItemView.setText(string);
}
@Override
public void onClick(android.view.View v) {
wordItemView.setText ("Clicked! "+ wordItemView.getText());
}
}
}
public class AdapterPresenter {
private LinkedList<String> wordList;
private WordModel model;
public AdapterPresenter(){
this.model = new WordModel();
this.wordList = model.initList();
}
public void onSetView(MVPInterface.WordHolder holder, int position) {
holder.setText(wordList.get(position));
}
public int getListSize(){
return model.getSize();
}
}
public class WordModel {
private LinkedList<String> wordList = new LinkedList<>();
private MVPInterface.viewActivity activity;
private boolean isFirst = true;
//private Listener listener;
public WordModel(){
this.wordList = initList();
}
public LinkedList<String> initList(){
if(isFirst){
for(int i = 0; i<20; i++){
wordList.addLast("Word "+i);
}
isFirst = false;
}
return wordList;
}
public LinkedList<String> getWordList(){
return wordList;
}
public int getSize(){
return wordList.size();
}
public void addList(Listener listener){
wordList.add("+ Word "+ getSize());
listener.onAddSuccess();
}
}
public class ActivityPresenter implements Listener {
private WordModel wordModel;
private MVPInterface.viewActivity activity;
public ActivityPresenter(MVPInterface.viewActivity activity){
this.activity = activity;
this.wordModel = new WordModel();
}
public void onAddList() {
wordModel.addList(this);
}
@Override
public void onAddSuccess() {
int size = wordModel.getSize();
activity.AddSuccess(size);
}
}
已成功插入列表(创建此logcat消息),但getItemCount
保持不变(CREATEItem)。
我怀疑AdapterPresenter内部可能有问题。我在演示者内部初始化了wordModel,因此我的列表没有动态更改(例如在logcat中,getItemCount不会更新为最新大小)。但是我仍然不知道到底是什么问题。
答案 0 :(得分:1)
我认为您应该删除var url_new;
if(window.location.href.indexOf("product") > -1) {
var url = window.location.href.substr(window.location.href.indexOf("?") +1, window.location.href.length);
var newValue = 10;
url_new = 'https://example.com/?modal-link=' + newValue + "&" + url
}
console.log(url_new);
或将其替换为setHasFixedSize(true)