Android 使用 Pull to Refresh 实现 SwipeListView

Posted

技术标签:

【中文标题】Android 使用 Pull to Refresh 实现 SwipeListView【英文标题】:Android implementing SwipeListView with Pull to Refresh 【发布时间】:2014-05-11 19:49:06 【问题描述】:

我正在尝试在 ListView 上使用 Pull-to-Refresh 实现 SwipeListView。它集成成功,但它在它之后的列表中添加了一项。列表视图不刷新。并且没有项目添加到列表中。

我已经实现了来自here 的SwipeListView 和来自here 的Pull to Refresh。 如何在列表视图中添加多个项目并在添加项目后刷新列表。 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】:

这不是一个直接的解决方案,但我不得不做类似的事情,我最终做的是使用 android 的原生“PullToRefresh”,它被称为 SwipeRefreshLayout,它位于支持库中: https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html。然后我使用了来自https://github.com/47deg/android-swipelistview 的SwipeListView,最终效果很好。

【讨论】:

以上是关于Android 使用 Pull to Refresh 实现 SwipeListView的主要内容,如果未能解决你的问题,请参考以下文章

Android源代码学习笔记:android-Ultra-Pull-To-Refresh-master

pull_to_refresh_header.xml

如何实现Android Pull-to-Refresh

eclipse Android项目 DDMS db文件 导出 Failed to pull selection null 问题

我可以在不使用 ScrollView/ListView 的情况下使用 Pull to Refresh 吗?

学习笔记:下拉刷新的视图和图标的旋转