发布时Android PullToRefreshListView问题

时间:2014-07-15 06:43:53

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

我在我的应用程序中使用PullToRefreshListView库。但我面临一个问题。释放Listview后,从底部滚出UI的ListView部分会保持空白一段明显的持续时间。建议解决这个问题或者可能是一些更稳定的库?

屏幕截图可以更好地解释这个问题;

enter image description here

状态'A'是普通视图,而'B'是刷新视图。虽然空白部分,蓝色矩形的区域,很快消失,但确实出现了明显的时间。

这是我的布局XML。

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <eu.erikw.PullToRefreshListView
        android:id="@+id/pull_to_refresh_listview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"        
        android:background="@android:color/white"
        android:cacheColorHint="@android:color/white" />

</RelativeLayout>

最后这是我的Activity代码。

public class MainActivity extends Activity {

    private PullToRefreshListView listView;
    private PullToRefreshListViewSampleAdapter adapter;
    private int REF_TIMEOUT = 2000;

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

        listView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);

        View searchView = getLayoutInflater().inflate(R.layout.search_header, null, false);
        EditText chatUsersSearch = (EditText) searchView.findViewById(R.id.chat_users_search);
        listView.addHeaderView(searchView);

        listView.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {

                adapter.loadData();

                listView.postDelayed(new Runnable() {


                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                    }
                }, REF_TIMEOUT);
            }
        });

        adapter = new PullToRefreshListViewSampleAdapter() {};
        listView.setAdapter(adapter);

        // Request the adapter to load the data
        adapter.loadData();

        // click listener
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                ViewHolder viewHolder = (ViewHolder) arg1.getTag();
                if (viewHolder.name != null){
                    Toast.makeText(MainActivity.this, viewHolder.name.getText(), Toast.LENGTH_SHORT).show();
                }                   
            }
        });

    }


    /**
     * The adapter used to display the results in the list
     * 
     */
    public abstract class PullToRefreshListViewSampleAdapter extends android.widget.BaseAdapter {

        private ArrayList<String> items = new ArrayList<String>();;

        public class ViewHolder {
            public String id;
            public TextView name;
        }

        /**
         * Loads the data. 
         */
        public void loadData() {

            // Here add your code to load the data for example from a webservice or DB

            // items = new ArrayList<String>();

            items.add("Ajax Amsterdam");
            items.add("Barcelona");
            items.add("Manchester United");

            // MANDATORY: Notify that the data has changed
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;

            String record = (String) getItem(position);

            LayoutInflater inflater = MainActivity.this.getLayoutInflater();

            ViewHolder viewHolder = new ViewHolder();

            if (convertView == null){
                rowView = inflater.inflate(R.layout.list_item,null);

                viewHolder.name = (TextView) rowView.findViewById(R.id.textView1);

                rowView.setTag(viewHolder);
            }

            final ViewHolder holder = (ViewHolder) rowView.getTag();

            holder.name.setText(record); 

            return rowView;
        }
    }

}

谢谢,

阿马尔

2 个答案:

答案 0 :(得分:1)

你有没有试过这种方式,

        listView.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {
                // Your code to refresh the list contents goes here

                // For the sake of this sample, the code will pause here to
                // force a delay when invoking the refresh
                listView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                    }
                }, 20);
            }
        });

修改:

使用PullToRefreshListView库的可能错误的最终解决方案,

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

        <eu.erikw.PullToRefreshListView
            android:id="@+id/pull_to_refresh_listview"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"        
            android:background="@android:color/white"
            android:cacheColorHint="@android:color/white" />
    </LinearLayout> 
</RelativeLayout>

答案 1 :(得分:0)

使用android swiperefreshlayout:

http://antonioleiva.com/swiperefreshlayout/

我之前在这里看过这个问题,这不是这个库的唯一问题。使用谷歌支持库。