我试图在recyclerview中显示meesages。
消息1 附件-1 附件-2消息2
attachement-1
Messge-3(没有附件)
如果行有附件,我会创建一个新的Recyclerview。点击监听器不适用于RecyclerView ,侦听器适用于除RecyclerView之外的所有其他视图元素。< / p>
MsgCursorAdapter
public class MsgCursorAdapter extends CursorRecyclerViewAdapter<RecyclerView.ViewHolder> {
private static final int VIEW_HOLDER_TYPE_1=1;
private static final int VIEW_HOLDER_TYPE_2=2;
private Context mcontext;
private Cursor mCursor;
private static PostMessageClickListener itemClickListener;
public MsgCursorAdapter(Context context,Cursor cursor,PostMessageClickListener listener){
super(context,cursor);
mcontext=context;
mCursor=cursor;
itemClickListener=listener;
}
@Override
public int getItemViewType(int position) {
if (mCursor.moveToPosition(position))
{
PostItem postItem=PostItem.fromCursor(mCursor);
if (postItem.isFromMe())
{
return VIEW_HOLDER_TYPE_1;
}
else{
return VIEW_HOLDER_TYPE_2;
}
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, Cursor cursor) {
PostItem postitem = PostItem.fromCursor(cursor);
switch (getItemViewType(cursor.getPosition())) {
case VIEW_HOLDER_TYPE_1:
ViewHolder_Type1 viewholder1 = (ViewHolder_Type1) holder;
TextView mytimeView = (TextView) viewholder1.mytimeTextView;
mytimeView.setText(postitem.getPostTime());
TextView mymsgView = (TextView) viewholder1.mymessageTextView;
mymsgView.setText(postitem.getPostMsg());
RecyclerView mRecyclerview1=(RecyclerView) viewholder1.attachments_list;
if (attachmentArrayList.size()>0)
{
mRecyclerview1.setHasFixedSize(true);
PostAttachmentAdapter mAdapter= new PostAttachmentAdapter(attachmentArrayList,mcontext,postitem.getPostColumnid());
mRecyclerview1.setAdapter(mAdapter);//);
mRecyclerview1.setLayoutManager(new CustomLinearLayoutManager(mcontext, LinearLayoutManager.VERTICAL, false));
mRecyclerview1.setVisibility(View.VISIBLE);
}else {mRecyclerview1.setVisibility(View.GONE);}
break;
case VIEW_HOLDER_TYPE_2:
ViewHolder_Type2 viewholder2 = (ViewHolder_Type2) holder;
TextView timeView = (TextView) viewholder2.timeTextView;
timeView.setText(postitem.getPostTime());
TextView msgView = (TextView) viewholder2.messageTextView;
msgView.setText(postitem.getPostMsg());
RecyclerView mRecyclerview2=(RecyclerView) viewholder2.attachments_list;
if (attachmentArrayList.size()>0)
{
mRecyclerview2.setHasFixedSize(true);
PostAttachmentAdapter mAdapter= new PostAttachmentAdapter(attachmentArrayList,mcontext,postitem.getPostColumnid());
mRecyclerview2.setAdapter(mAdapter);//);
mRecyclerview2.setLayoutManager(new CustomLinearLayoutManager(mcontext, LinearLayoutManager.VERTICAL, false));
mRecyclerview2.setVisibility(View.VISIBLE);
}else {
mRecyclerview2.setVisibility(View.GONE);
}
break;
default:
break;
}
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType) {
case VIEW_HOLDER_TYPE_1:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.mypost, parent, false);
ViewHolder_Type1 vh1 = new ViewHolder_Type1(v);
return vh1;
case VIEW_HOLDER_TYPE_2:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post, parent, false);
ViewHolder_Type2 vh2 = new ViewHolder_Type2(v);
return vh2;
default:
break;
}
return null;
}
public static class ViewHolder_Type1 extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mymessageTextView, mytimeTextView;
public RecyclerView attachments_list;
public ViewHolder_Type1(View v) {
super(v);
mymessageTextView = (TextView) v.findViewById(R.id.mymessageTextView);
mytimeTextView = (TextView) v.findViewById(R.id.mytimeTextView);
attachments_list=(RecyclerView)v.findViewById(R.id.post_attachment_recyclerview);
mymessageTextView.setOnClickListener(this);
attachments_list.setOnClickListener(this);
}
@Override
public void onClick(View v) {
itemClickListener.itemClicked(v,getLayoutPosition());
}
}
public static class ViewHolder_Type2 extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView messageTextView, timeTextView;
public RecyclerView attachments_list;
public ViewHolder_Type2(View v) {
super(v);
messageTextView = (TextView) v.findViewById(R.id.messageTextView);
timeTextView = (TextView) v.findViewById(R.id.timeTextView);
attachments_list=(RecyclerView)v.findViewById(R.id.post_attachment_recyclerview);
messageTextView.setOnClickListener(this);
attachments_list.setOnClickListener(this);
}
@Override
public void onClick(View v) {
itemClickListener.itemClicked(v,getLayoutPosition());
}
}
public interface PostMessageClickListener
{
public void itemClicked(View v, int position);
}
PostAttachmentAdapter
public class PostAttachmentAdapter扩展了RecyclerView.Adapter {
private Context mContext;
private ArrayList<PostAttachment> mAttachmentsArray;
private String postCloumnid;
public PostAttachmentAdapter(ArrayList<PostAttachment> attachmentsArray,Context context,String postColumnId)
{
this.mContext=context;
this.mAttachmentsArray=attachmentsArray;
this.postCloumnid=postColumnId;
}
@Override
public PostAttachmentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_attachment_image,null);
final ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
File f = new File(mAttachmentsArray.get(position).getFileName());
String filename = "Thumb_"+f.getName();
if (FileUtility.getMimeType(filename).startsWith("image/")) {
String targetPath= Util.getAttachmentPathForUser(Integer.toString(CurrentUser.getInstance().getId()),postCloumnid, filename);
File thumb=new File(targetPath);
if (thumb.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(targetPath);
myBitmap = Bitmap.createScaledBitmap(myBitmap,250,250,true);
myBitmap= ImageUtility.fastblur(myBitmap,0.4f,7);
holder.imgViewIcon.setImageBitmap(myBitmap);
}
else {
holder.imgViewIcon.setImageResource(R.drawable.ic_photo_black_48dp);
}
}
else {
holder.imgViewIcon.setImageResource(R.drawable.ic_insert_drive_file_black_48dp);
}
}
@Override
public int getItemCount() {
return mAttachmentsArray.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.post_attachment);
}
}
如何为嵌套的RecyclerView Items
创建Click侦听器我已经尝试搜索SO以寻找类似的解决方案,但我找不到任何