ListView和PullToRefresh向下滑动冲突

时间:2014-02-19 16:03:22

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

我已将@chrisbanes ActionBar-PullToRefresh用于我的项目,现在我面临ListViewPullToRefresh向下滑动手势之间的冲突。 当ListView已经滚动并且我希望通过向下滑动到达顶部时,PullToRefresh会触发刷新操作,而不是向上滑动列表。

PS: 这是我的源代码,没有额外的东西。

public class TestFragment extends Fragment implements OnRefreshListener {
    private PullToRefreshLayout mPullToRefreshLayout;
    private TestAdapter testAdapter;
    private ListView testListView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.testActivity = (TestActivity)getActivity();
        PullToRefreshLayout testLayout = (PullToRefreshLayout)inflater.inflate(R.layout.test_list_view, null);
        this.testData = new ArrayList<Object>();

        // Set List Adapter
        this.testListView = new ListView(this.testActivity);
        this.testData = getSearchResult();
        this.testAdapter = new TestAdapter(this.testActivity, this.testData);
        testListView.setAdapter(this.testAdapter);
        testListView.setFocusableInTouchMode(false);

        testLayout.addView(twitterListView);

        return testLayout;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewGroup viewGroup = (ViewGroup) view;
        // As we're using a Fragment we create a PullToRefreshLayout manually
        mPullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext());

        // We can now setup the PullToRefreshLayout
        ActionBarPullToRefresh.from(getActivity())
                // We need to insert the PullToRefreshLayout into the Fragment's ViewGroup
                .insertLayoutInto(viewGroup)
                // all children are pullable
                .allChildrenArePullable()
                .listener(this)
                .setup(mPullToRefreshLayout);
    }

        @Override
    public void onRefreshStarted(View view) {
        final TestAdapter adapter = this.testAdapter;
        final TestActivity activity = this.testActivity;
        final PullToRefreshLayout pull2Refresh = this.mPullToRefreshLayout;

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                /// refresh data source
                getFragmentData(activity, true);
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // Notify Program Adapter that data has changed
                        adapter.notifyDataSetChanged();
                    }
                });
                // Notify PullToRefreshLayout that the refresh has finished
                pull2Refresh.setRefreshComplete();
            }
        }.execute();

    }
}

1 个答案:

答案 0 :(得分:0)

我发现了这个问题 这是我的错误,首先我必须通过标识符

ListView加载Layout
-- this.testListView = new ListView(this.testActivity);
++ this.testListView = (ListView)testLayout.findViewById(R.id.test_listview);

删除最后一行

-- testLayout.addView(twitterListView);