如果我在同一活动/布局中滚动另一个ListView,是否可以隐藏一个ListView?

时间:2014-09-16 12:54:59

标签: android listview

我有一个包含两个列表视图的布局,XML看起来有点像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <ListView
        android:id="@+id/list_view_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/list_view_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/list_view_1" />

</RelativeLayout>

&#34; list_view_2&#34;将包含比&#34; list_view_1&#34;更大量的数据。滚动&#34; list_view_2&#34;是否可能它隐藏&#34; list_view_1&#34;。所以它从:https://i.imgur.com/Illeild.pnghttps://i.imgur.com/VFO33qg.png

这一切都可能吗?

提前致谢!

4 个答案:

答案 0 :(得分:0)

这是完整的代码示例:

Java代码:

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {

    ListView LV1, LV2;
    List<String> li1, li2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LV1 = (ListView)findViewById(R.id.list_view_1);
        LV2 = (ListView)findViewById(R.id.list_view_2);

        li1=new ArrayList<String>();
        li1.add("List1 1");
        li1.add("List1 2");
        li1.add("List1 3");
        li1.add("List1 4");
        li1.add("List1 5");

        li2=new ArrayList<String>();
        li2.add("List2 1");
        li2.add("List2 2");
        li2.add("List2 3");
        li2.add("List2 4");
        li2.add("List2 5");

        ArrayAdapter<String> adp1=new ArrayAdapter<String> (getBaseContext(),
                android.R.layout.simple_dropdown_item_1line,li1);
        adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        ArrayAdapter<String> adp2=new ArrayAdapter<String> (getBaseContext(),
                android.R.layout.simple_dropdown_item_1line,li2);
        adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);


        LV1.setAdapter(adp1);
        LV2.setAdapter(adp2);
        adp1.notifyDataSetChanged();
        adp2.notifyDataSetChanged();
        LV2.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                LV1.setVisibility(View.GONE);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                //LV2.setVisibility(View.GONE);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <ListView
        android:layout_alignParentTop="true"
        android:id="@+id/list_view_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/list_view_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/list_view_1" />

</RelativeLayout>

答案 1 :(得分:0)

使用此功能检测滚动事件https://stackoverflow.com/a/17017477/2155188

然后你可以使用

list_view_1.setVisibility(View.GONE);

答案 2 :(得分:0)

listview2.setOnScrollListener(new OnScrollListener() {

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

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

           listview1.setVisibility(View.GONE);

        }
    });

答案 3 :(得分:0)

绝对可能。

做类似的事

将您的父布局设为LinearLayout

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightsum="10">

<ListView 
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=3/>

<ListView 
android:id="@+id/listview2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=7/>

 </LinearLayout>

现在在ListView 2中

listview2.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        YourListView1.setVisibility(View.GONE);

    }
});