java - 点击 RecyclerView 中的项目时日志中出现 "ViewPostImeInputStage processPointer 0"?

标签 java android list android-recyclerview onclicklistener

在我正在创建的应用程序中,我有一个项目列表。当用户点击这些项目之一时,应该会打开一个新窗口,显示用户点击的项目内的信息。这是我的 java 代码-

NoteListFragment.java

public class NoteListFragment extends Fragment {

private FloatingActionButton mFab;
private View mRootView;
private List<Note> mNotes;
private RecyclerView mRecyclerView;
private NoteListAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;


public NoteListFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment and hold the reference
    //in mRootView
    mRootView = inflater.inflate(R.layout.fragment_note_list, container, false);

    //Get a programmatic reference to the Floating Action Button
    mFab = (FloatingActionButton)mRootView.findViewById(R.id.fab);

    //attach an onClick listener to the Floating Action Button
    mFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), NoteEditorActivity.class));
        }
    });

    setupList();
    return mRootView;
}


private void setupList() {
    mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.note_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);

    final GestureDetector mGestureDetector =
            new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener(){
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }
            });


    mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
            View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

            if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
                int position = recyclerView.getChildLayoutPosition(child);
                Note selectedNote = mNotes.get(position);
                Intent editorIntent = new Intent(getActivity(), NoteEditorActivity.class);
                editorIntent.putExtra("id", selectedNote.getId());
            }
            System.out.print("Failed to load Editor class.");
            return false;

        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });

    mAdapter = new NoteListAdapter(mNotes, getActivity());
    mRecyclerView.setAdapter(mAdapter);
    mNotes = NoteManager.newInstance(getActivity()).getAllNotes();
}

}

NoteEditorActivity.java

public class NoteEditorActivity extends AppCompatActivity {

private Toolbar mToolbar;

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


    mToolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //remove this line in the MainActivity.java

    if (savedInstanceState == null){
        Bundle args = getIntent().getExtras();
        if (args != null && args.containsKey("id")){
            long id = args.getLong("id", 0);
            if (id > 0){
                openFragment(NotePlainEditorFragment.newInstance(id), "Editor");
            }
        }
        openFragment(NotePlainEditorFragment.newInstance(0), "Editor");
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_note_editor, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void openFragment(final Fragment fragment, String title){
    getSupportFragmentManager()
            .beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .replace(R.id.container, fragment)
            .addToBackStack(null)
            .commit();
    getSupportActionBar().setTitle(title);
}
}

出于某种原因,当我点击其中一项时,没有任何反应,并且日志显示

11-26 09:45:17.963 12963-12963/com.example.simplenotepad D/ViewRootImpl: ViewPostImeInputStage processPointer 0
11-26 09:45:18.073 12963-12963/com.example.simplenotepad D/ViewRootImpl: ViewPostImeInputStage processPointer 1

这是什么意思?我该如何解决?谢谢!

最佳答案

您没有在 RecyclerView 中调用触摸事件中的 Activity 。

修正后的代码是:

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

        if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
            int position = recyclerView.getChildLayoutPosition(child);
            Note selectedNote = mNotes.get(position);
            Intent editorIntent = new Intent(getActivity(), NoteEditorActivity.class);
            editorIntent.putExtra("id", selectedNote.getId());
            startActivity(editorIntent);
        }
        System.out.print("Failed to load Editor class.");
        return false;

    }

关于java - 点击 RecyclerView 中的项目时日志中出现 "ViewPostImeInputStage processPointer 0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40821803/

相关文章:

java - 如何将 "*"解析为 Java 程序作为输入参数?

java - 在参数上调用方法时 PowerMockito 模拟静态方法失败

Android 海绵城堡存档所需的库

node.js - 如何高效地创建不同 UUID 的大列表?

python - 在python中有效地知道两个列表的交集是否为空

java - 同步和信号量

java - 如果记录仅存在 13 个月,则检查数据库

android - SAX 解析器与 XMLPull 解析器

java - 格式错误的 URLException : unknown protocol: android

c# - C# 中的列表大小限制