使用notifyItemChange(位置,有效负载)

问题描述 投票:0回答:1

我需要一个明确的例子,说明在main_activity之外的另一个活动中调用它时如何使用此功能。我在网上搜索过,但我还没有找到完整的例子。始终是高级用户的部分代码。

我需要一个示例,包括适配器代码以及如何从另一个活动调用该功能。

编辑:添加了我到目前为止的代码,但我真的不知道如何使它工作

适配器:

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder>{
private List<Movie> moviesList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, year, genre;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        genre = (TextView) view.findViewById(R.id.genre);
        year = (TextView) view.findViewById(R.id.year);
    }
}


public MoviesAdapter(List<Movie> moviesList) {
    this.moviesList = moviesList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.movie_list_row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Movie movie = moviesList.get(position);
    holder.title.setText(movie.getTitle());
    holder.genre.setText(movie.getGenre());
    holder.year.setText(movie.getYear());
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position, List<Object> payloads) {
    if (!payloads.isEmpty()) {
        if (payloads.get(0) instanceof Movie){
            Movie mymovie=(Movie)payloads.get(0);
            holder.title.setText(mymovie.getTitle());}

    } else {
        super.onBindViewHolder(holder, position, payloads);
    }
}

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

}

主要活动:

public class MainActivity extends AppCompatActivity {
private List<Movie> movieList = new ArrayList<>();
private RecyclerView recyclerView;
private MoviesAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    mAdapter = new MoviesAdapter(movieList);

    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());

    recyclerView.setLayoutManager(mLayoutManager);


    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));


    recyclerView.setItemAnimator(new DefaultItemAnimator());

    recyclerView.setAdapter(mAdapter);


    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
        int mposition;
        @Override
        public void onClick(View view, int position) {
            mposition=position;
            Movie movie = movieList.get(position);
            Toast.makeText(getApplicationContext(), movie.getTitle() + " is selected!", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(MainActivity.this,ChangeMovie.class);
            intent.putExtra("passposition",mposition);
            startActivity(intent);
        }

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

        }
    }));

    prepareMovieData();
}


private void prepareMovieData() {
    Movie movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "2015");
    movieList.add(movie);

    movie = new Movie("Inside Out", "Animation, Kids & Family", "2015");
    movieList.add(movie);

    movie = new Movie("Star Wars: Episode VII - The Force Awakens", "Action", "2015");
    movieList.add(movie);

    movie = new Movie("Shaun the Sheep", "Animation", "2015");
    movieList.add(movie);

    movie = new Movie("The Martian", "Science Fiction & Fantasy", "2015");
    movieList.add(movie);

    movie = new Movie("Mission: Impossible Rogue Nation", "Action", "2015");
    movieList.add(movie);

    movie = new Movie("Up", "Animation", "2009");
    movieList.add(movie);

    movie = new Movie("Star Trek", "Science Fiction", "2009");
    movieList.add(movie);

    movie = new Movie("The LEGO Movie", "Animation", "2014");
    movieList.add(movie);

    movie = new Movie("Iron Man", "Action & Adventure", "2008");
    movieList.add(movie);

    movie = new Movie("Aliens", "Science Fiction", "1986");
    movieList.add(movie);

    movie = new Movie("Chicken Run", "Animation", "2000");
    movieList.add(movie);

    movie = new Movie("Back to the Future", "Science Fiction", "1985");
    movieList.add(movie);

    movie = new Movie("Raiders of the Lost Ark", "Action & Adventure", "1981");
    movieList.add(movie);

    movie = new Movie("Goldfinger", "Action & Adventure", "1965");
    movieList.add(movie);

    movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014");
    movieList.add(movie);

    // notify adapter about data set changes
    // so that it will render the list with new data
    mAdapter.notifyDataSetChanged();
}

}

我在“Recyclerview”中输入我想要更改的新电影标题的“其他活动”

public class ChangeMovie extends AppCompatActivity {

EditText newmoviefield;
String newmovie;
Button savebutton;
MoviesAdapter mAdapter;
private static final String TAG="changemovie";
private List<Movie> movieList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_movie);
    mAdapter = new MoviesAdapter(movieList);

    final int position=(int)getIntent().getExtras().get("passposition");
    newmoviefield=(EditText)findViewById(R.id.Title_field);
    savebutton=(Button)findViewById(R.id.btn_save);



    savebutton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view){
            newmovie=newmoviefield.getText().toString();
            Movie changemovie=new Movie(newmovie,"test1","test2");
            Log.v(TAG,"movieafter"+changemovie.getTitle());
            mAdapter.notifyItemChanged(position,(Object)changemovie);
            finish();
        }
    });
}

}

和movie.class

public class Movie {
private String title, genre, year;

public Movie() {
}

public Movie(String title, String genre, String year) {
    this.title = title;
    this.genre = genre;
    this.year = year;
}

public String getTitle() {
    return title;
}

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

public String getYear() {
    return year;
}

public void setYear(String year) {
    this.year = year;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}

}

在此先感谢您的帮助

android
1个回答
1
投票

最好在android中使用未来的onactivityresult

它易于使用在您的回收项目点击方法下进行以下更改

制作电影模型类实现serializable

将以下更改应用于您的MainActivity:

Movie movie = movieList.get(mposition);
Intent intent=new Intent(MainActivity.this,ChangeMovie.class);
intent.putExtra("passposition",mposition);
intent.putExtra("movie",movie);
startActivityForResult(i, 100);

覆盖以下方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 100 && resultCode == Activity.RESULT_OK) {

    int listpos = mIntent.getIntExtra("listpos", --1);
    if(listpos!=-1){

        Movie movie = (Movie) data.getSerializable("movie");

        movieList.set(listpos,movie);

        if(mAdapter!=null){
        mAdapter.notifyDataSetChanged
        }
    }
}
}

在ChangeMovie中应用以下更改

获取on create方法中的列表位置

int pos = -1;
int mposition = getIntent().getExtra().getInt("mposition");

在您单击保存按钮

newmovie=newmoviefield.getText().toString();
Intent intent = new Intent();
intent.putExtra("movie", changemovie);
intent.putExtra("passposition",mposition);
setResult(RESULT_OK, intent);
finish();

让我知道是否还有问题,因为手动编写代码可能是你必须纠正一些区分大小写的谢谢


推荐问答