0

我使用 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>
4

1 回答 1

1

这是我在片段、活动或适配器之间传递数据的方法。我创建了一个单独ViewModel的用于数据传递的。

/**
 * 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;
  }
}

LifecycleOwner在包含via的 Activity 或 Fragment 中初始化它

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

观察员上ViewModel

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

在 Adapter/Fragment 中,DataPassingViewModel使用父活动的上下文初始化。对于片段,您可以通过获取父上下文,getActivity()但对于适配器,您可以通过构造函数传递它来获取父上下文。因此,您可以通过以下方式创建一个新实例 -

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

或者

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

将数据传回mDataPassingViewModel.setData(mFileOrFolderTypes.get(position));。由于接受通用对象,setData()因此您可以传递任何东西或整个类。IntegerString

于 2018-02-04T06:13:38.137 回答