2017-03-12 2 views
0

Ich habe MVVM, Databinding und Retrofit verwendet, um diese App zu entwickeln. Ich habe Daten in RecyclerView geladen. Wenn ich auf einen Artikel in RecyclerView klicke, benötige ich die Position und Details dieser Position. Weil ich diese Informationen in verschiedenen Aktivitäten zeigen werde. Wie kann ich das machen?Wie kann ich Artikelposition und Artikeldetails (Modell) aus RecyclerView auswählen?

Ich brauche Artikelinformationen und Position nach dem Klicken auf diese. Es befindet sich unter Modell anzeigen.

public void onItemClick(View view){ 
    Intent innn=new Intent(view.getContext(),HomeActivity.class); 
    view.getContext().startActivity(innn); 

} 

Ansicht Modell:

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); 

} 



} 

Adapter:

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> 
+2

Mögliches Duplikat von [Position des ViewPager-Objekts mit Datenbindung] (http://stackoverflow.com/questions/39463100/position-of-viewpager-item-with-data-binding) –

Antwort

0

Hier ist, was ich tun, Daten zu übergeben zwischen Fragment, Aktivität oder Adapter. Ich habe eine separate ViewModel erstellt, die für die Datenübergabe verwendet wird.

/** 
* ViewModel used for passing data between different components 
*/ 

public class DataPassingViewModel extends ViewModel { 
    private MutableLiveData<Object> mutableLiveData = new MutableLiveData<>(); 

    public void setData(final Object mObject) { 
    mutableLiveData.setValue(mObject); 
    } 

    public MutableLiveData<Object> getObservable() { 
    return mutableLiveData; 
    } 
} 

es in Aktivität oder Fragment Initialisiere LifecycleOwner über

final DataPassingViewModel mDataPassingViewModel = ViewModelProviders.of(this).get(DataPassingViewModel.class); 

Observer auf die Aktivität ViewModel

mDataPassingViewModel.getObservable().observe(this, object -> { 
     if (object != null) { 
     // Do whatever you want 
     } 
    }); 

In Adapter/Fragment, Initialisieren der DataPassingViewModel mit geordneten Kontext enthält. Für Fragment können Sie über getActivity() Elternkontext erhalten, aber für Adapter können Sie Eltern erhalten, indem Sie es über Konstruktor übergeben. So können Sie eine neue Instanz über erstellen -

mDataPassingViewModel = 
    ViewModelProviders.of(mActivity).get(DataPassingViewModel.class); 

oder

mDataPassingViewModel = 
     ViewModelProviders.of(getActivity()).get(DataPassingViewModel.class); 

Daten zu mDataPassingViewModel.setData(mFileOrFolderTypes.get(position)); passieren zurück. Da setData() akzeptiert generische Objekt, können Sie alles Integer, String oder ganze Klasse übergeben.

Verwandte Themen