我对设置RecyclerView的适配器有疑问,我在一些教程中看到他们制造了Adapter,然后在与他们设置信息的RecyclerView相同的类中制造。但是我的老师用另一种方式做到这一点,他制作了4个类,即适配器,然后这个类获得名为MovieServices的所有信息:
public class MoviesService
{
public static MoviesService moviesService;
public static MoviesService getInstance()
{
if ( moviesService == null )
{
moviesService = new MoviesService();
}
return moviesService;
}
private List<Movie> movies;
private MoviesService()
{
this.movies = new ArrayList<>();
movies.add( new Movie("001", "El hombre que mató a Liberty Valance", 9.8) );
movies.add( new Movie("002", "Río Rojo", 9.2) );
movies.add( new Movie("003", "Río Bravo", 9.9) );
movies.add( new Movie("004", "La diligencia", 9.1) );
movies.add( new Movie("005", "Que verde era mi valle", 9.8) );
movies.add( new Movie("006", "El hombre tranquilo", 9.3) );
movies.add( new Movie("007", "Misión de audaces", 9) );
movies.add( new Movie("008", "La taberna del irlandés", 8.6) );
movies.add( new Movie("009", "El Padrino", 9.5) );
movies.add( new Movie("010", "El Padrino II", 9.6) );
movies.add( new Movie("011", "El Padrino III", 9.7) );
movies.add( new Movie("012", "Los increibles", 9.2) );
movies.add( new Movie("013", "Los increibles II", 8.9) );
movies.add( new Movie("014", "Coco", 9.6) );
}
public List<Movie> getMovies()
{
return movies;
}
public Movie getMovie(int position)
{
return movies.get( position );
}
public void update(int moviePosition, Movie movie)
{
movies.set(moviePosition, movie);
}
}
然后他又叫另一个班级
public class MainViewModel extends ViewModel
{
private MoviesService moviesService;
public MainViewModel()
{
moviesService = MoviesService.getInstance();
}
public List<Movie> getMovies()
{
return moviesService.getMovies();
}
public Movie getMovie(int position)
{
return moviesService.getMovie( position );
}
}
最后是具有RecyclerView的类:
public class MainActivity extends AppCompatActivity implements MovieAdapter.OnItemClickListener
{
private MainViewModel viewModel;
private RecyclerView rv;
private RecyclerView.LayoutManager layoutManager;
private MovieAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
this.rv = findViewById(R.id.rv);
this.layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
this.adapter = new MovieAdapter(this);
this.adapter.setOnItemClickListener(this);
rv.setAdapter( adapter );
rv.setLayoutManager( layoutManager );
adapter.setMovies( viewModel.getMovies() );
}
@Override
public void onItemClick(View view, int position) {
Movie movie = this.viewModel.getMovie(position);
Toast.makeText(this, movie.getName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MovieActivity.class );
intent.putExtra(MovieActivity.EXTRA_MOVIE_POSITION, position);
startActivityForResult( intent, 88 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch ( requestCode )
{
case 88: if ( resultCode == Activity.RESULT_OK)
{
adapter.setMovies( viewModel.getMovies() );
}
break;
}
}
}
但是我不确定哪种方式会更优化,因为我认为他做了很多我认为不必要的事情。谢谢!
答案 0 :(得分:0)
最好的方法是按照以下步骤创建单独的元素,以进行清晰的编码:
1-创建一个单独的自定义对象类
2-创建一个单独的RecyclerView.Adapter
,将数据填充到RecyclerView
3-在单独的活动中将适配器绑定到RecyclerView