用于RecyclerView.ItemDecoration内部视图的Android OnClick Listener

时间:2015-11-15 15:14:01

标签: android android-recyclerview

片段中有RecyclerView。我使用RecyclerView.ItemDecoration将标题设置为我的Recyclerview。参考此link

如何将onClick侦听器设置为ItemDecorators内的视图。

片段OnCreateView()方法初始化recyclerview

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mView = inflater.inflate(R.layout.fragment_navigation_google, container, false);
    ButterKnife.inject(this, mView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
            DividerItemDecoration.VERTICAL_LIST, R.drawable.divider));
    HeaderItemDecorator headerItemDecorator = new HeaderItemDecorator(getActivity(),
            container, R.layout.header_navigation);
    mRecyclerView.addItemDecoration(headerItemDecorator);

    return mView;
}

HeaderItemDecorator类

public class HeaderItemDecorator extends RecyclerView.ItemDecoration {

@InjectView(R.id.user_info_holder)
RelativeLayout mUserInfoHolder;

@InjectView(R.id.user_image)
ImageView mUserImage;

@InjectView(R.id.user_name)
TextView mUserName;

@InjectView(R.id.user_email)
TextView mUserEmail;

View mView;

public HeaderItemDecorator(final Context context, RecyclerView parent, @LayoutRes int resId) {
    // inflate and measure the layout
    mView = LayoutInflater.from(context).inflate(resId, parent, false);
    ButterKnife.inject(this, mView);
    mView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
}

@OnClick({R.id.user_info_parent, R.id.user_image, R.id.user_name, R.id.user_email})
public void onClickListener(View view){
    Log.d("HeaderItemDecorator","onClick::" + view.getId());
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
    // layout basically just gets drawn on the reserved space on top of the first view
    mView.layout(parent.getLeft(), 0, parent.getRight(), mView.getMeasuredHeight());
    for (int i = 0; i < parent.getChildCount(); i++) {
        View view = parent.getChildAt(i);
        if (parent.getChildAdapterPosition(view) == 0) {
            c.save();
            final int height = mView.getMeasuredHeight();
            final int top = view.getTop() - height;
            c.translate(0, top);
            mView.draw(c);
            c.restore();
            break;
        }
    }
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getChildAdapterPosition(view) == 0) {
        outRect.set(0, mView.getMeasuredHeight(), 0, 0);
    } else {
        outRect.setEmpty();
    }
}
}

0 个答案:

没有答案