如何在listView中创建Pull-to-Refresh

时间:2014-03-13 18:33:13

标签: android listview

嘿伙计们,我正在努力实现这些项目

https://github.com/erikwt/PullToRefresh-ListView
https://github.com/johannilsson/android-pulltorefresh
https://github.com/chrisbanes/Android-PullToRefresh

我已经尝试了所有三个,但我似乎没有正确地做到这一点。谁能帮助我?

我的代码是:

private class JsonReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setTitle(R.string.waiting);
            pDialog.setMessage(getString(R.string.get_stocks));
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            }

            catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            }
            catch (IOException e) {
                // e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }
        @Override
        protected void onPostExecute(String result) {
            ListDrwaer();
            pDialog.dismiss();
        }
    }// end async task
    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[] { url });
    }
    // build hash set for list view
    public void ListDrwaer() {
        List<Map<String, String>> stocksList = new ArrayList<Map<String, String>>();
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("name");
                String price = jsonChildNode.optString("price");
                stocksList.add(createStockList(name, price));
            }


        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        String[] from = { "name", "price"};
        int[] to = { R.id.stock_name, R.id.stock_price };
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, stocksList,
                R.layout.list_item,
                from, to);
        listView.setAdapter(simpleAdapter);
    }
    private HashMap<String, String> createStockList(String name, String price) {
        HashMap<String, String> stockNameNo = new HashMap<String, String>();
        stockNameNo.put("name", name);
        stockNameNo.put("price", price);
        return stockNameNo;
    }
}

我的布局文件是

<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout>
<LinearLayout 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"
    android:background="#9FA5A4"
    android:layout_weight="1"
    android:shape="rectangle"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:layout_alignParentStart="true"
        android:dividerHeight="2dp"
        android:divider="@drawable/list_grad"
        android:layout_alignParentEnd="true" >

    </ListView>

</LinearLayout>
/>

任何帮助都会得到很大的帮助。我是新手,所以请帮助我。

2 个答案:

答案 0 :(得分:1)

也许您可能想尝试遵循此example

1)编译此库:compile 'com.android.support:support-v4:21.0.+'

2)修改您的布局,如:

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolBar">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/startingList"
        android:scrollingCache="false"
        android:animationCache="false"
        android:smoothScrollbar="true"
        android:layout_below="@+id/toolBar" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

3)在onCreate方法中添加此代码

SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
ListView mListView = (ListView) findViewById(R.id.activity_main_list_view);

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
          mSwipeRefreshLayout.setRefreshing(false);
  }
});

希望它有所帮助!!!

答案 1 :(得分:0)

也许是How to implement Android Pull-to-Refresh

的副本

我已经使用了Chris Banes&#39;实施多次,他的样本非常清楚。您发布的布局未包含在

<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout>