Android 仿微信朋友圈 全文,收起功能

一、添加依赖

implementation 'com.android.support:recyclerview-v7:28.0.0'

二、资源配置

1.drawable文件夹下的shape文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorAccent"/>
</shape>

2.layout布局文件

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_text_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

item_test_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_hend"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/circle"
            android:gravity="center"
            android:text="1"
            android:textColor="@android:color/white"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="0.87"
            android:text="丁先森"
            android:textColor="@android:color/black"
            android:textSize="14sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="56dp"
        android:orientation="vertical"
        android:paddingBottom="8dp">

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:alpha="0.85"
            android:ellipsize="end"
            android:text=""
            android:textColor="@android:color/black"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/tv_expand_or_collapse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全文"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="14sp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginLeft="56dp"
        android:alpha="0.12"
        android:background="@android:color/black" />
</LinearLayout>

逻辑代码

MainActivity

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRvTextList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRvTextList= (RecyclerView) findViewById(R.id.rv_text_list);
        mRvTextList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRvTextList.setAdapter(new TextListAdapter(this));
    }
}

适配器TextListAdapter

public class TextListAdapter extends RecyclerView.Adapter<TextListAdapter.TextHolder> {
    private Activity mContent;

    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;//文本超过限定行数,被点击全文展开

    private SparseArray<Integer> mTextStateList;

    public TextListAdapter(Activity context) {
        mContent = context;
        mTextStateList = new SparseArray<>();
    }

    @Override
    public TextHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TextHolder(mContent.getLayoutInflater().inflate(R.layout.item_test_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final TextHolder holder, final int position) {
        holder.hend.setText(position + 1 + "");//设置头部的文字
        holder.name.setText(Util.getName(position));//设置名称
        int state = mTextStateList.get(position, STATE_UNKNOW);
//        如果该itme是第一次初始化,则取获取文本的行数
        if (state == STATE_UNKNOW) {
            holder.content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
//                    这个回掉会调用多次,获取玩行数后记得注销监听
                    holder.content.getViewTreeObserver().removeOnPreDrawListener(this);
//                    holder.content.getViewTreeObserver().addOnPreDrawListener(null);
//                    如果内容显示的行数大于限定显示行数
                    if (holder.content.getLineCount() > MAX_LINE_COUNT) {
                        holder.content.setMaxLines(MAX_LINE_COUNT);//设置最大显示行数
                        holder.expandOrCollapse.setVisibility(View.VISIBLE);//让其显示全文的文本框状态为显示
                        holder.expandOrCollapse.setText("全文");//设置其文字为全文
                        mTextStateList.put(position, STATE_COLLAPSED);
                    } else {
                        holder.expandOrCollapse.setVisibility(View.GONE);//显示全文隐藏
                        mTextStateList.put(position, STATE_NOT_OVERFLOW);//让其不能超过限定的行数
                    }
                    return true;
                }
            });

            holder.content.setMaxLines(Integer.MAX_VALUE);//设置文本的最大行数,为整数的最大数值
            holder.content.setText(Util.getContent(position));//用Util中的getContent方法获取内容
        } else {
//            如果之前已经初始化过了,则使用保存的状态,无需在获取一次
            switch (state) {
                case STATE_NOT_OVERFLOW:
                    holder.expandOrCollapse.setVisibility(View.GONE);
                    break;
                case STATE_COLLAPSED:
                    holder.content.setMaxLines(MAX_LINE_COUNT);
                    holder.expandOrCollapse.setVisibility(View.VISIBLE);
                    holder.expandOrCollapse.setText("全文");
                    break;
                case STATE_EXPANDED:
                    holder.content.setMaxLines(Integer.MAX_VALUE);
                    holder.expandOrCollapse.setVisibility(View.VISIBLE);
                    holder.expandOrCollapse.setText("收起");
                    break;
            }
            holder.content.setText(Util.getContent(position));
        }


//        设置显示和收起的点击事件
        holder.expandOrCollapse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int state = mTextStateList.get(position, STATE_UNKNOW);
                if (state == STATE_COLLAPSED) {
                    holder.content.setMaxLines(Integer.MAX_VALUE);
                    holder.expandOrCollapse.setText("收起");
                    mTextStateList.put(position, STATE_EXPANDED);
                } else if (state == STATE_EXPANDED) {
                    holder.content.setMaxLines(MAX_LINE_COUNT);
                    holder.expandOrCollapse.setText("全文");
                    mTextStateList.put(position, STATE_COLLAPSED);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return 15;
    }


    public class TextHolder extends RecyclerView.ViewHolder {
        public TextView hend;
        public TextView name;
        public TextView content;
        public TextView expandOrCollapse;

        public TextHolder(View itemView) {
            super(itemView);
//            绑定xml布局中的控件
            hend = (TextView) itemView.findViewById(R.id.tv_hend);
            name = (TextView) itemView.findViewById(R.id.tv_name);
            content = (TextView) itemView.findViewById(R.id.tv_content);
            expandOrCollapse = (TextView) itemView.findViewById(R.id.tv_expand_or_collapse);
        }
    }
}

Util工具类

public class Util {
    private static String[] nameArray = new String[]{
            "Windows", "Mac", "Linux"
    };
    private static String[] contentArray = new String[]{
            "在动作类影片中,只要发生混乱,那么绝对就有木仓战。现在的技术越来越发达,电影或电视中的特效也做的越来越逼真,演员们被木仓打中的效果也很形象,我们经常能看到被木仓打中的伤口血淋林的暴露在大屏幕中,从演员的表演中我们能看到木仓击是很痛的,那么你们有想过被木仓打中到底会有多痛?什么感觉吗?网站有网友为我们分享被子弹打中的感觉\n" +
                    "1、“老实说,比我想象中的感觉要轻很多。本来我以为很痛,可是被打中后就像是被棒球击中的感觉一样,刚开始的几秒钟没什么知觉,过会才感到痛\n" +
                    "2、“被子弹打到的感觉就像是一直有人拿针扎你一样,刺痛刺痛的。”\n" +
                    "3、“我当初大腿被木仓击中,子弹直接从我的大腿中传过去,连带着我的肌腱也被击中,那种感觉我觉得用疼痛两个字已经不足以形容了\n" +
                    "4、“在我十七岁的时候,脚被木仓击中,当时我以为是被蜜蜂蛰了,因为仿佛听到了蜜蜂的声音,没过几秒钟,脚上就传来灼热感,这才知道原来是被木仓击中了。\n" +
                    "5、“我只是听到的木仓声,却没有意识到自己中木仓了。直到血流出来才意识到。所以,对我来讲,被子弹击中没什么感觉。"
          ,
            "GNOME or KDE desktop\n" +
                   " processor with support for AMD Virtualization™ (AMD-V™)"


    };

    /**
     * 获取文本内容根据下标
     *
     * @param position
     * @return
     */

    public static String getContent(int position) {
        return contentArray[position % contentArray.length];
    }

    /**
     * 获取名称根据下标
     *
     * @param position
     * @return
     */
    public static String getName(int position) {
        return nameArray[position % contentArray.length];
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Android仿微信朋友圈图片查看功能,可以按照以下步骤进行: 1. 首先,需要使用一个RecyclerView来展示朋友圈的列表,每个朋友圈项包含了图片的缩略图、文字内容和评论等信息。 2. 当用户点击某个朋友圈项时,需要跳转到一个新的Activity或者Fragment来显示该朋友圈的详细内容。 3. 在新的界面中,可以使用ViewPager来展示朋友圈中的图片。ViewPager的每一页对应一张图片,并实现左右滑动切换图片的功能。 4. 对于图片的加载,可以使用一个图片加载库如Glide或Picasso来加载图片,避免OOM(Out of Memory)的问题。 5. 为了更好的用户体验,可以在ViewPager上添加一个类似于微信的图片预览效果,即当用户点击某张图片时,可以全屏显示,并支持缩放、双击放大、手势滑动等功能。 6. 为了保证性能和流畅度,可以使用一些优化技巧,如图片的压缩、缓存、异步加载等。 7. 如果需要支持多张图片的查看,可以使用PhotoView或类似的第三方库来实现,它可以显示多张图片,并支持手势操作。 8. 最后,为了提高用户体验,可以加入一些其他功能,如显示图片的点赞数和评论数、支持多种分享方式、图片保存等。 通过以上步骤的实现,就可以实现Android仿微信朋友圈图片查看的功能了。这样用户就可以在朋友圈列表中预览图片,点击后再进行详细查看和操作,提高了用户的交互体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值