码迷,mamicode.com
首页 > 其他好文 > 详细

给GridView每行加上分割线

时间:2014-07-31 23:16:20      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   os   io   数据   

大概思想就是:既然在GridView里没有设置的方法,那就直接加在item里,在通过别的方法改变item的显示效果,从而达到设置分割线的方法~废话不多说~下面详细的介绍:首先是GridView的布局,android:numColumns=”3″ 每行三列

<GridView
       android:id="@+id/content"
       android:layout_width="fill_parent"
       android:layout_height="match_parent"
       android:numColumns="3" >
   </GridView>

接下来是item的布局

<?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" >

    <FrameLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="105dp"
        android:layout_marginBottom="13dp"
        android:layout_marginTop="13dp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="105dp"
            android:scaleType="centerCrop" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="bottom"
            android:background="#50000000" >

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:maxLines="2"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textColor="@color/text_color"
                android:textSize="16sp" />
        </RelativeLayout>
    </FrameLayout>

    <View
        android:id="@+id/line"
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/line_color" />

</LinearLayout>

接下来是最重要的数据显示了,自定义一个GridViewAdapter继承

public class GridViewAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    public GridViewAdapter(Context context, List<Map<String, Object>> list) {
        super();
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        Viewholder viewholder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gridview_item, null);
            viewholder = new Viewholder(convertView);
            convertView.setTag(viewholder);
        } else {
            viewholder = (Viewholder) convertView.getTag();
        }
        viewholder.update(list.get(position),position);
        return convertView;
    }

    public class Viewholder {
        private ImageView imageView;
        private TextView textView;
        private FrameLayout layout;
        private View view;
        public Viewholder(View convertView) {
            imageView=(ImageView) convertView.findViewById(R.id.imageView);
            textView=(TextView) convertView.findViewById(R.id.textView);
            layout=(FrameLayout) convertView.findViewById(R.id.layout);
            view=convertView.findViewById(R.id.line);
        }

        public void update(Map<String, Object> map,int position) {
            textView.setText(map.get("name").toString());
            int i=0;
            i=position%3;
            switch (i) {
            case 0:
                //每列第一个item不包括分割线的layout右内边距20
                layout.setPadding(0, 0, 20, 0);
                break;
            case 1:
                //每列第二个item不包括分割线的layout左、右内边距各10
                layout.setPadding(10, 0, 10, 0);
                break;
            case 2:
                //每列第三个item不包括分割线的layout左内边距20;必须使三个item的宽度一致
                layout.setPadding(20, 0, 0, 0);
                break;
            default:
                break;
            }
            String id=map.get("id").toString();
            setImage(id, imageView);
            setLine(position, view);
        }
    }

    private void setImage(String id,ImageView imageView){
        if(id.equals("1")){
            imageView.setImageResource(R.drawable.img1);
        }else if(id.equals("2")){
            imageView.setImageResource(R.drawable.img2);
        }else if(id.equals("3")){
            imageView.setImageResource(R.drawable.img3);
        }else if(id.equals("4")){
            imageView.setImageResource(R.drawable.img4);
        }else if(id.equals("5")){
            imageView.setImageResource(R.drawable.img5);
        }
    }

    private void setLine(int position,View view){
        int i=0;
        i=list.size()%3;
        if(position+i+1>list.size()){
            //最后一行分割线隐藏
            view.setVisibility(View.GONE);
        }
    }

bubuko.com,布布扣

以上文章摘自:http://www.easymorse.com/index.php/archives/2120

给GridView每行加上分割线,布布扣,bubuko.com

给GridView每行加上分割线

标签:android   style   blog   http   color   os   io   数据   

原文地址:http://www.cnblogs.com/jinghua1425/p/3883528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!