我使用 MVVM、Databinding 和 Retrofit 来开发这个应用程序。我已将数据加载到 RecyclerView。当我将点击 RecyclerView 上的任何项目时,我需要该职位的职位和详细信息。因为我会在不同的活动中展示这些信息。我怎样才能做到这一点?
单击此后,我需要项目信息和位置。它在视图模型下。
public void onItemClick(View view){
Intent innn=new Intent(view.getContext(),HomeActivity.class);
view.getContext().startActivity(innn);
}
查看型号:
package com.nitolniloygroup.operating.viewmodel;
import android.content.Context;
import android.content.Intent;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.view.View;
import android.widget.Toast;
import com.nitolniloygroup.operating.BR;
import com.nitolniloygroup.operating.model.Movie;
import com.nitolniloygroup.operating.view.activity.HomeActivity;
import com.nitolniloygroup.operating.view.activity.LoginActivity;
/**
* Created by durloveit on 12-Mar-2017.
*/
public class MovieMVVMViewModel extends BaseObservable {
private Movie mMovie;
private Context mContext;
public MovieMVVMViewModel(Movie mMovie, Context mContext) {
this.mMovie = mMovie;
this.mContext = mContext;
}
@Bindable
public String getTitle() {
return mMovie.getTitle();
}
public void setTitle(String title) {
mMovie.setTitle(title);
notifyPropertyChanged(BR.title);
}
@Bindable
public String getSubtitle() {
return mMovie.getReleaseDate();
}
public void setSubtitle(String subtitle) {
mMovie.setReleaseDate(subtitle);
notifyPropertyChanged(BR.subtitle);
}
@Bindable
public String getDescription() {
return mMovie.getOverview();
}
public void setDescription(String description) {
mMovie.setReleaseDate(description);
notifyPropertyChanged(BR.description);
}
public View.OnClickListener onReadMoreClicked(){
return new View.OnClickListener(){
@Override
public void onClick(View view){
//Toast.makeText(view.getContext(), "Button Read More", Toast.LENGTH_SHORT).show();
Intent innn=new Intent(view.getContext(),LoginActivity.class);
view.getContext().startActivity(innn);
}
};
}
public void onItemClick(View view){
Intent innn=new Intent(view.getContext(),HomeActivity.class);
view.getContext().startActivity(innn);
}
}
适配器:
package com.nitolniloygroup.operating.view.adapter;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.nitolniloygroup.operating.databinding.ListItemMoviemvvmBinding;
import com.nitolniloygroup.operating.model.Movie;
import com.nitolniloygroup.operating.viewmodel.MovieMVVMViewModel;
import com.nitolniloygroup.operating.R;
import java.util.List;
/**
* Created by durloveit on 12-Mar-2017.
*/
public class MovieMVVMAdapter extends RecyclerView.Adapter<MovieMVVMAdapter.BindingHolder>{
private List<Movie> mMovies;
private Context mContext;
public MovieMVVMAdapter(List<Movie> mMovies, Context mContext) {
this.mMovies = mMovies;
this.mContext = mContext;
}
@Override
public BindingHolder onCreateViewHolder(ViewGroup parent,int viewType){
ListItemMoviemvvmBinding binding= DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()),
R.layout.list_item_moviemvvm,parent,false);
return new BindingHolder(binding);
}
@Override
public void onBindViewHolder(BindingHolder holder,int position){
ListItemMoviemvvmBinding binding=holder.binding;
binding.setMoviemvvmVM(new MovieMVVMViewModel(mMovies.get(position),mContext));
}
@Override
public int getItemCount(){
return mMovies.size();
}
public static class BindingHolder extends RecyclerView.ViewHolder{
private ListItemMoviemvvmBinding binding;
public BindingHolder(ListItemMoviemvvmBinding binding){
super(binding.cardviewMovieitem);
this.binding=binding;
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="moviemvvmVM"
type="com.nitolniloygroup.operating.viewmodel.MovieMVVMViewModel"></variable>
</data>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{moviemvvmVM::onItemClick}"
android:id="@+id/cardview_movieitem">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="72dp"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingRight="16dp"
android:textStyle="bold"
android:text="@{moviemvvmVM.title}"
android:textSize="16sp" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{moviemvvmVM.subtitle}"
android:paddingRight="16dp"
/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:text="@{moviemvvmVM.description}"
android:paddingRight="16dp"
/>
</LinearLayout>
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:onClick="@{moviemvvmVM.onReadMoreClicked}"
android:text="Read More" />
</LinearLayout>
</android.support.v7.widget.CardView>
</layout>