Android通过Pull to Refresh实现SwipeListView

时间:2014-04-01 07:09:10

标签: android android-listview pull-to-refresh swipe-gesture

我正尝试在SwipeListView上使用 Pull-to-Refresh 实施ListView。它成功集成,但它后面的列表中添加了一个项目。列表视图不爽快。没有项目添加到列表中。

我已经从here实施了SwipeListView,并从here实施了拉动刷新。 如何在列表视图中添加多个项目并在添加项目后刷新列表。 JAVA代码

package com.eample.swipelistviewexample;

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

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.PullToRefreshBase;
import com.fortysevendeg.swipelistview.PullToRefreshBase.OnRefreshListener;
import com.fortysevendeg.swipelistview.PullToRefreshBase.OnRefreshListener2;
import com.fortysevendeg.swipelistview.PullToRefreshSwipeListView;
import com.fortysevendeg.swipelistview.SwipeListView;

public class Contact extends Activity {

    public PullToRefreshSwipeListView ptorefreshList;
    public  SwipeListView swipeListView;
    ProgressDialog dialog;
    private ArrayList<String> mItems;
    CategoryAdapter adapter;
    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mItems = new ArrayList<String>();
        for(int i=0;i<10;i++)
        mItems.add("Musethe place "+i);
        ptorefreshList=(PullToRefreshSwipeListView) findViewById(R.id.example_list);
        swipeListView = ptorefreshList.getRefreshableView();

        adapter = new CategoryAdapter(Contact.this,
                mItems);

    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setListview();
        ptorefreshList.setOnRefreshListener(new OnRefreshListener<SwipeListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<SwipeListView> refreshView) {
                // TODO Auto-generated method stub
                new NewDataTask().execute();
            }
        });
    }
    private void setListview() {

        swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
            //position di -1 karena sejak gabung library swipelistview + pulltorefresh, position slalu kelebihan 1 & menyebabkan OutOfBound error.

            @Override
            public void onClickFrontView(final int position) {
                //do something here
            }

            @Override
            public void onOpened(int position, boolean toRight) {
                // TODO Auto-generated method stub
                super.onOpened(position-1, toRight);
            }

            @Override
            public void onMove(int position, float x) {
                // TODO Auto-generated method stub
                super.onMove(position-1, x);
            }

            @Override
            public int onChangeSwipeMode(int position) {
                // TODO Auto-generated method stub
                            return SwipeListView.SWIPE_MODE_DEFAULT;
            }

            @Override
            public void onStartOpen(int position, int action, boolean right) {
                // TODO Auto-generated method stub
                super.onStartOpen(position-1, action, right);

            }

        });
        ptorefreshList.setAdapter(adapter);
        ptorefreshList.setLongClickable(true);
        swipeListView.setSwipeOpenOnLongPress(false);
    }
  public class CategoryAdapter extends BaseAdapter {

        LayoutInflater inflator;
        ArrayList<String> category;
        public CategoryAdapter(Context context,ArrayList<String> category)
            {
            inflator=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.category=category;
            }

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

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return 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
            final int p=position;
            View vi=convertView;
            if(convertView==null)
                vi=inflator.inflate(R.layout.custom_row,null);
            TextView tv=(TextView) vi.findViewById(R.id.example_itemname);
            tv.setText(category.get(position));
            return vi;
        }



    }
  private class NewDataTask extends AsyncTask<Void, Void, String> {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            if (android.os.Build.VERSION.SDK_INT >= 11) {
                    dialog = new ProgressDialog(Contact.this,AlertDialog.THEME_HOLO_LIGHT);
                } else {
                    dialog = new ProgressDialog(Contact.this);
                }

                dialog.setMessage("Loading...");
                dialog.setIndeterminate(true);
                dialog.setCancelable(false);
                dialog.show();
        }

            @Override
            protected String doInBackground(Void... params) {
                // TODO Auto-generated method stub
                 try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

             return "WWW.MUSETHEPLACE.COM";
            }
            @Override
            protected void onPostExecute(String result) {
                    // This should be called after refreshing finished
                    mItems.add(0, result);
                    adapter = new CategoryAdapter(Contact.this,
                            mItems);
                    ptorefreshList.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    dialog.dismiss();
                    super.onPostExecute(result);

            }
        }

    public int convertDpToPixel(float dp) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return (int) px;
    }

}

1 个答案:

答案 0 :(得分:0)

这不是一个直接的解决方案,但我必须做类似的事情,我最终做的是使用Android的原生&#39; PullToRefresh&#39;名为 SwipeRefreshLayout 的名称位于支持库中:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html。然后我使用了来自https://github.com/47deg/android-swipelistview SwipeListView ,结果很好地一起玩。