|
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));
}
} |
————————————————
版权声明:本文遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/GrassEva/article/details/87935431
程序猿的技术大观园:www.javathinker.net
|
|