80,362
社区成员
发帖
与我相关
我的任务
分享
/* 自定义的Adapter,继承android.widget.BaseAdapter */
public class VideoAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<VideoBean> items;
private String filePath;
public VideoAdapter(Context context, List<VideoBean> it, String path) {
mInflater = LayoutInflater.from(context);
items = it;
filePath = path;
}
/* 因继承BaseAdapter,需重写以下method */
@Override
public int getCount() {
return 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 conView, ViewGroup par) {
ViewHolder holder;
if (conView == null) {
/* 使用自定义的video_view作为Layout */
conView = mInflater.inflate(R.layout.video_view, null);
// /* 初始化holder */
holder = new ViewHolder();
holder.image = (ImageView) conView.findViewById(R.id.videoImage);
holder.info = (TextView) conView.findViewById(R.id.videoInfo);
holder.size = (TextView) conView.findViewById(R.id.videoSize);
holder.videoPlay = (ImageView) conView.findViewById(R.id.playImage);
conView.setTag(holder);
} else {
holder = (ViewHolder) conView.getTag();
}
// /* 设定名称 */
VideoBean videoBean = (VideoBean) items.get(position);
if (!videoBean.isBlankBean()) {
holder.info.setText("简介: " + videoBean.getVideoInfo());
holder.size.setText("片长: " + videoBean.getVideoSize());
holder.videoPlay.setOnClickListener(new ButtonListener());
} else {
holder.videoPlay.setVisibility(View.INVISIBLE);
}
// 划出表格线
LinearLayout linL = (LinearLayout)conView.findViewById(R.id.linL);
System.out.println("===========GridView line start======================");
System.out.println("position = " + position);
System.out.println("===========GridView line end=========================");
int id = position;
if (id == 0) {
setPadding(linL, 1);
}else if(id == 1) {
setPadding(linL, 2);
}else {
if (id % 2 == 0) {
setPadding(linL, 3);
} else {
setPadding(linL, 4);
}
}
if (!videoBean.isBlankBean()) {
/* 设定照片 */
URL url;
try {
url = new URL(filePath + videoBean.getId()
+ "."
+ videoBean.getPicType());
URLConnection conn = url.openConnection();
conn.connect();
Bitmap bm = BitmapFactory.decodeStream(conn.getInputStream());
holder.image.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
}
return conView;
}
private void setPadding(LinearLayout linL, int lineType){
switch (lineType) {
case 1: linL.setPadding(1, 1, 1, 1);break;
case 2: linL.setPadding(0, 1, 1, 1);break;
case 3: linL.setPadding(1, 0, 1, 1);break;
case 4: linL.setPadding(0, 0, 1, 1);break;
case 5: linL.setPadding(1, 0, 1, 1);break;
case 6: linL.setPadding(0, 0, 1, 1);break;
}
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
private class ViewHolder {
ImageView image;
TextView size;
TextView info;
ImageView videoPlay;
}
// 设置GridView的网格项不会触发onChick事件
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return false;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/darkgray"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/darkgray"
>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/white"
>
<TableRow>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingTop="5dip"
android:paddingBottom="5dip"
>
<ImageView android:id="@+id/videoImage"
android:layout_width="150dip"
android:layout_height="140dip"
/>
<ImageView
android:id="@+id/playImage"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_gravity="center_vertical|center_horizontal"
android:src="@drawable/video"
/>
</FrameLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="10dip"
android:paddingBottom="10dip"
>
<TextView android:id="@+id/videoInfo"
android:layout_width="150dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:gravity="left"
android:textSize="12sp"
android:paddingLeft="3dip"
android:textColor="@color/black"
/>
<TextView android:id="@+id/videoSize"
android:layout_width="fill_parent"
android:layout_weight="0.0"
android:layout_height="wrap_content"
android:gravity="bottom"
android:textSize="12sp"
android:paddingLeft="3dip"
android:textColor="@color/black"
/>
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>