2016-06-09 4 views
3

У меня есть деятельность, которая отображает AppToolbar рядом с RecyclerView, который находится ниже Toolbar. Каждая строка RecyclerView - это CardView. Моя проблема в том, что RecyclerView не прокручивается вообще. Кажется, что он не получает никаких обратных вызовов жестов.Android recycler view не прокручивает

Вот мой основной макет XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true" 
    android:background="@color/colorActivityBackground" 
    tools:context=".MainActivity"> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/rv" 
      android:scrollbarAlwaysDrawVerticalTrack="true" 
      android:scrollbars="vertical" 
      android:fadeScrollbars="true" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 


    <include layout="@layout/tool_bar" /> 

    <include layout="@layout/nav_drawer_layout" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_add" 
     app:layout_anchor="@id/rv" 
     app:layout_anchorGravity="bottom|end" /> 

</android.support.design.widget.CoordinatorLayout> 

Edit: Вот панель инструментов Код:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.AppBarLayout android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="false" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:elevation="4dp" 
     android:theme="@style/AppTheme.AppBarOverlay" 
     app:layout_collapseMode="pin" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 
</android.support.design.widget.AppBarLayout> 

Вот и мой нав ящик расположение:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="?attr/actionBarSize"> 

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <!-- The main content view --> 
     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
     <!-- The navigation drawer --> 
     <android.support.v7.widget.RecyclerView 
      android:id="@+id/right_drawer" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" 
      android:background="#99ccff" /> 

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

И вот моя основная деятельность, где я заполняю вид моего переработчика:

package bikurim.silverfix.com.bikurim; 

import android.app.SearchManager; 
import android.content.Context; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.DefaultItemAnimator; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.support.v7.widget.SearchView; 
import android.util.Log; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 

import java.util.ArrayList; 

import bikurim.silverfix.com.bikurim.adapters.MenuDrawerAdapter; 
import bikurim.silverfix.com.bikurim.adapters.RecyclerViewAdapter; 
import bikurim.silverfix.com.bikurim.items.FamilyItem; 

public class MainActivity extends AppCompatActivity { 


    private ArrayList<FamilyItem> families = new ArrayList<FamilyItem>(); 
    private ArrayList<bikurim.silverfix.com.bikurim.items.MenuItem> items = new ArrayList<bikurim.silverfix.com.bikurim.items.MenuItem>(); 
    private DrawerLayout drawerLayout; 
    private ActionBarDrawerToggle drawerToggle; 
    private RecyclerView recyclerView, menuView; 
    private RecyclerViewAdapter adapter; 
    private MenuDrawerAdapter navAdapter; 
    private SearchView searchView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_families); 
     // Setting up the tool bar 
     final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     // Setting up the main lists and their adapters 
     fillList(); 
     createMenuItems(); 
     recyclerView = (RecyclerView) findViewById(R.id.rv); 
     menuView = (RecyclerView) findViewById(R.id.right_drawer); 
     adapter = new RecyclerViewAdapter(families); 
     navAdapter = new MenuDrawerAdapter(items); 
     // Default Animator 
     DefaultItemAnimator animator = new DefaultItemAnimator(); 
     recyclerView.setItemAnimator(animator); 
     menuView.setItemAnimator(animator); 
     recyclerView.setAdapter(adapter); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     menuView.setLayoutManager(new LinearLayoutManager(this)); 
     menuView.setAdapter(navAdapter); 
     Log.d("Menu Items Count:", ""+navAdapter.getItemCount()); 
     // Setting up the navigation drawer side-menu with DrawerLayout 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) { 
      /** Called when a drawer is in closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getSupportActionBar().setTitle(R.string.app_name); 
      } 

      /** Called when a drawer is in open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getSupportActionBar().setTitle(R.string.drawer_name); 
      } 
     }; 
     drawerLayout.addDrawerListener(drawerToggle); 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Does absolutly nothing, yet!", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
     getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 
    } 

    private void fillList() { 
     families.add(new FamilyItem("Ohayon", "12:36")); 
     families.add(new FamilyItem("Zohar", "14:23")); 
     families.add(new FamilyItem("Lasry", "15:55")); 
     families.add(new FamilyItem("Zambuzak", "17:45")); 
     families.add(new FamilyItem("Cohen", "11:33")); 
     families.add(new FamilyItem("Tapera", "09:25")); 
     families.add(new FamilyItem("Ochuya", "09:25")); 
     families.add(new FamilyItem("Konichiwua", "09:25")); 
     families.add(new FamilyItem("Kontaktiwa", "09:25")); 
    } 

    private void createMenuItems() { 
     items.add(new bikurim.silverfix.com.bikurim.items.MenuItem(R.drawable.about, "עזרה")); 
     items.add(new bikurim.silverfix.com.bikurim.items.MenuItem(R.drawable.settings, "הגדרות")); 
    } 

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

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     drawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_families, menu); 
     SearchManager searchManager = 
       (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 
     searchView.setSearchableInfo(
       searchManager.getSearchableInfo(getComponentName())); 
     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
      @Override 
      public boolean onQueryTextSubmit(String query) { 
       adapter.getFilter().filter(query); 
       return true; // handled 
      } 

      @Override 
      public boolean onQueryTextChange(String newText) { 
       adapter.getFilter().filter(newText); 
       return true; 
      } 
     }); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_search) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 
+0

Из того, что вы опубликовали, все выглядит так, как будто оно должно работать. Post tool_bar, nav_drawer_layout и код, который вы используете для заполнения recyclerview. –

+0

Я добавил эти коды к сообщению –

+0

Wow у вас там много чего происходит. Я не очень хорошо знаком с DrawerLayout, но я не думаю, что вы используете его по назначению, и я смущен тем, почему у вас есть 2 recyclerviews. Ознакомьтесь с этим ответом о том, как должны взаимодействовать схемы Координатора, AppBar и Drawer: http://stackoverflow.com/a/33413334/1224186 –

ответ

-2
I think your layout is wrong. Make the following changes in nav drawer layout. 

<LinearLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 
<include layout="@layout/tool_bar" /> 

<android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/rv" 
      android:scrollbarAlwaysDrawVerticalTrack="true" 
      android:scrollbars="vertical" 
      android:fadeScrollbars="true" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
</LinearLayout> 

And`kindly remove the recycler view from main layout. 
+0

Я сделал то, что вы сказали, но он все еще не работает. Должен ли я использовать NestedScrollView с моим RecyclerView? –

+0

В представлении Recyler уже предусмотрено поведение прокрутки. Нам следует избегать использования прокрутки в прокрутке. –

Смежные вопросы