阻止一个列表视图中的项目打开另一个列表视图中的活动?

时间:2018-09-02 05:49:44

标签: java android

我有两个列表视图,它们是从同一个Java文件动态填充的。我已经将它们设置为同时滚动,以便list1的项目1始终位于list2的item1等附近。不幸的是,我已经以某种方式链接了两个列表的onClick行为,就像单击列表2中的项目时一样它将在列表1中相应项目的活动顶部打开正确的活动。用户直到按下后才能看到它,但它使我的堆栈混乱。

这是两个列表的当前Java代码:

//make listviews scroll simultaneously
    lv.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(touchSource == null)
                touchSource = v;

            if(v == touchSource) {
                gradeView.dispatchTouchEvent(event);
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    clickSource = v;
                    touchSource = null;
                }
            }
            return false;
        }
    });

    gradeView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(touchSource == null)
                touchSource = v;

            if(v == touchSource) {
                lv.dispatchTouchEvent(event);
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    clickSource = v;
                    touchSource = null;
                }
            }
            return false;
        }
    });

    //make listview items respond to clicks and open relevant activity
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // selected item
            String standard = ((TextView) view).getText().toString();

            // Launching new Activity on selecting single List Item
            Intent i = new Intent(getApplicationContext(), StandardInfoActivity.class);
            // sending data to new activity
            i.putExtra("standard", standard);
            i.putExtra("subject", subjectId);
            i.putExtra("subjectName", subject);
            i.putExtra("level", level);
            startActivity(i);
            finish();
        }
    });

    gradeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // Launching new Activity on selecting single List Item
            Intent i = new Intent(getApplicationContext(), AddGradeActivity.class);
            // sending data to new activity
            i.putExtra("subjectId", subjectId);
            i.putExtra("position", position);
            i.putExtra("level", level);
            i.putExtra("subject", subject);
            startActivity(i);
            finish();
        }
    });

    //fix inconsistencies in listview scrolling motion
    lv.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if(view == clickSource)
                gradeView.setSelectionFromTop(firstVisibleItem, view.getChildAt(0).getTop() + offset);
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {}
    });

您能看到我做错了什么/我需要添加些什么来防止当用户单击gradeview时打开lv活动吗?

编辑:和相关的XML代码:

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <ListView
                android:id="@+id/standardList"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3">
            </ListView>
            <ListView
                android:id="@+id/gradeList"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
            </ListView>
        </LinearLayout>

0 个答案:

没有答案