2017-05-03 25 views
0

enter image description here 私はこのpostに記載された何かをしたいが、私はスクロールして動作させることができなかった。すべてが表示されている、引き出しのメニュー、ヘッダービューとリストビューが、リストビューを上下にスクロールすると、ヘッダービューはそこにとどまり、リストビューがスクロールされている間隠れて表示されません。私は解決策のためにこれらのようないくつかの投稿を参照しましたが、どれも役に立たなかった。DrawerLayout CoordinatorLayout AppBarLayoutスクロールで表示と非表示を切り替える

Android design library CoordinatorLayout, AppBarLayout and DrawerLayout

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

Iは、引き出しメニューおよびフラグメントを含む活性を有します。フラグメントには、リストビューのスクロールでヘッダービューを表示および非表示にしたいCoordinatorLayoutおよびAppBarLayoutがあります。

メインのコンテンツのFrameLayoutとドロワーメニューのコンテンツのRelativeLayoutを含むルートビューとしてのDrawerLayoutを持つアクティビティレイアウト。

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

私は、リストビューをスクロールアップされたときにヘッダビューを非表示にすると、リストビューをスクロールダウンされたヘッダビューを表示するフラグメントレイアウト。

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

アプリのテーマは、この記事によると

Theme.AppCompat.Light.DarkActionBar 

Activityクラス

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

断片クラス

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

に変更することをお勧め:layout_behavior =「@文字列は、/ appbar_scrolling_view_behavior」でframeLayoutするのではなく、ListViewのために? –

+0

@ Kayo Lima、それを試しても、まだ動作しません。 –

答えて

1

です、ScrollingViewBehavior for ListViewCoordinatorLayoutのみRecyclerViewNestedScrollViewと作品なので、私はあなたがListViewアプリを変更した場合はどうなりRecyclerView

+0

ニースが見つかりました!それはListViewで動作する場合はいいだろう。 –

関連する問題