【问题标题】:SwipeRefreshLayout got error and cannot refresh (load more) in the first timeSwipeRefreshLayout 出现错误,无法在第一时间刷新(加载更多)
【发布时间】:2016-02-23 03:10:32
【问题描述】:

我正在尝试在我的应用程序中实现“滑动以加载更多”方法,但是当我第一次向下滑动时出现该错误。这是它在控制台中显示的内容:

E/SwipeRefreshLayout:得到 ACTION_MOVE 事件但没有活动 指针标识。 E/SwipeRefreshLayout:得到 ACTION_MOVE 事件但没有 一个活动的指针 id。 E/SwipeRefreshLayout:得到 ACTION_MOVE 事件但 没有活动的指针 ID。 E/SwipeRefreshLayout:得到 ACTION_MOVE 事件,但没有活动的指针 id。

这是我的布局代码:

<?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>

当我第二次滑动时:它会加载双倍数据。

这是ChatActivity,实现了那个方法:

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);
    }

}

更新问题的原因

我找到了该问题的原因。这是因为我在使用此代码初始化时强制列表视图滚动到底部:

android:stackFromBottom="true"

我用这个替换了那个代码,但仍然有同样的问题:

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

【问题讨论】:

  • 您能否添加实现此功能的活动,以及完整的 logcat。
  • 我刚刚添加了实现方法的活动和 logcat:它多次显示该错误。
  • 这个问题有什么解决办法吗? :(

标签: android swiperefreshlayout


【解决方案1】:

我认为问题是,您试图在同一方法(即onRefresh())内显示、关闭和刷新 swipeRefreshLayout。 为显示和关闭对话框创建单独的方法,并从需要它们的地方调用它们,如下所示:

@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);
    }

【讨论】:

  • 我已经实现了 OnRefreshListener 接口,但无法覆盖 showDialog() 和 dismissDialog()。你确定它属于那个接口吗?
  • 我可以将两个对话框方法放入 onRefresh() 方法中吗?
  • 实际上我有一个单独的接口,它从 SwipeRefreshlayout 扩展 OnRefreshListener 接口,我在那里定义了显示对话框和解除对话框方法。您当前的活动实现了该单独的接口。那些被覆盖的方法来自那里。它来自我的工作区。
  • 在 onRefresh() 中放置两个对话框方法不起作用?好吧,那我猜代码有问题。你应该包括整个日志猫错误!其他人可能会觉得这很有帮助
猜你喜欢
  • 2021-02-02
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
相关资源
最近更新 更多