2017-05-03 4 views
0

enter image description here Ich möchte etwas in diesem post beschriebenen tun, aber ich konnte nicht die Verstecken und zeigen auf Scroll zur Arbeit. Alles wird angezeigt, das Schubladenmenü, die Kopfansicht und die Listenansicht. Wenn ich jedoch die Listenansicht nach oben und unten blät- tern lasse, bleibt die Kopfzeile dort und wird nicht angezeigt, wenn die Listenansicht gescrollt wird. Ich habe einige andere Beiträge wie diese für eine Lösung gesucht, aber keiner von ihnen hat geholfen.DrawerLayout CoordinatorLayout AppBarLayout Fragment nicht zeigen und verstecken auf Scroll

Android design library CoordinatorLayout, AppBarLayout and DrawerLayout

DrawerLayout + CollapsingToolbar + Fragments; Toolbar won't collapse or show image

Ich habe eine Tätigkeit, die eine Schublade Menü und ein Fragment enthält. Im Fragment gibt es das CoordinatorLayout und das AppBarLayout, die ich anzeigen und ausblenden möchte, die Header-Ansicht beim Scrollen der Listenansicht.

Das Aktivitätslayout mit dem DrawerLayout als Stammansicht, das ein FrameLayout für den Hauptinhalt und RelativeLayout für den Inhalt des Schubladenmenüs enthält.

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true"> 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:clickable="true"/> 


    <RelativeLayout 
     android:id="@+id/left_drawer" 
     android:layout_height="match_parent" 
     android:layout_width="280dp" 
     android:layout_gravity="start" 
     android:background="#eee"> 

     <RelativeLayout 
      android:id="@+id/sliding_menu_logo_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#eee"> 
      <ImageView 
       android:id="@+id/sliding_menu_logo" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_margin="20dp" 
       android:layout_centerInParent="true" 
       android:scaleType="centerInside" 
       android:src="@drawable/ic_launcher" /> 
     </RelativeLayout> 
     <ListView 
      android:id="@+id/list" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:layout_alignParentBottom="true" 
      android:layout_below="@id/sliding_menu_logo_container" 
      android:clipToPadding="true" 
      android:divider="@null" 
      android:dividerHeight="0dp" 
      android:drawSelectorOnTop="false" 
      android:fastScrollEnabled="false" 
      android:scrollbarStyle="outsideOverlay" /> 
    </RelativeLayout> 

</android.support.v4.widget.DrawerLayout> 

Das Fragment-Layout, wo ich die Kopfansicht ausblenden möchten, wenn der Listenansicht wird Scrollen nach oben und zeigen die Kopfansicht, wenn die Listenansicht nach unten scrollt.

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/my_appbar_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:background="#eee" 
      app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
       <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="5" 
        android:text="First Name"/> 
       <EditText 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="5"/> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
       <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="5" 
        android:text="Last Name"/> 
       <EditText 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="5"/> 
      </LinearLayout> 
     </LinearLayout> 
    </android.support.design.widget.AppBarLayout> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
     <ListView 
      android:id="@+id/rv_numbers" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
    </FrameLayout> 
</android.support.design.widget.CoordinatorLayout> 

Die App Thema ist

Theme.AppCompat.Light.DarkActionBar 

Die Aktivitätsklasse

public class ScrollingActivity4 extends AppCompatActivity { 

    protected DrawerLayout drawerLayout; 
    RelativeLayout leftDrawerView; 
    protected ActionBarDrawerToggle drawerToggle; 

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

     ScrollingActivity4Fragment scrollingActivity4Fragment = new ScrollingActivity4Fragment(); 
     getFragmentManager() 
       .beginTransaction() 
       .replace(R.id.content_frame, scrollingActivity4Fragment, "tag_scrollingActivity4Fragment") 
       .addToBackStack(null) 
       .commit(); 
    } 

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

     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setDisplayUseLogoEnabled(true); 
     actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher); 


     if (drawerToggle == null) { 
      drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.drawer_open, R.string.drawer_close) { 
       public void onDrawerClosed(View view) { 
       } 

       public void onDrawerOpened(View drawerView) { 

       } 

       public void onDrawerSlide (View drawerView, float slideOffset) { 
       } 

       public void onDrawerStateChanged(int newState) { 

       } 

      }; 
      drawerLayout.setDrawerListener(drawerToggle); 
     } 

     drawerToggle.syncState(); 

     leftDrawerView = (RelativeLayout) findViewById(R.id.left_drawer); 
     ListView rvNumbers = (ListView) findViewById(R.id.list); 

     String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; 
     ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, numbers); 
     rvNumbers.setAdapter(itemArrayAdapter); 
    } 

    @Override 
    public boolean onOptionsItemSelected (MenuItem item) { 
     // The action bar home/up action should open or close the drawer. 
     // ActionBarDrawerToggle will take care of this. 
     if (drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     // Handle action buttons 
     switch (item.getItemId()) { 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    public class ItemArrayAdapter extends ArrayAdapter<String> { 
     String[] itemList; 
     private int listItemLayout; 
     public ItemArrayAdapter(Context context, int layoutId, String[] itemList) { 
      super(context, layoutId, itemList); 
      listItemLayout = layoutId; 
      this.itemList = itemList; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      int pos = position; 
      String item = getItem(pos); 

      ViewHolder viewHolder; 
      if (convertView == null) { 
       viewHolder = new ViewHolder(); 
       LayoutInflater inflater = LayoutInflater.from(getContext()); 
       convertView = inflater.inflate(listItemLayout, parent, false); 
       viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number); 
       convertView.setTag(viewHolder); 
      } else { 
       viewHolder = (ViewHolder) convertView.getTag(); 
      } 

      viewHolder.item.setText(item); 
      return convertView; 
     } 
     class ViewHolder { 
      TextView item; 
     } 
    } 
} 

Das Fragment Klasse

public class ScrollingActivity4Fragment extends Fragment { 
    LinearLayout llHeader; 
    ListView rvNumbers; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.activity_scrolling4_fragment, container, false); 

     llHeader = (LinearLayout) view.findViewById(R.id.ll_header); 
     rvNumbers = (ListView) view.findViewById(R.id.rv_numbers); 

     String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; 

     ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.list_item, numbers); 
     rvNumbers.setAdapter(itemArrayAdapter); 

     return view; 
    } 

    public class ItemArrayAdapter extends ArrayAdapter<String> { 
     String[] itemList; 
     private int listItemLayout; 
     public ItemArrayAdapter(Context context, int layoutId, String[] itemList) { 
      super(context, layoutId, itemList); 
      listItemLayout = layoutId; 
      this.itemList = itemList; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      int pos = position; 
      String item = getItem(pos); 

      ViewHolder viewHolder; 
      if (convertView == null) { 
       viewHolder = new ViewHolder(); 
       LayoutInflater inflater = LayoutInflater.from(getContext()); 
       convertView = inflater.inflate(listItemLayout, parent, false); 
       viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number); 
       convertView.setTag(viewHolder); // view lookup cache stored in tag 
      } else { 
       viewHolder = (ViewHolder) convertView.getTag(); 
      } 

      viewHolder.item.setText(item); 
      return convertView; 
     } 
     class ViewHolder { 
      TextView item; 
     } 
    } 


} 
+0

Was passiert, wenn Sie app geschieht zu ändern ändern: layout_behavior = „@ Zeichenfolge/appbar_scrolling_view_behavior“ statt FrameLayout zu Listview? –

+0

@ Kayo Lima, versuchte es, immer noch nicht funktioniert. –

Antwort

1

zu diesem Beitrag nach: ScrollingViewBehavior for ListView, CoordinatorLayout Werke nur mit RecyclerView und NestedScrollView, so empfehle ich Ihnen die ListView-RecyclerView

+0

Schöner Fund! Es wäre schön, wenn es mit ListView funktioniert. –

Verwandte Themen