zoukankan      html  css  js  c++  java
  • 上拉加载 下拉刷新

    package com.bwie.sx;

    import java.io.IOException;

    import java.util.Arrays;
    import java.util.LinkedList;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;

    import com.bwie.bean.List_news;
    import com.bwie.bean.MyAdapter;
    import com.google.gson.Gson;
    import com.handmark.pulltorefresh.library.PullToRefreshBase;
    import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
    import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
    import com.handmark.pulltorefresh.library.PullToRefreshListView;

    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.IBinder.DeathRecipient;
    import android.text.format.DateUtils;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    public class MainActivity extends ListActivity {
        int index = 1;

        private PullToRefreshListView mPullRefreshListView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);// 1
            new GetDataTask()
                    .execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/"
                            + index);
            // Set a listener to be invoked when the list should be refreshed.下拉刷新处理
            mPullRefreshListView
                    .setOnRefreshListener(new OnRefreshListener<ListView>() { // 2
                        @Override
                        public void onRefresh(
                                PullToRefreshBase<ListView> refreshView) {
                            String label = DateUtils.formatDateTime(
                                    getApplicationContext(),
                                    System.currentTimeMillis(),
                                    DateUtils.FORMAT_SHOW_TIME
                                            | DateUtils.FORMAT_SHOW_DATE
                                            | DateUtils.FORMAT_ABBREV_ALL);

                            // Update the LastUpdatedLabel
                            refreshView.getLoadingLayoutProxy()
                                    .setLastUpdatedLabel(label);// 3.刷新界面处理代理,显示新的时间

                            // Do work to refresh the list here.

                            new GetDataTask()
                                    .execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/"
                                            + index); // 4.执行自定义AsyncTask,进行数据的请求刷新
                        }
                    });

            // Add an end-of-list listener //5.设置上拉加载处理
            mPullRefreshListView
                    .setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

                        @Override
                        public void onLastItemVisible() {

                            new GetDataTask()
                                    .execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/"
                                            + index);
                            Toast.makeText(MainActivity.this, "正在加载", 1).show();
                        }
                    });

        }

        private class GetDataTask extends AsyncTask<String, Integer, String> {

            @Override
            protected String doInBackground(String... params) {
                // Simulates a background job.
                String json = null;
                try {

                    Thread.sleep(4000);
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(params[0]);
                    try {
                        HttpResponse execute = httpClient.execute(httpGet);
                        int statusCode = execute.getStatusLine().getStatusCode();
                        if (statusCode == 200) {
                            HttpEntity entity = execute.getEntity();
                            json = EntityUtils.toString(entity);
                            Log.i("aaa", json);
                        }
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                } catch (InterruptedException e) {
                }

                return json;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Gson gson = new Gson();
                final List_news fromJson = gson.fromJson(result, List_news.class);
                Log.i("bbb", fromJson.getData().toString());
                // mListItems.addFirst("Added after refresh...");
                MyAdapter adapter = new MyAdapter(fromJson.getData(),
                        getApplicationContext());
                ListView actualListView = mPullRefreshListView.getRefreshableView();
                actualListView.setAdapter(adapter);

                actualListView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(MainActivity.this,
                                DetailsActivity.class);
                        intent.putExtra("list", fromJson.getData().get(arg2));
                        startActivity(intent);
                    }
                });
                adapter.notifyDataSetChanged();
                index = index + 1;
                // Call onRefreshComplete when the list has been refreshed.
                mPullRefreshListView.onRefreshComplete();
            }
        }

    }

    package com.bwie.sx;

    import com.bwie.bean.News;
    import com.lidroid.xutils.BitmapUtils;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class DetailsActivity extends Activity {
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.show_detail);
            Intent intent = getIntent();
            News news=(News) intent.getSerializableExtra("list");
            ImageView imageView=(ImageView) findViewById(R.id.image_show);
            TextView textView=(TextView) findViewById(R.id.text_show);
            textView.setText(news.getNews_title());
            BitmapUtils bitmapUtils=new BitmapUtils(getApplicationContext());
            bitmapUtils.display(imageView, news.getPic_url());
        }

    }

    package com.bwie.bean;

    import java.util.List;

    public class List_news {
        List<News> data;

        public List<News> getData() {
            return data;
        }

        public void setData(List<News> data) {
            this.data = data;
        }
        
    }

    package com.bwie.bean;

    import java.util.List;

    import com.bwie.sx.R;
    import com.lidroid.xutils.BitmapUtils;

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

    public class MyAdapter extends BaseAdapter {
        List<News> list;
        Context context;

        public MyAdapter(List<News> list, Context context) {
            super();
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.list_view, null);
                viewHolder.textView = (TextView) convertView
                        .findViewById(R.id.text);
                viewHolder.imageView = (ImageView) convertView
                        .findViewById(R.id.image);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textView.setText(list.get(position).getNews_title());
            BitmapUtils bitmapUtils = new BitmapUtils(context);
            bitmapUtils.display(viewHolder.imageView, list.get(position)
                    .getPic_url());
            return convertView;
        }

        class ViewHolder {
            TextView textView;
            ImageView imageView;
        }
    }

    package com.bwie.bean;

    import java.io.Serializable;

    public class News implements Serializable{
        String news_title;
        String pic_url;
        public String getNews_title() {
            return news_title;
        }
        public void setNews_title(String news_title) {
            this.news_title = news_title;
        }
        public String getPic_url() {
            return pic_url;
        }
        public void setPic_url(String pic_url) {
            this.pic_url = pic_url;
        }

    }

    <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"
        tools:context="com.bwie.sx.MainActivity" >

        <com.handmark.pulltorefresh.library.PullToRefreshListView
            android:id="@+id/pull_refresh_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#00000000"
            android:divider="#19000000"
            android:dividerHeight="4dp"
            android:fadingEdge="none"
            android:fastScrollEnabled="false"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false"
            android:smoothScrollbar="true" />

    </RelativeLayout>

    list_view.xml

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

        <ImageView
            android:id="@+id/image"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="38dp"
            android:layout_marginTop="28dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/image"
            android:layout_marginBottom="24dp"
            android:layout_marginLeft="52dp"
            android:layout_toRightOf="@id/image"
            android:text="TextView" />

    </RelativeLayout>

    show_detail.xml

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

        <ImageView
            android:id="@+id/image_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="57dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/text_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/image_show"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="89dp"
            android:text="TextView" />

    </RelativeLayout>

  • 相关阅读:
    微服务常见安全认证方案Session token cookie跨域
    谈谈基于OAuth 2.0的第三方认证 [上篇]
    Kerberos安全体系详解---Kerberos的简单实现
    kerberos认证原理---讲的非常细致,易懂
    重放攻击(Replay Attacks)
    HTTP
    Cookie/Session机制详解
    cookie和session的区别与联系
    基于Token的WEB后台认证机制
    如何用phpmyadmin导入大容量.sql文件,直接使用cmd命令进行导入
  • 原文地址:https://www.cnblogs.com/cuizhe/p/5299403.html
Copyright © 2011-2022 走看看