在哪里放android.support.v4.widget.SwipeRefreshLayout以及如何实现它?

时间:2016-09-14 01:59:42

标签: java android eclipse

想知道将android.support.v4.widget.SwipeRefreshLayout放在我的代码上的哪个位置?我很困惑,如果它应该在我的activity_main.xml或我的list_item.xml文件中..

这是我的activity_main.xml:

    <?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" >
        <ListView
            android:id="@+android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

这是我的List_item.xml:

    <?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="wrap_content"
        android:orientation="vertical"
        android:padding="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <TextView
            android:id="@+id/datetime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textColor="#43bd00"
            android:textSize="16sp"
            android:textStyle="bold" 
            android:layout_weight="1" />

        <TextView
            android:id="@+id/qname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="#acacac" 
            android:layout_weight="1" />

        <TextView
            android:id="@+id/qagent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="#acacac" 
            android:layout_weight="1" />

        <TextView
            android:id="@+id/qevent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="#acacac" 
            android:layout_weight="1" />

        <TextView
            android:id="@+id/info1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="#acacac" 
            android:layout_weight="1" />

        <TextView
            android:id="@+id/info2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="#acacac"
            android:layout_weight="1"  />

        <TextView
            android:id="@+id/info3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textColor="#acacac" 
            android:layout_weight="1" />  
    </LinearLayout>

我在互联网上找到了一些示例代码,它们将它放在主xml上,但有些样本也将它放在xml列表中。

这是我的主要活动:

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Fragment;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class MainActivity extends ListActivity {

    private ProgressDialog pDialog;
    private static String IPaddress = "http://192.168.1.110/";

    // URL to get contacts JSON
    private static String url = IPaddress + "Projects/GetUsers.php";


    private static final String TAG_QUEUE = "queue"; 
    private static final String TAG_DATETIME = "datetime";
    private static final String TAG_NAME = "qname"; 
    private static final String TAG_AGENT = "qagent"; 
    private static final String TAG_EVENT = "qevent";
    private static final String TAG_INFO1 = "info1";
    private static final String TAG_INFO2 = "info2";
    private static final String TAG_INFO3 = "info3";


    // contacts JSONArray
    JSONArray queue = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>>queueList;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        queueList = new ArrayList<HashMap<String, String>>();

      //  ListView lv = getListView();





        // Calling async task to get json
        new GetEventCounter().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetEventCounter extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity2.this);   
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }




        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);


                    queue = jsonObj.getJSONArray(TAG_QUEUE);


                    for (int i = 0; i < queue.length(); i++) {
                        JSONObject c = queue.getJSONObject(i);           

                        String datetime = String.format("%s : %s", TAG_DATETIME, c.getString(TAG_DATETIME));
                        String qname = String.format("%s : %s", TAG_NAME, c.getString(TAG_NAME));
                        String qagent = String.format("%s : %s", TAG_AGENT, c.getString(TAG_AGENT));
                        String qevent = String.format("%s : %s", TAG_EVENT, c.getString(TAG_EVENT));
                        String info1 = String.format("%s : %s", TAG_INFO1, c.getString(TAG_INFO1));
                        String info2 = String.format("%s : %s", TAG_INFO2, c.getString(TAG_INFO2));
                        String info3 = String.format("%s : %s", TAG_INFO3, c.getString(TAG_INFO3));





                        HashMap<String, String> event = new HashMap<String, String>();


                        event.put(TAG_DATETIME, datetime);
                        event.put(TAG_NAME, qname);
                        event.put(TAG_AGENT, qagent);
                        event.put(TAG_EVENT, qevent);
                        event.put(TAG_INFO1, info1);
                        event.put(TAG_INFO2, info2);
                        event.put(TAG_INFO3, info3);



                        queueList.add(event);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity2.this, queueList,
                    R.layout.list_item, new String[] { TAG_DATETIME, TAG_NAME, TAG_AGENT, TAG_EVENT, TAG_INFO1, TAG_INFO2, TAG_INFO3}, 
                    new int[] { R.id.datetime, R.id.qname, R.id.qagent, R.id.qevent, R.id.info1, R.id.info2, R.id.info3 });

            setListAdapter(adapter);
        }



    }




    protected Fragment getSampleFragment() {
        // TODO Auto-generated method stub
        return null;
    }

}

这将是我想要具有SwipeRefresh效果的输出。 这些输出来自MySQL数据库:

enter image description here

这个样本引起了我的困惑

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swype"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v4.widget.SwipeRefreshLayout>

并且:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swype"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/serial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="25dp"
        android:layout_margin="5dp"
        android:layout_alignParentLeft="true"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/title"
        android:layout_toRightOf="@id/serial"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="18dp" />

</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

谢谢!

2 个答案:

答案 0 :(得分:1)

  

要将滑动刷新小部件添加到现有应用,请将SwipeRefreshLayout添加为单个ListView或GridView的父级。请记住,SwipeRefreshLayout仅支持单个ListView或GridView子级。 - Add the SwipeRefreshLayout Widget

所以,你的activity_main.xml应该是这样的:

<?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" >
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout
</LinearLayout>

实现:

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{

private SwipeRefreshLayout swipeRefreshLayout

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        swipeRefreshLayout.setOnRefreshListener(this);

        //to change the color of the refresh indictor
        swipeRefreshLayout.setColorScheme(getResources().getColor(R.color.yourcolor), 
                getResources().getColor(R.color.yourcolor), 
                getResources().getColor(R.color.yourcolor), 
                getResources().getColor(R.color.yourcolor));
    }

    @Override
    public void onRefresh() {
    //do something here
    //setRefreshing(false) will hide the indicator
    swipeRefreshLayout.setRefreshing(false);
    }
}

答案 1 :(得分:0)

它应该包含listview,如下所示:

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp">

    <android.widget.ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.widget.ListView>

</android.support.v4.widget.SwipeRefreshLayout>

如果您使用的是Android Studio,则可以查看Swiperefresh示例:

File \ New \ Import Sample \然后搜索“swipe ...”

在你的java代码中,你初始化SwipeRefresh对象的方法与你在onResume()中的TextView,Button或其他任何东西完全一样,你也想要这样的东西:

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

然后在initiateRefresh()中,你可以这样做:

AssignData2ListView();
    mSwipeRefreshLayout.setRefreshing(false);