禁用滚动列表视图不起作用

时间:2013-03-05 11:55:42

标签: android listview

实际上是使用嵌套的listview。父列表视图使用自定义适配器和正常的列表视图。

我知道我可以使用expandable list view。但是在我的场景中处理它们的子项更复杂。有很多组件。

这是我尝试过的,点击父列表中的按钮会显示相应的子列表项。这工作正常。但唯一的问题是滚动。我想滚动子列表,但是父列表是滚动的。我为父母尝试了setScrollContainer(false)setClickable(false)(刚试过),但没有用。

这是我的代码。

activity_main.xml中

  

<RelativeLayout
    android:id="@+id/parent_rel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin" >

    <Button
        android:id="@+id/b_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="View" />

    <TextView
        android:id="@+id/parent_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/b_view"
        android:layout_alignBottom="@+id/b_view"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="96dp"
        android:text="TextView" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/child_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/parent_rel"
    android:layout_below="@+id/parent_rel"
    android:visibility="gone" >

    <ListView
        android:id="@+id/ch_listView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#000000" >
    </ListView>
</RelativeLayout>

MainActivity.java

  

公共类MainActivity扩展了ListActivity {

ListView lv;
String[] prnt_items = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
boolean isViewing = false;
RelativeLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    lv = getListView();
    lv.setCacheColorHint(Color.parseColor("#00000000"));
    lv.setAdapter(new ParentItems());

}

class ParentItems extends BaseAdapter {

    @Override
    public int getCount() {
        return prnt_items.length;
    }

    @Override
    public Object getItem(int position) {
        return prnt_items[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final View p_v = getLayoutInflater().inflate(
                R.layout.activity_main, parent, false);

        TextView tv = (TextView) p_v.findViewById(R.id.parent_txt);

        tv.setText(prnt_items[position]);
        Button sub_view = (Button) p_v.findViewById(R.id.b_view);

        sub_view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!isViewing) {
                    lv.setScrollContainer(false);
                    //lv.setClickable(false);
                    isViewing = !isViewing;
                    layout = (RelativeLayout) p_v.findViewById(R.id.child_view);
                    layout.setVisibility(View.VISIBLE);
                    ListView c_lv = (ListView) p_v.findViewById(R.id.ch_listView);
                    c_lv.setAdapter(new ArrayAdapter<String>(
                            getApplicationContext(),
                            android.R.layout.simple_list_item_1,
                            new String[] { "Hello", "Hai", "Oops!!!" }));
                    c_lv.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                            Toast.makeText(getApplicationContext(),
                                    "Click @ " + arg2, Toast.LENGTH_SHORT)
                                    .show();
                        }

                    });
                }
                else{
                    if(layout !=null){
                        isViewing = !isViewing;
                        lv.setScrollContainer(true);
                        //lv.setClickable(true);
                        layout.setVisibility(View.GONE);
                    }
                }
            }
        });

        return p_v;
    }
}

}

感谢您的帮助。

感谢。

1 个答案:

答案 0 :(得分:13)

当您在具有滚动功能的另一个视图中放置具有自己滚动的视图时,会出现问题。

这是你应该尝试使用内部列表视图的东西

ListView lv = (ListView)findViewById(R.id.myListView);  // your listview inside scrollview
lv.setOnTouchListener(new ListView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });

当触摸孩子时,它会禁用父母的滚动。

相关问题