Android 仿微信朋友圈查看
程序员文章站
2022-05-12 22:53:59
...
项目要做一个类似于这样的功能,就做了。 项目下载地址:http://download.csdn.net/detail/u014608640/9917626
一,看下效果:
二.activity类
public class MainActivity extends Activity {
/** Item数据实体集合 */
private ArrayList<ItemEntity> itemEntities;
/** ListView对象 */
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
initData();
listview.setAdapter(new ListItemAdapter(this, itemEntities));
}
/**
* 初始化数据
*/
private void initData() {
itemEntities = new ArrayList<ItemEntity>();
// 1.无图片
ItemEntity entity1 = new ItemEntity(//
"http://img.my.csdn.net/uploads/201410/19/1413698871_3655.jpg", "张三", "今天天气不错...", null);
itemEntities.add(entity1);
// 2.1张图片
ArrayList<String> urls_1 = new ArrayList<String>();
urls_1.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
ItemEntity entity2 = new ItemEntity(//
"http://img.my.csdn.net/uploads/201410/19/1413698865_3560.jpg", "李四", "今天雾霾呢...", urls_1);
itemEntities.add(entity2);
// 3.3张图片
ArrayList<String> urls_2 = new ArrayList<String>();
urls_2.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
urls_2.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
urls_2.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
ItemEntity entity3 = new ItemEntity(//
"http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg", "王五", "今天好大的太阳...", urls_2);
itemEntities.add(entity3);
// 4.6张图片
ArrayList<String> urls_3 = new ArrayList<String>();
urls_3.add("http://img.my.csdn.net/uploads/201410/19/1413698837_7507.jpg");
urls_3.add("http://img.my.csdn.net/uploads/201410/19/1413698865_3560.jpg");
urls_3.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
urls_3.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
urls_3.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
urls_3.add("http://img.my.csdn.net/uploads/201410/19/1413698839_2302.jpg");
ItemEntity entity4 = new ItemEntity(//
"http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg", "赵六", "今天下雨了...", urls_3);
itemEntities.add(entity4);
}
三.适配器
public class ListItemAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<ItemEntity> items;
public ListItemAdapter(Context ctx, ArrayList<ItemEntity> items) {
this.mContext = ctx;
this.items = items;
}
@Override
public int getCount() {
return items == null ? 0 : items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(mContext, R.layout.item_list, null);
holder.iv_avatar = (ImageView) convertView.findViewById(R.id.iv_avatar);
holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
holder.gridview = (NoScrollGridView) convertView.findViewById(R.id.gridview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ItemEntity itemEntity = items.get(position);
holder.tv_title.setText(itemEntity.getTitle());
holder.tv_content.setText(itemEntity.getContent());
// 使用ImageLoader加载网络图片
DisplayImageOptions options = new DisplayImageOptions.Builder()//
.showImageOnLoading(R.drawable.ic_launcher) // 加载中显示的默认图片
.showImageOnFail(R.drawable.ic_launcher) // 设置加载失败的默认图片
.cacheInMemory(true) // 内存缓存
.cacheOnDisk(true) // sdcard缓存
.bitmapConfig(Config.RGB_565)// 设置最低配置
.build();//
ImageLoader.getInstance().displayImage(itemEntity.getAvatar(), holder.iv_avatar, options);
final ArrayList<String> imageUrls = itemEntity.getImageUrls();
if (imageUrls == null || imageUrls.size() == 0) { // 没有图片资源就隐藏GridView
holder.gridview.setVisibility(View.GONE);
} else {
holder.gridview.setAdapter(new NoScrollGridAdapter(mContext, imageUrls));
}
// 点击回帖九宫格,查看大图
holder.gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
imageBrower(position, imageUrls);
}
});
return convertView;
}
/**
* 打开图片查看器
*
* @param position
* @param urls2
*/
protected void imageBrower(int position, ArrayList<String> urls2) {
Intent intent = new Intent(mContext, ImagePagerActivity.class);
// 图片url,为了演示这里使用常量,一般从数据库中或网络中获取
intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
mContext.startActivity(intent);
}
/**
* listview组件复用,防止“卡顿”
*
* @author Administrator
*
*/
class ViewHolder {
private ImageView iv_avatar;
private TextView tv_title;
private TextView tv_content;
private NoScrollGridView gridview;
}
四:gv适配器
public class NoScrollGridAdapter extends BaseAdapter {
/** 上下文 */
private Context ctx;
/** 图片Url集合 */
private ArrayList<String> imageUrls;
public NoScrollGridAdapter(Context ctx, ArrayList<String> urls) {
this.ctx = ctx;
this.imageUrls = urls;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageUrls == null ? 0 : imageUrls.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return imageUrls.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(ctx, R.layout.item_gridview, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
DisplayImageOptions options = new DisplayImageOptions.Builder()//
.cacheInMemory(true)//
.cacheOnDisk(true)//
.bitmapConfig(Config.RGB_565)//
.build();
ImageLoader.getInstance().displayImage(imageUrls.get(position), imageView, options);
return view;
}
}
五:图片查看器
/**
* 图片查看器
*/
public class ImagePagerActivity extends FragmentActivity {
private static final String STATE_POSITION = "STATE_POSITION";
public static final String EXTRA_IMAGE_INDEX = "image_index";
public static final String EXTRA_IMAGE_URLS = "image_urls";
private HackyViewPager mPager;
private int pagerPosition;
private TextView indicator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_detail_pager);
pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
ArrayList<String> urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
mPager = (HackyViewPager) findViewById(R.id.pager);
ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
mPager.setAdapter(mAdapter);
indicator = (TextView) findViewById(R.id.indicator);
CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
indicator.setText(text);
// 更新下标
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
indicator.setText(text);
}
});
if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}
mPager.setCurrentItem(pagerPosition);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, mPager.getCurrentItem());
}
private class ImagePagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<String> fileList;
public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {
super(fm);
this.fileList = fileList;
}
@Override
public int getCount() {
return fileList == null ? 0 : fileList.size();
}
@Override
public Fragment getItem(int position) {
String url = fileList.get(position);
return ImageDetailFragment.newInstance(url);
}
}