類似qq發說說的查看全文和收起


package com.example.shangcheng.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.example.shangcheng.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
* Created by Hello on 2017/3/3.
*/
public class MyCircleAdapter extends BaseAdapter {
private final Context context;
private final List<AVObject> list;
private int flag=-1;
private final int MAX_LINE_COUNT = 3;

private final int STATE_UNKNOW = -1;

private final int STATE_NOT_OVERFLOW = 1; //文本行數不超過限定行數

private final int STATE_COLLAPSED = 2; //文本行數超過限定行數,處於折疊狀態

private final int STATE_EXPANDED = 3; //文本行數超過限定行數,被點擊全文展開

public MyCircleAdapter(Context context, List<AVObject> list) {
this.context = context;
this.list = list;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;

if (convertView == null) {
convertView = View.inflate(context, R.layout.circle_item_layout, null);
viewHolder = new ViewHolder();
viewHolder.riv = (RoundedImageView) convertView.findViewById(R.id.circle_item_touxiang);
viewHolder.nickname = (TextView) convertView.findViewById(R.id.circle_item_nickname);
viewHolder.content = (TextView) convertView.findViewById(R.id.circle_item_content);
viewHolder.time = (TextView) convertView.findViewById(R.id.circle_item_time);
viewHolder.comment = (TextView) convertView.findViewById(R.id.circle_item_comment);
viewHolder.show = (TextView) convertView.findViewById(R.id.circle_item_show);
viewHolder.gv = (RecyclerView) convertView.findViewById(R.id.circle_item_gv);
viewHolder.lv = (RecyclerView) convertView.findViewById(R.id.circle_item_lv);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}

Object tag = viewHolder.content.getTag();

AVObject object = list.get(position);
AVFile photo = (AVFile) object.get("photo");
Picasso.with(context).load(photo.getUrl()).into(viewHolder.riv);
viewHolder.nickname.setText(object.getString("userName"));
Object messageBigPics = object.get("messageBigPics");
if (messageBigPics != null) {
List<AVFile> listGv = (List<AVFile>) messageBigPics;
viewHolder.gv.setLayoutManager(new GridLayoutManager(context, 3));
MyItemGvAdapter myItemGvAdapter = new MyItemGvAdapter(context, listGv);
viewHolder.gv.setAdapter(myItemGvAdapter);
}
Object commentMessages = object.get("commentMessages");
if (commentMessages != null) {
List<AVObject> listLv = (List<AVObject>) commentMessages;
viewHolder.lv.setLayoutManager(new LinearLayoutManager(context));
MyItemLvAdapter myItemLvAdapter = new MyItemLvAdapter(listLv);
viewHolder.lv.setAdapter(myItemLvAdapter);
}

if(tag==null)
{
viewHolder.content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
viewHolder.content.getViewTreeObserver().removeOnPreDrawListener(this);
if(viewHolder.content.getLineCount()>MAX_LINE_COUNT)
{
viewHolder.content.setMaxLines(MAX_LINE_COUNT);
viewHolder.show.setVisibility(View.VISIBLE);
viewHolder.show.setText("全文");
viewHolder.content.setTag(STATE_COLLAPSED);
}else{
viewHolder.show.setVisibility(View.GONE);
viewHolder.content.setTag(STATE_NOT_OVERFLOW);
}

return true;
}
});
viewHolder.content.setMaxLines(Integer.MAX_VALUE);
viewHolder.content.setText(object.getString("message"));
}else{
int state= (int) tag;
switch (state){
case STATE_NOT_OVERFLOW:
viewHolder.show.setVisibility(View.GONE);
break;
case STATE_COLLAPSED:
viewHolder.content.setMaxLines(MAX_LINE_COUNT);
viewHolder.show.setVisibility(View.VISIBLE);
viewHolder.show.setText("全文");
break;
case STATE_EXPANDED:
viewHolder.content.setMaxLines(Integer.MAX_VALUE);
viewHolder.show.setVisibility(View.VISIBLE);
viewHolder.show.setText("收起");
break;
}
viewHolder.content.setText(object.getString("message"));
}

viewHolder.show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Object tag = viewHolder.content.getTag();
if(tag!=null)
{
int state= (int) tag;
if(state == STATE_COLLAPSED){
viewHolder.content.setMaxLines(Integer.MAX_VALUE);
viewHolder.show.setText("收起");
viewHolder.content.setTag(STATE_EXPANDED);
}else if(state == STATE_EXPANDED){
viewHolder.content.setMaxLines(MAX_LINE_COUNT);
viewHolder.show.setText("全文");
viewHolder.content.setTag(STATE_COLLAPSED);
}
}
}
});

return convertView;
}

class ViewHolder {
RoundedImageView riv;
TextView nickname;
TextView content;
TextView time;
TextView comment;
TextView show;
RecyclerView gv, lv;
}
}

0 個評論

要回覆文章請先登錄註冊