在自定义ListView中,已经做了比较复杂的布局,但和芒果TV还有一定的差距。继续模仿芒果TV,实现了更为复杂的ListView。效果如下,点击Item左部弹出播放影片的对话框:
点击Item的右部,弹出收藏影片的对话框:
代码结构如下:
布局文件mylist.xml,不是很复杂,ImageView的宽和高都用的是绝对变量,需要以后改进:
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <imageview android:id="@+id/img" android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="3px"/>
- <linearlayout android:orientation="vertical" android:layout_gravity="center"
- android:layout_width="203px"
- android:layout_height="wrap_content">
- <textview android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- android:textSize="22px" />
- <textview android:id="@+id/info"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- android:textSize="13px" />
-
- <imageview android:id="@+id/view_btn"
- android:layout_width="40px"
- android:layout_height="fill_parent"
- android:background="@drawable/aa"
- android:layout_gravity="right"
- android:layout_marginRight="1px"/>
- public class MyListView extends ListActivity {
- private List<map> mData;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mData = ListVIewDate.getData();
- this.getListView().setCacheColorHint(0);
- this.getListView().setBackgroundColor(android.graphics.Color.WHITE);
- MyAdapter adapter = new MyAdapter(this);
- setListAdapter(adapter);
- }
- public final class ViewHolder {
- public ImageView img;
- public TextView title;
- public TextView info;
- public ImageView viewBtn;
- }
- public class MyAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
- public MyAdapter(Context context) {
- this.mInflater = LayoutInflater.from(context);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return mData.size();
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = mInflater.inflate(R.layout.mylist, null);
- holder.img = (ImageView) convertView.findViewById(R.id.img);
- holder.title = (TextView) convertView.findViewById(R.id.title);
- holder.info = (TextView) convertView.findViewById(R.id.info);
- holder.viewBtn = (ImageView) convertView
- .findViewById(R.id.view_btn);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.img.setBackgroundResource((Integer) mData.get(position).get(
- "img"));
- holder.title.setText((String) mData.get(position).get("title"));
- holder.info.setText((String) mData.get(position).get("info"));
- holder.viewBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showMyDialog(2);
- }
- });
- return convertView;
- }
- }