2016-05-09 106 views
0

我試圖顯示一個listView。我還希望,如果需要,我的應用程序用戶將能夠向下滾動listView。所以我做了這個:ScrollView中的Android 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>

,我exeperiencing的問題是,Android是不包括滾動視圖列表視圖的滾動視圖已經有一個滾動能夠功能。我希望列表視圖能夠與內容一樣長,並且主滾動視圖可以滾動。

我試圖使用本指南: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(); 
     } 
    }); 

} 

回答