简体   繁体   中英

How can I add onClickListener in Materialviewpage Adapter?

I am using this library from github: https://github.com/florent37/MaterialViewPager . I want the diffrent card views to open diffrent activities. How can I achieve it?

I couldn"t find any solution in wiki there

TestRecyclerViewAdapter.java

public class TestRecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {

List<Object> contents;

static final int TYPE_HEADER = 0;
static final int TYPE_CELL = 1;

public TestRecyclerViewAdapter(List<Object> contents) {
    this.contents = contents;
}

@Override
public int getItemViewType(int position) {
    switch (position) {
        case 0:
            return TYPE_HEADER;
        default:
            return TYPE_CELL;
    }
}

@Override
public int getItemCount() {
    return contents.size();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;

    switch (viewType) {
        case TYPE_HEADER: {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item_card_big, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        }
        case TYPE_CELL: {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item_card_small, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        }
    }
    return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (getItemViewType(position)) {
        case TYPE_HEADER:
            break;
        case TYPE_CELL:
            break;
    }
}
}

Well, if you don't want to implement a custom viewHolder you can do something like this in the onBindViewHolder(RecyclerView.ViewHolder holder, int position) :

holder.itemView.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        //start which activity you want
    }
})

Otherwise you can implement a custom ViewHolder. Here is a guide: https://developer.android.com/guide/topics/ui/layout/recyclerview

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