RecyclerView项目布局截取当前ItemLayout的屏幕截图

时间:2016-10-12 12:11:57

标签: android android-recyclerview

我是Android开发的新手,我有一个recyclerview,其中我正在膨胀不同的自定义视图,如Need Employee,NeedCofounder等。 我正在尝试添加一项功能,以便在社交媒体上分享这些帖子,我无法弄清楚如何获取用户选择分享的当前视图。谢谢

package com.devs.bevy.homeScreen.holders;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.devs.bevy.R;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class BevyPostViewHolder {

    public static class MainHolder extends RecyclerView.ViewHolder{
        public @Nullable @BindView(R.id.userImage) CircleImageView userImage;
        public @Nullable @BindView(R.id.userName) AppCompatTextView userName;
        public @Nullable @BindView(R.id.postTime) AppCompatTextView postTime;
        public @Nullable @BindView(R.id.postType) AppCompatTextView postType;
        public @Nullable @BindView(R.id.btnAddToWishList) AppCompatCheckBox btnAddToWishList;
        public @Nullable @BindView(R.id.messageWindow) AppCompatImageView messageWindow;
        public @Nullable @BindView(R.id.commentWindow) AppCompatImageView commentWindow;
        public @Nullable @BindView(R.id.commentCount) AppCompatTextView commentCount;
        public @Nullable @BindView(R.id.markDoneUndone) AppCompatCheckBox markDoneUndone;
        public @Nullable @BindView(R.id.deletePost) AppCompatImageView deletePost;
        public @Nullable @BindView(R.id.sharePost) AppCompatImageView sharePost;

        public MainHolder(View itemView) {
            super(itemView);
        }
    }

    public static class NeedCoFounder extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.experience) AppCompatTextView experience;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.equity) AppCompatTextView equity;
        public @BindView(R.id.salary) AppCompatTextView salary;
        public @BindView(R.id.description) AppCompatTextView description;

        public NeedCoFounder(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class NeedEmployee extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.experience) AppCompatTextView experience;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.equity) AppCompatTextView equity;
        public @BindView(R.id.salary) AppCompatTextView salary;
        public @BindView(R.id.description) AppCompatTextView description;

        public NeedEmployee(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class NeedIntern extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.stipend) AppCompatTextView stipend;
        public @BindView(R.id.description) AppCompatTextView description;

        public NeedIntern(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class NeedFreelancer extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.experience) AppCompatTextView experience;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.price) AppCompatTextView price;
        public @BindView(R.id.description) AppCompatTextView description;

        public NeedFreelancer(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class NeedOffice extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.type) AppCompatTextView type;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.price) AppCompatTextView price;

        public NeedOffice(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class NeedEntities extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.type) AppCompatTextView type;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.price) AppCompatTextView price;
        public @BindView(R.id.entitiesName) AppCompatTextView entitiesName;

        public NeedEntities(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class NeedMentor extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.speciality) AppCompatTextView speciality;
        public @BindView(R.id.description) AppCompatTextView description;

        public NeedMentor(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class OfferCoFounder extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.experience) AppCompatTextView experience;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.description) AppCompatTextView description;

        public OfferCoFounder(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class OfferEmployee extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.experience) AppCompatTextView experience;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.salary) AppCompatTextView salary;

        public OfferEmployee(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class OfferIntern extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.stipend) AppCompatTextView stipend;
        public @BindView(R.id.description) AppCompatTextView description;

        public OfferIntern(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static class OfferFreelancer extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.experience) AppCompatTextView experience;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.price) AppCompatTextView price;
        public @BindView(R.id.description) AppCompatTextView description;

        public OfferFreelancer(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class OfferOffice extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.type) AppCompatTextView type;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.price) AppCompatTextView price;
        public @BindView(R.id.officeImage) SimpleDraweeView officeImage;
        public @BindView(R.id.officeImageCount) AppCompatTextView officeImageCount;
        public @BindView(R.id.imageLayout) RelativeLayout imageLayout;

        public OfferOffice(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class OfferEntities extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.type) AppCompatTextView type;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.price) AppCompatTextView price;
        public @BindView(R.id.entitiesImage) SimpleDraweeView entitiesImage;
        public @BindView(R.id.entitiesImageCount) AppCompatTextView entitiesImageCount;
        public @BindView(R.id.imageLayout) RelativeLayout imageLayout;

        public OfferEntities(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static class OfferMentor extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.speciality) AppCompatTextView speciality;
        public @BindView(R.id.description) AppCompatTextView description;

        public OfferMentor(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static class GpIdeaValidation extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.domain) AppCompatTextView domain;
        public @BindView(R.id.title) AppCompatTextView title;
        public @BindView(R.id.query) AppCompatTextView query;

        public GpIdeaValidation(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class GpOptimizeCashFlow extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.title) AppCompatTextView title;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpOptimizeCashFlow(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpTeam extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.profile) AppCompatTextView profile;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpTeam(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class GpCompetitorAnalysis extends MainHolder {

        public GpCompetitorAnalysis(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class GpProductPricing extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.currentPrice) AppCompatTextView currentPrice;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.shortDescription) AppCompatTextView shortDescription;

        public GpProductPricing(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpProductReview extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.website) AppCompatTextView website;
        public @BindView(R.id.questions) AppCompatTextView questions;

        public GpProductReview(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpBusinessModel extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.title) AppCompatTextView title;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpBusinessModel(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpMarketing extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.title) AppCompatTextView title;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpMarketing(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpCustomerVoices extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpCustomerVoices(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpMarketAnalysis extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.role1) AppCompatTextView role1;
        public @BindView(R.id.location) AppCompatTextView location;
        public @BindView(R.id.title) AppCompatTextView title;
        public @BindView(R.id.when) AppCompatTextView when;

        public GpMarketAnalysis(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpGetMotivation extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpGetMotivation(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpPivotAnalysis extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.brief) AppCompatTextView brief;
        public @BindView(R.id.query) AppCompatTextView query;
        public @BindView(R.id.title) AppCompatTextView title;

        public GpPivotAnalysis(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpNeedInvestor extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.domains) AppCompatTextView domains;
        public @BindView(R.id.title) AppCompatTextView title;
        public @BindView(R.id.highLevelPitch) AppCompatTextView highLevelPitch;
        public @BindView(R.id.description) AppCompatTextView description;
        public @BindView(R.id.attachment) AppCompatTextView attachment;

        public GpNeedInvestor(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpLegal extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.issues) AppCompatTextView issues;
        public @BindView(R.id.query) AppCompatTextView query;

        public GpLegal(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class GpNetworking extends MainHolder {
        public @BindView(R.id.role) AppCompatTextView role;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpNetworking(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class GpOther extends MainHolder {
        public @BindView(R.id.title) AppCompatTextView title;
        public @BindView(R.id.description) AppCompatTextView description;

        public GpOther(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

/*PROGRESS BAR HOLDERS*/

    public static class ProgressViewHolder extends MainHolder {
        public @BindView(R.id.progressBar) ProgressBar progressBar;

        public ProgressViewHolder(View v) {
            super(v);
            try {
                ButterKnife.bind(this, v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

每个布局都有不同的xml文件

我想尝试这样的截图

public Bitmap takeScreenshot() {

        // Cannot figure out how to get the current view 
        View rootView = ????
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
    }

SharePost按钮

 holder.sharePost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = takeScreenshot();
        }
    });

2 个答案:

答案 0 :(得分:1)

   holder.sharePost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = takeScreenshot();
//getAdapterPosition() will return position of a listview, from that list you can take holder.rootview
            }
        });

答案 1 :(得分:1)

<强> RecyclerViewAdapter.class

private View.OnClickListener globalClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedPosition = (int) v.getTag();
                // Save selectedPositions on list or do something              
                Bitmap bitmap = takeScreenshot();
            }
        }

<强> RecyclerViewAdapter.onBindViewHolder

holder.sharePost.setTag(position);
holder.sharePost.setOnClickListener(globalClickListener);