2016-05-09 21 views
0

Ich versuche, eine ListView anzuzeigen. Ich möchte auch, dass meine App-Benutzer in der Lage sein werden, den ListView nach unten zu scrollen, wenn es benötigt wird. Also machte ich dies:Android ListView in einem ScrollView

<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>

Das Problem, das ich bin exeperiencing, ist, dass Android-Listenansicht aus der Scrollview ist ausschließlich als die Scrollview bereits eine Scroll-able Funktion hat. Ich möchte, dass der listView so lange ist, wie der Inhalt ist und dass die Master-Bildlaufansicht scrollbar ist.

Ich habe versucht, diese Anleitung zu verwenden: Android list view inside a scroll view

Aber es funktioniert nicht. Es ist immer noch das gleiche Problem.

Dies ist mein Code:

@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(); 
     } 
    }); 

} 

Antwort

4

nicht Listview innerhalb Scroll verwenden.

Verwenden Sie RecyclerView in NestedScrollview. NestedScrollView und RecyclerView wurden für die Zusammenarbeit entwickelt. Überprüfen Sie diese Antwort: https://stackoverflow.com/a/33494987/3669559