自定义view加列表展示

来源:互联网 发布:机械加工工艺报价软件 编辑:程序博客网 时间:2024/06/06 01:47


布局


//main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >




    <custom.CustomView
        android:id="@+id/customview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>


//show

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
   >


    <xlistview.XListView
        android:id="@+id/xlv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>


//item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/contnt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textSize="20dp" />


    <TextView
        android:id="@+id/times"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text" />
</LinearLayout>



//adapter


package adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import java.text.SimpleDateFormat;
import java.util.List;


import bean.News;
import bwie.gaojian20161107.R;


/**
 * Created by tang on 2016/11/7.
 */
public class MyAdapter extends BaseAdapter {
    List<News.ResultBean.DataBean> listall;
    Context context;


    public MyAdapter(List<News.ResultBean.DataBean> listall, Context context) {
        this.listall = listall;
        this.context = context;
    }


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


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


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


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = View.inflate(context, R.layout.item, null);
            vh.content = (TextView) convertView.findViewById(R.id.contnt);
            vh.time = (TextView) convertView.findViewById(R.id.times);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.content.setText(listall.get(position).content);
        int time = listall.get(position).unixtime;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        String str = simpleDateFormat.format(time);
        vh.time.setText(str);
        return convertView;
    }


    class ViewHolder {
        TextView content;
        TextView time;
    }
}

//bean

package bean;


import java.util.List;


/**
 * Created by tang on 2016/11/7.
 */
public class News {


    /**
     * error_code : 0
     * reason : Success
     * result : {"data":[{"content":"    跟你们说个今天发生的真事!在公交车上 一女孩腿不舒服 没给大妈让座 大妈开始骂那女孩 说话很难听 冷嘲热风的 说人家没出息 嫁不出去怎样的 还说女孩爸妈没教好她 都怪父母教育程度低怎么怎么的 我实在听不下去了 趁人多的时候 我把那个大妈的钱包偷了!","hashId":"751fdace9eb819af23d6a022dc9c86de","unixtime":1478469230,"updatetime":"2016-11-07 05:53:50"},{"content":"女友:\u201c我就是看准你是只潜力股才逢低买入的,  哪知道几年了,一点上升的势头都没有,当初还不如直接选只绩优股。\u201d  男朋友:\u201c知足吧,就你那眼光,肯定去买中石油。\u201d","hashId":"82880bd35aa5b4afb5deed4cd88c47f4","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"乾隆下江南,乌龟挡道。  乾隆问:\u201c王八们有何事上奏?\u201d  乌龟道:\u201c我等有王八蛋进贡,欲求乌纱帽!\u201d  乾隆大笑:\u201c好,哪日灯头朝下时,就让你等全部当官。\u201d  乌龟们扣谢。  乾隆笑道:\u201c这帮蠢王八,油灯烛头什么时候可以朝下?\u201d  转眼300年后,电灯取代了蜡烛,  于是,君无戏言,这些乌龟果真全部当了官。","hashId":"3aaf8abfc6486dc51242210662e672e2","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"班主任教两个班,另一个班的大雄喜欢他班上一美女。  一天我对大雄说:\u201c你别喜欢她了,你和她如果真在一起了那简直就是一朵鲜花插在了牛粪上。\u201d  他沉默了三秒,拍案而起怒道:\u201c你不能说她是牛粪!\u201d  我更郁闷:\u201c大哥我有说你是鲜花么?\u201d","hashId":"3a127c956b9ab1039d3279b2dbfa9596","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"大飞上高中,学校收补课费,就给爸打电话说学校收补课费。  爸问:\u201c多少?\u201d  大飞说:\u201c480。\u201d  然后只听见大飞爸对大飞妈说:\u201c孩子学校收补课费1580 。\u201d","hashId":"de4f82d80a8e59b556abcd435d85c9ac","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"\u201c这件好看吗?\u201d  \u201c不好。\u201d  \u201c这件呢?\u201d  \u201c也不好 。\u201d  \u201c这件总可以吧?这款丝袜我看很久了, 薄,透气,花纹什么也可以。\u201d  \u201c大哥快走吧,我们这是去抢劫啊!\u201d","hashId":"6be0d1b16bddee1a1e49f8d6dd134f9c","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"邻居家的孩子昨天被老师批评了,今天哭着不上学。  我对我女儿说,看,这是不对的,不能因为老师批评就不上学。  我那六岁的女儿不屑的说:\u201c就是,哭什么呀?老师天天批评我,我都没哭。\u201d","hashId":"b11d2f89510b6fcabcad073d35edd7ed","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"月初又有新的流量,  我让一个室友帮我在手机里下载几部日本动作片,  并委婉地提出要不穿衣服、没有剧情的,  室友邪恶地一笑:\u201c看不出你爱好很独特嘛。\u201d  后来他得意地把手机交给我,  并当面打开帮我下载的相扑比赛视频,  我怀着无比复杂的心情表达了对他的感激,室友之间的代沟莫过于此。","hashId":"deb6dd77b034b2b1dc50645191c742aa","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"一队记者扑面而来:\u201c您好,先生您好,请问能问您几个问题...\u201d  \u201c我幸福!\u201d我本能地抢答。  记者脸稍红:\u201c不是这...\u201d   \u201c我最开心是这月又长工资了!\u201d   记者:\u201c也不是,您。。\u201d   \u201c凝心聚力谋发展,立足岗位作贡献,喜迎大会笑开颜!\u201d  记者怔怔地看着我,狠狠地道:\u201c谢谢!\u201d","hashId":"c732103914bf115ef0825c654057a1fb","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"},{"content":"狗对熊说:嫁给我吧,你会幸福的。  熊说:嫁你生狗熊,我要嫁给猫,  生熊猫才尊贵,说明选择很重要。","hashId":"375a326fd5f28bbb0ead71646e4f88e2","unixtime":1478468630,"updatetime":"2016-11-07 05:43:50"}]}
     */


    public int error_code;
    public String reason;
    public ResultBean result;


    public static class ResultBean {
        /**
         * content :     跟你们说个今天发生的真事!在公交车上 一女孩腿不舒服 没给大妈让座 大妈开始骂那女孩 说话很难听 冷嘲热风的 说人家没出息 嫁不出去怎样的 还说女孩爸妈没教好她 都怪父母教育程度低怎么怎么的 我实在听不下去了 趁人多的时候 我把那个大妈的钱包偷了!
         * hashId : 751fdace9eb819af23d6a022dc9c86de
         * unixtime : 1478469230
         * updatetime : 2016-11-07 05:53:50
         */


        public List<DataBean> data;


        public static class DataBean {
            public String content;
            public String hashId;
            public int unixtime;
            public String updatetime;
        }
    }
}

//MYView

package custom;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by tang on 2016/11/7.
 */
public class CustomView extends View {
    private float radius;


    public float getRadius() {
        return radius;
    }




    public CustomView(Context context) {
        this(context, null);
    }


    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width = 0;
        int height = 0;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = 80;
            radius = 40;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = 80;
            radius = 40;
        }
        setMeasuredDimension(width, height);


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float x = getWidth();
        float y = getHeight();
        //创建画笔
        Paint p = new Paint();
        Rect rect = new Rect();


        //画圆
        p.setColor(Color.RED);
        canvas.drawCircle(x / 2, y / 2, radius, p);
        p.setColor(Color.BLACK);
        String str = "下一步";
        p.getTextBounds(str, 0, str.length(), rect);
        canvas.drawText(str, x / 2 - rect.width() / 2, y / 2 + rect.height() / 2, p);
    }
}

activity


//Mainactivity

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;


import baseactivity.BaseActivity;
import butterknife.Bind;
import custom.CustomView;


public class MainActivity extends BaseActivity {
    @Bind(R.id.customview)
    CustomView c;


    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }


    @Override
    public void initData() {


    }


    @Override
    public void initView(Bundle savedInstanceState) {
      /*  c = (CustomView) findViewById(R.id.customview);*/
        c.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    float downx = event.getX();
                    float downy = event.getY();
                    float radius = c.getRadius();
                    double width = Math.abs(downx - radius);
                    double height = Math.abs(downy - radius);
                    double d = Math.sqrt(width * width + height * height);
                    if (d < radius) {
                        startAct(Show.class);
                        return true;
                    }
                }
                return false;
            }
        });
    }


    @Override
    public void loadData() {


    }
}


//show




import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


import com.google.gson.Gson;


import java.util.ArrayList;
import java.util.List;


import adapter.MyAdapter;
import baseactivity.BaseActivity;
import bean.News;
import xlistview.XListView;


public class Show extends BaseActivity implements XListView.IXListViewListener {
    @Override
    public int bindLayout() {
        return R.layout.activity_show;
    }


    @Override
    public void initData() {


    }


    @Override
    public void initView(Bundle savedInstanceState) {
        xlv = (XListView) findViewById(R.id.xlv);
        xlv.setPullRefreshEnable(true);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
    }


    @Override
    public void loadData() {
        GetRes(1);
    }


    List<News.ResultBean.DataBean> listall = new ArrayList<>();
    MyAdapter adapter;
    int page = 1;
    String path = "http://japi.juhe.cn/joke/content/list.from?key= 874ed931559ba07aade103eee279bb37&pagesize=10&sort=asc&time=1418745237&page=" + page;
    private XListView xlv;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int type = msg.arg1;
            String json = (String) msg.obj;
            Gson gson = new Gson();
            News news = gson.fromJson(json, News.class);
            News.ResultBean res = news.result;
            List<News.ResultBean.DataBean> list = res.data;
            switch (type) {
                case 1:
                    listall.addAll(list);
                    adapter = new MyAdapter(listall, Show.this);
                    xlv.setAdapter(adapter);
                    break;
                case 2:
                    listall.clear();
                    listall.addAll(list);
                    adapter.notifyDataSetChanged();
                    stop();
                    break;
                case 3:
                    listall.addAll(list);
                    adapter.notifyDataSetChanged();
                    stop();
                    break;
            }
        }
    };




    /**
     * 获取网络资源
     */
    private void GetRes(final int tag) {
        path = "http://japi.juhe.cn/joke/content/list.from?key= 874ed931559ba07aade103eee279bb37&pagesize=10&sort=asc&time=1418745237&page=" + page;
        getAyn(path, handler, tag);
    }


    public void stop() {
        xlv.stopLoadMore();
        xlv.stopRefresh();
    }


    @Override
    public void onRefresh() {
        page = page + 1;
        path = "http://japi.juhe.cn/joke/content/list.from?key= 874ed931559ba07aade103eee279bb37&pagesize=10&sort=asc&time=1418745237&page=" + page;
        GetRes(2);
    }


    @Override
    public void onLoadMore() {
        page = page + 1;
        path = "http://japi.juhe.cn/joke/content/list.from?key= 874ed931559ba07aade103eee279bb37&pagesize=10&sort=asc&time=1418745237&page=" + page;
        GetRes(3);
    }

}


0 0
原创粉丝点击