美文网首页
ListView动态加载

ListView动态加载

作者: 大舜_666 | 来源:发表于2017-12-27 09:51 被阅读0次

    1.Activity代码:


    private ListView mListView; private int mPageIndex = 1; private MyAdapter myAdapter; private ListmList = new ArrayList<>();

    private View mFootView;

    private Button mLoadMore;

    private ProgressBar mPro;

    private int mLastIndex;

    private SwipeRefreshLayout mSwipe;

    private Handler mHandler = new Handler() {

    @Override

    public void handleMessage(Message msg) {

    String jsonStr = (String) msg.obj;

    Gson gson = new Gson();

    //反射???

    MyData myData = gson.fromJson(jsonStr, MyData.class);

    //给集合赋值

    mList.addAll(myData.getParamz().getFeeds());

    myAdapter.notifyDataSetChanged();

    mSwipe.setRefreshing(false);

    }

    };

    protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    initFootView();

    initView();

    getInfo();

    }

    private void initView() {

    mSwipe = (SwipeRefreshLayout) findViewById(R.id.SwipeRefresh);

    mListView = (ListView) findViewById(R.id.lv);

    mListView.addFooterView(mFootView);

    mListView.setOnScrollListener(this);

    myAdapter = new MyAdapter(mList, this);

    mListView.setAdapter(myAdapter);

    }

    //初始化底部视图

    private void initFootView() {

    mFootView = getLayoutInflater().inflate(R.layout.layout, null);

    mLoadMore = (Button) mFootView.findViewById(R.id.Load_More_Btn);

    mPro = (ProgressBar) mFootView.findViewById(R.id.progressBar2);

    mLoadMore.setOnClickListener(this);

    }

    private void getInfo() {

    new Thread(new Runnable() {

    @Override

    public void run() {

    try {

    String s = MyOkHttp.getMyOkHttp().sendGet("http://litchiapi.jstv.com/api/GetFeeds?column=3&PageSize=10&pageIndex=" + mPageIndex + "&val=100511D3BE5301280E0992C73A9DEC41");

    Message msg = new Message();

    msg.obj = s;

    mHandler.sendMessage(msg);

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }).start();

    }

    @Override

    public void onClick(View v) {

    switch (v.getId()) {

    case R.id.Load_More_Btn:

    mPro.setVisibility(View.VISIBLE);

    mLoadMore.setVisibility(View.GONE);

    mPageIndex++;

    getInfo();

    break;

    }

    }

    //滑动状态监听

    @Override

    public void onScrollStateChanged(AbsListView view, int scrollState) {

    if (scrollState == 0 && myAdapter.getCount() == mLastIndex) {

    mPro.setVisibility(View.GONE);

    mLoadMore.setVisibility(View.VISIBLE);

    }

    }

    //滑动时走的方法

    @Override

    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    mLastIndex = totalItemCount - 1;

    }

    }

    2.Adapter(适配器)

    public class MyAdapter extends BaseAdapter {

    private ListmList;

    private Context mContext;

    public MyAdapter(ListmList, Context mContext) {

    this.mList = mList;

    this.mContext = mContext;

    }

    @Override

    public int getCount() {

    return mList.size();

    }

    @Override

    public Object getItem(int position) {

    return null;

    }

    @Override

    public long getItemId(int position) {

    return 0;

    }

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    Holder holder = null;

    if (convertView == null) {

    holder = new Holder();

    convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);

    holder.mText = (TextView) convertView.findViewById(R.id.ListView_Text);

    convertView.setTag(holder);

    } else {

    holder = (Holder) convertView.getTag();

    }

    holder.mText.setText(mList.get(position).getData().getSubject() + "");

    return convertView;

    }

    class Holder {

    private TextView mText;

    }

    }


    2.Adapter(适配器)代码:

    public class MyAdapter extends BaseAdapter {

    private ListmList;

    private Context mContext;

    public MyAdapter(ListmList, Context mContext) {

    this.mList = mList;

    this.mContext = mContext;

    }

    @Override

    public int getCount() {

    return mList.size();

    }

    @Override

    public Object getItem(int position) {

    return null;

    }

    @Override

    public long getItemId(int position) {

    return 0;

    }

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    Holder holder = null;

    if (convertView == null) {

    holder = new Holder();

    convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);

    holder.mText = (TextView) convertView.findViewById(R.id.ListView_Text);

    convertView.setTag(holder);

    } else {

    holder = (Holder) convertView.getTag();

    }

    holder.mText.setText(mList.get(position).getData().getSubject() + "");

    return convertView;

    }

    class Holder {

    private TextView mText;

    }

    }

    相关文章

      网友评论

          本文标题:ListView动态加载

          本文链接:https://www.haomeiwen.com/subject/ezkzwxtx.html