开发环境:macOS 10.12 + Android Studio 2.2,MinSDK Android 5.1
先看看总体效果
本示例是基于Fragment进行的,直接上代码:
【界面结构】
在 Fragment 中,采用 ScrollView + LinearLayout 实现。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
tools:context=".Fragment.HomeFrg">
<LinearLayout
android:id="@+id/frg_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:divider="@drawable/sep_home"
android:showDividers="middle" />
</ScrollView>
顺便说一句,显示列表中的分割线,采用自身的divider来实现,实现方式见上述代码中的第12、13行。
分割线采用 drawable 的 shape,注意:shape 中一定要添加 solid 和 size,并且 solid 的颜色必须定义,哪怕是透明的也要定义。
项目结构如下:
背景颜色的定义,在 values/colors 中实现,如下所示:
<color name="colorBG">#EEEEEE</color>
整个 divider 的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorBG" />
<size android:height="10dp" />
</shape>
【代码结构】
新建 java 的类库,名为:TestImage.java,主要是结合缓存实现图片的异步加载(线程池方式),代码如下:
import android.graphics.drawable.Drawable;
import android.os.Handler; import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class TestImage {
// 为了加快速度,在内存中开启缓存
// 主要应用于重复图片较多时,或者同一个图片要多次被访问,比如在ListView时来回滚动
public Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>(); // 固定 10 个线程来执行任务
private ExecutorService _exeService = Executors.newFixedThreadPool(10);
private final Handler _handler = new Handler(); public Drawable getImage(final String url, final Callback callback) { // 缓存中存在就用缓存中的图片
if (imageCache.containsKey(url)) {
SoftReference<Drawable> softReference = imageCache.get(url); if (softReference.get() != null) {
return softReference.get();
}
} // 缓存中没有图片,就从网络中获取图片,同时,存入缓存
_exeService.submit(new Runnable() { @Override
public void run() {
final Drawable drawable = getImage(url);
imageCache.put(url, new SoftReference<Drawable>(drawable)); _handler.post(new Runnable() { @Override
public void run() {
callback.imageLoaded(drawable);
}
});
}
}); return null;
} // 从网络中获取图片
protected Drawable getImage(String url) {
Drawable drawable = null; try {
drawable = Drawable.createFromStream(new URL(url).openStream(), "img.png");
} catch (Exception e) {
e.printStackTrace();
} return drawable;
} // 回调方法
public interface Callback {
void imageLoaded(Drawable drawable);
}
}
类库建立好了之后,在 Fragment 的后台代码中进行调用(含代码创建页面布局),代码如下:
public class HomeFrg extends Fragment { private LinearLayout _layout;
//private TestImage _testImage = new TestImage(); public HomeFrg() {
// Required empty public constructor
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frg_home, container, false);
initView(view); // Inflate the layout for this fragment
return view;
} private void initView(View view) {
_layout = (LinearLayout) view.findViewById(R.id.frg_home); for (int i = 0; i < 3; i++) {
initCell(view);
}
} private void initCell(View view) {
Context self = this.getContext(); // 创建单个的单元格的容器(RelativeLayout)
RelativeLayout.LayoutParams layoutWrapper = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout wrapper = new RelativeLayout(self);
wrapper.setBackgroundColor(Helper.getColor(self, R.color.colorWhite));
wrapper.setPadding(0, 30, 0, 30);
_layout.addView(wrapper, layoutWrapper); // 创建封面图片(ImageView)
RelativeLayout.LayoutParams layoutCover = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 600);
ImageView imgCover = new ImageView(self);
int idCover = view.generateViewId();
imgCover.setId(idCover);
// 异步加载网络图片(图片URL为测试图片)
loadImage("http://pic9.nipic.com/20100904/4845745_195609329636_2.jpg", imgCover);
imgCover.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgCover.setPadding(20, 0, 20, 0);
wrapper.addView(imgCover, layoutCover); // 创建标题(TextView)
RelativeLayout.LayoutParams layoutTitle = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutTitle.setMargins(20, 0, 20, 0);
layoutTitle.addRule(RelativeLayout.BELOW, idCover);
TextView txtTitle = new TextView(self);
int idTitle = view.generateViewId();
txtTitle.setId(idTitle);
txtTitle.setText("标题内容标题内容标题内容标题内容标题内容标题内容");
txtTitle.setTextSize(20);
// 标题单行显示,多余的字符用省略号代替(包括以下两行)
txtTitle.setEllipsize(TextUtils.TruncateAt.END);
txtTitle.setSingleLine();
txtTitle.setTextColor(Helper.getColor(self, R.color.colorBlack));
wrapper.addView(txtTitle, layoutTitle); // 创建作者(TextView)
RelativeLayout.LayoutParams layoutAuthor = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutAuthor.setMargins(20, 0, 20, 0);
layoutAuthor.addRule(RelativeLayout.BELOW, idTitle);
TextView txtAuthor = new TextView(self);
int idAuthor = view.generateViewId();
txtAuthor.setId(idAuthor);
txtAuthor.setText("作者名称");
txtAuthor.setTextColor(Helper.getColor(self, R.color.colorBlack));
wrapper.addView(txtAuthor, layoutAuthor); // 创建日期(TextView)
RelativeLayout.LayoutParams layoutTime = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutTime.setMargins(20, 0, 20, 0);
layoutTime.addRule(RelativeLayout.BELOW, idAuthor);
TextView txtTime = new TextView(self);
txtTime.setText("2016年9月22日 16:33");
wrapper.addView(txtTime, layoutTime);
} // 再次封装 TestImage,实现异步加载,方便页面内调用
private void loadImage(String url, final ImageView imageView) {
Drawable imgCache = new TestImage().getImage(url, new TestImage.Callback() { @Override
public void imageLoaded(Drawable drawable) {
imageView.setImageDrawable(drawable);
}
}); if (imgCache != null) {
imageView.setImageDrawable(imgCache);
}
}
}
至此,所有功能实现完毕。
【特别说明】
上述代码在创建布局时,如果碰到最终效果,多个控件(包括 ImageView 和 TextView)重叠时,那是由于 RelativeLayout 的布局的特殊性,需要声明几个关键的东西:
1、声明 LayoutParams layout
2、控件.setId(view.generateViewId())
3、layout.addRule(RelativeLayout.BELOW, 上述 generateViewId() 所产生的 Id)
注意以上三点,即可在 RelativeLayout 中,将各个控件依次分开排列布局。同时,可通过 layout.setMargins 或 控件.setPadding 进行各处留白距离的微调。