ScrollView中的Android ListView

时间:2016-05-09 15:47:07

标签: android listview scrollview

我试图显示listView。我还想,如果需要,我的应用程序用户将能够向下滚动listView。所以我做到了:



<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#cacaca">
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.giladneiger.clubber.Party_List_Activity"
    tools:showIn="@layout/activity_party__list_"
    android:background="#cacaca">



    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:contextClickable="true"
        android:textColor="#FFFFFF"
        style="@style/ListViewStyle"
        android:background="#4c008b"
        android:layout_marginTop="60dp"
        android:divider="#cacaca"
        android:dividerHeight="15dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Now Events:"
        android:id="@+id/textView11"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="30dp"
        android:textColor="#7e008d"
        android:textStyle="bold" />



</RelativeLayout>
</ScrollView>
&#13;
&#13;
&#13;

我正在执行的问题是,android正在从scrollView中排除listView,因为scrollView已经具有可滚动功能。我希望listView与内容一样长,并且主滚动视图可以滚动。

我已尝试使用本指南:Android list view inside a scroll view

但它不起作用。它仍然是同样的问题。

这是我的代码:

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

    Firebase.setAndroidContext(this);
    firebaseRef = new Firebase(FIREBASE_URL);
    final ListView listView = (ListView) findViewById(R.id.listView1);

    populatePartyList();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            openPartyInfo(parent, view, position, id);

        }
    });

}

public static void setListViewHeightBasedOnChildren(ListView listView, ListAdapter adapter) {
    //ListAdapter listAdapter = listView.getAdapter();
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < adapter.getCount(); i++) {
        view = adapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, RelativeLayout.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(params);
}
private void populatePartyList() {
    firebaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {


            ArrayList<String> subTitles = new ArrayList<String>();
            ArrayList<PartyClass> partyList = new ArrayList<PartyClass>();
            ArrayList<String> titles = new ArrayList<String>();
            for (DataSnapshot user : snapshot.child("users").getChildren()) {
                if (user.child("party/party_title").exists()) {
                    if (dateRelevant(user.child("party/party_date").getValue().toString()) == true) {

                        String party_title = user.child("party/party_title").getValue().toString();

                        String party_date = user.child("party/party_date").getValue().toString();
                        //dateRelevant(party_date);
                        subTitles.add(party_date);
                        String party_content = user.child("party/party_content").getValue().toString();
                        String age_limit = user.child("party/age_limit").getValue().toString();
                        String party_hour = user.child("party/party_hour").getValue().toString();
                        String location_left = user.child("location_left").getValue().toString();
                        String location_right = user.child("location_right").getValue().toString();

                        PartyClass oneParty = new PartyClass(party_title, party_date, party_content, age_limit, party_hour, location_left, location_right);
                        partyList.add(oneParty);
                    }
                }

            }


            for (PartyClass party : partyList) {
                titles.add(party.getParty_title());
            }
            ArrayAdapter adapter = new ArrayAdapter(Party_List_Activity.this, android.R.layout.simple_list_item_1, titles) {

                @Override
                public View getView(int position, View convertView,
                                    ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);

                    TextView textView = (TextView) view.findViewById(android.R.id.text1);
                    textView.setTextColor(Color.WHITE);

                    return view;
                }
            };
            ListView listView = (ListView) findViewById(R.id.listView1);

            listView.setAdapter(adapter);
            setListViewHeightBasedOnChildren(listView, adapter);



        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            String message = "Error";
            Toast.makeText(Party_List_Activity.this, message, Toast.LENGTH_SHORT).show();
        }
    });

}

1 个答案:

答案 0 :(得分:4)

不要在Scrollview中使用Listview。

在NestedScrollview中使用RecyclerView。 NestedScrollView和RecyclerView开发用于协同工作。

检查此答案:https://stackoverflow.com/a/33494987/3669559