SwipeRefreshLayout got error and cannot refresh (load more) in the first time

删除回忆录丶 提交于 2019-12-24 17:19:25

问题


I am trying to implement "Swipe to load more" method in my application but I got that error when I swipe down in the first time. This is what it showed in the console:

E/SwipeRefreshLayout: Got ACTION_MOVE event but don't have an active pointer id. E/SwipeRefreshLayout: Got ACTION_MOVE event but don't have an active pointer id. E/SwipeRefreshLayout: Got ACTION_MOVE event but don't have an active pointer id. E/SwipeRefreshLayout: Got ACTION_MOVE event but don't have an active pointer id.

This is my layout code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="training.com.chatgcmapplication.ChatActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeLayout"
            android:layout_width="match_parent"
            android:layout_height="380dp">

            <ListView
                android:id="@+id/listMessage"
                android:layout_width="match_parent"
                android:layout_height="380dp"
                android:layout_alignParentLeft="false"
                android:layout_alignParentTop="false"
                android:divider="@null"
                android:listSelector="@android:color/transparent"
                android:stackFromBottom="true"
                android:transcriptMode="alwaysScroll" />

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="1">

            <EditText
                android:id="@+id/txt_chat"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.92"
                android:inputType="text" />

            <Button
                android:id="@+id/btn_send"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="30dp"
                android:text="@string/btn_send" />
        </LinearLayout>
    </LinearLayout>


</android.support.design.widget.CoordinatorLayout>

And when I swipe in the second time: it load double data.

This is ChatActivity, what implement that method:

public class ChatActivity extends AppCompatActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private static EditText txt_chat;
    private String registId;
    private String chatTitle;
    private MessageSender mgsSender;
    private int userId;
    private DatabaseHelper databaseHelper;
    private TimeUtil timeUtil;
    private MessageAdapter messageAdapter;
    private int offsetNumber = 5;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        Button btn_send = (Button) findViewById(R.id.btn_send);
        txt_chat = (EditText) findViewById(R.id.txt_chat);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        swipeRefreshLayout.setOnRefreshListener(this);
        ListView lv_message = (ListView) findViewById(R.id.listMessage);
        timeUtil = new TimeUtil();
        databaseHelper = DatabaseHelper.getInstance(getApplicationContext());
        btn_send.setOnClickListener(this);
        Bundle bundle = getIntent().getExtras();
        chatTitle = bundle.getString("titleName");
        if (getIntent().getBundleExtra("INFO") != null) {
            chatTitle = getIntent().getBundleExtra("INFO").getString("name");
            this.setTitle(chatTitle);
        } else {
            this.setTitle(chatTitle);
        }
        registId = bundle.getString("regId");
        userId = databaseHelper.getUser(chatTitle).getUserId();
        List<Message> messages = databaseHelper.getLastTenMessages(AppConfig.USER_ID, databaseHelper.getUser(chatTitle).getUserId(), 0);
        messageAdapter = new MessageAdapter(getApplicationContext(), R.layout.chat_item, (ArrayList<Message>) messages);
        LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));
        if (messages.size() > 0) lv_message.setAdapter(messageAdapter);
    }

    private BroadcastReceiver onNotice = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("message");
            try {
                Message messageObj = new Message();
                messageObj.setMessage(message);
                messageObj.setUserId(userId);
                messageObj.setSender_id(AppConfig.USER_ID);
                messageObj.setExpiresTime(timeUtil.formatDateTime(timeUtil.getCurrentTime()));
                messageAdapter.add(messageObj);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            messageAdapter.notifyDataSetChanged();
        }
    };


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }


    @Override
    protected void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
        super.onDestroy();
    }


    private static MessageSenderContent createMegContent(String regId, String title) {
        String message = txt_chat.getText().toString();
        MessageSenderContent mgsContent = new MessageSenderContent();
        mgsContent.addRegId(regId);
        mgsContent.createData(title, message);
        return mgsContent;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                String message = txt_chat.getText().toString();
                databaseHelper = DatabaseHelper.getInstance(getApplicationContext());
                mgsSender = new MessageSender();
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        MessageSenderContent mgsContent = createMegContent(registId, AppConfig.USER_NAME);
                        mgsSender.sendPost(mgsContent);
                        return null;
                    }
                }.execute();
                databaseHelper.addMessage(message, timeUtil.getCurrentTime(), userId, AppConfig.USER_ID);
                txt_chat.setText("");
                try {
                    Message messageObj = new Message();
                    messageObj.setMessage(message);
                    messageObj.setUserId(AppConfig.USER_ID);
                    messageObj.setSender_id(userId);
                    messageObj.setExpiresTime(timeUtil.formatDateTime(timeUtil.getCurrentTime()));
                    messageAdapter.add(messageObj);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                messageAdapter.notifyDataSetChanged();
                break;
        }
    }


    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(true);
        List<Message> messages = databaseHelper.getLastTenMessages(AppConfig.USER_ID, databaseHelper.getUser(chatTitle).getUserId(), offsetNumber);
        messageAdapter.insertToTheFirst(messages);
        messageAdapter.notifyDataSetChanged();
        offsetNumber += 5;
        Log.i("Offset number", offsetNumber + "");
        swipeRefreshLayout.setRefreshing(false);
    }

}

UPDATE ISSUE's REASON

I found the reason of that issue. It due to I force the listview scroll to the bottom when it init with this code :

android:stackFromBottom="true"

I replace that code with this but still have same issue:

lv_message.post(new Runnable() {
            @Override
            public void run() {
                lv_message.setSelection(lv_message.getCount() -1);
            }
        });

回答1:


I think the problem is , You are trying to show , dismiss and refresh swipeRefreshLayout inside the same method i.e onRefresh(). Make seperate methods for showing and dismissing dialog and invoke them from the place where they are required as I have done below:

@Override
    public void onRefresh() {
        // refresh code here.
    }

    @Override
    public void showDialog() {
        swipeRef.post(new Runnable() {
            @Override
            public void run() {
                if(swipeRef != null)
                    swipeRef.setRefreshing(true);
            }
        });
    }

    @Override
    public void dismissDialog() {
        if(swipeRef!=null && swipeRef.isShown() )
            swipeRef.setRefreshing(false);
    }


来源:https://stackoverflow.com/questions/35568075/swiperefreshlayout-got-error-and-cannot-refresh-load-more-in-the-first-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!