简体   繁体   中英

access RecyclerView.Adapter's method from ViewHolder or ViewModel of ViewHolder

I need to access a method of RecyclerView Adapter from View Holder. I don't find any solution for this.

Or is it possible to access adpater'method(get an instance of adapter) from ViewModel class (I've defined a viewModel for items of RecyclerView in MVVM pattern) of ViewHolder.

Regards, Habib

Thanks to all, here is code snippets. in ItemViewModel class I want to get adapter's methods.

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ........

        public MyViewHolder(View view) {
            super(view);
             ......
        }
    }

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
           ........
    }

    @Override
    public int getItemCount() {
        .........
    }
}

<?xml version="1.0" encoding="utf-8"?>

<data>
    <variable
        name="viewModel"
        type="program.viewmodel.ItemViewModel" />
</data>

<android.support.v7.widget.CardView
    android:id="@+id/cvMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="@{viewModel.onClickCard()}"
    android:onLongClick="@{viewModel.onLongClickCard()}" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingLeft="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/engM"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:paddingLeft="5dp"
                android:text="@{viewModel.engligh}"
                android:textSize="20sp" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

public class ItemViewModel{
    private Proverb proverb; //Proverb is model class

    public ItemViewModel(Proverb item) {
        this.proverb = item;
        notifyChange();
    }  

    @Bindable
    public String getEngligh() {
        return proverb.getEngligh();
    }

    /**
     * click each item
     * @return
     */
    public View.OnClickListener onClickProverb() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {            
                // here I need to access adapter's method               
            }
        };
    }

    /**
     * long click listener    
     * @return
     */
    public View.OnLongClickListener onLongClickProverb() {
        return new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {     
                // here I need to access adapter's method               
                return true;
            }
        };
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM