Android——文章详情页的处理

private LinearLayout mContentContainer;//文章容器

1.普通文章

private void dealNormalArt(String content){//content是html格式的文章
    String reg = "<img.*?>";//匹配img标签
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(content);

    List<String> imgurls=new ArrayList<>();

    boolean result = matcher.find();

    while(result) {//开始查找
        Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");//匹配img的src
        Matcher m_src = p_src.matcher(matcher.group());
        if (m_src.find()) {
            String str_src = m_src.group();
            String src=str_src.substring(5,str_src.length()-1);//去掉前边的src="和后面的"
            imgurls.add(src);//将处理好的图片地址加入List
        }
        result = matcher.find();//判断是否还有img标签
    }

    String[] text = pattern.split(content);//获得被图片分割的文本数组
    for (int i=0;i<text.length;i++){
        TextView textView = new TextView(this);
        textView.setText(Html.fromHtml(text[i]));//设置html文本显示到TextView
        mContentContainer.addView(textView);
        if (i<imgurls.size()){//如果还有图片
            String imgurl = imgurls.get(i);
            //android9要求https,所以在这里检查将http全部替换为https,根据需要可以省略
            if (imgurl.contains("http:")) imgurl=imgurl.replace("http","https");
            ImageView imageView = new ImageView(this);
            //imageView.setImageResource(R.drawable.temp);
            ImageDownloader.imageLoader(this, imgurl,imageView);
            mContentContainer.addView(imageView);
        }
    }
}

2.幻灯片图片文章

private void dealPicGroup(String content){//content是json数据,涉及到json处理的部分按照实际数据处理
    String[] parts = content.split("\\[\\{");
    TextView textView = new TextView(this);
    textView.setText(Html.fromHtml(parts[0]));//设置标题
    mContentContainer.addView(textView);
    try {
        JSONArray array=new JSONArray("[{"+parts[1]);
        ArrayList<View> viewList = new ArrayList<>();
        ViewPager pager = new ViewPager(this);//使用ViewPager
        pager.setBackgroundColor(getResources().getColor(R.color.black));

        LayoutInflater inflater = getLayoutInflater();
        Log.i(TAG,"list size: "+array.length());
        for (int i =0; i<array.length(); i++){
            JSONObject object = array.getJSONObject(i);
            //这里使用了写好的xml
            View view = inflater.inflate(R.layout.image_group_item, null, false);
            ImageView image= view.findViewById(R.id.gimage);
            TextView text= view.findViewById(R.id.gtext);
            ImageDownloader.imageLoader(mContext,object.getString("img"),image);
            text.setText(object.getString("text"));

            //这里设置了监听控制文字展开和收缩,不需要可以删掉
            //TextView spread = view.findViewById(R.id.spread);
            //spread.setOnClickListener(new SpreadListener(text));
            viewList.add(view);
        }
        pager.setAdapter(new GroupImageAdapter(viewList));//设置adapter
        mContentContainer.addView(pager);
    } catch (JSONException e) {
        Log.e(TAG, "parse group image json error", e);
    }
}

image_group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gimage"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:lines="2"
        android:ellipsize="end"
        android:layout_alignParentBottom="true"
        android:background="@color/text_al_bg"
        android:textColor="@color/white"
        android:id="@+id/gtext"/>
    <TextView
        android:id="@+id/spread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/spread"
        android:layout_above="@id/gtext"
        android:layout_alignParentEnd="true"
        android:background="@color/text_al_bg"
        android:textColor="@color/white"/>
</RelativeLayout>

GroupImageAdapter.java

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class GroupImageAdapter extends PagerAdapter {
    private ArrayList<View> viewList;

    public GroupImageAdapter(ArrayList<View> viewList) {
        this.viewList = viewList;
    }

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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(viewList.get(position));
        return viewList.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(viewList.get(position));
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值