【问题标题】:Not working scrolling of RecyclerView in layout extended from base activity在从基本活动扩展的布局中无法滚动 RecyclerView
【发布时间】:2019-05-07 19:40:25
【问题描述】:

我需要一些有关我的 android 应用程序的帮助。我遇到的问题是我创建了控制抽屉布局和工具栏的基本活动。当我在相对布局和嵌套回收视图的活动中扩展它时,我遇到了一个无法滚动它的问题(但是,当活动开始时,我可以抓住这一刻并做到这一点,但只有一次)。另一种情况 - 我打开了一个应用程序,但没有在我的 RecycleView 活动中扩展 Base 活动,它工作正常。

回收站视图:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".EntryList"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/entry_list">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:padding="4dp"
    android:scrollbars="vertical"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

</RelativeLayout>

主要活动(抽屉):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

AppBar 包含在 Drawer 中:

<?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:id="@+id/coordinator_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/gradient_color"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <FrameLayout
            android:id="@+id/activity_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

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

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

基础活动:

public abstract class BaseActivity extends AppCompatActivity {

    protected Context context;
    protected Toolbar toolbar;
    private DrawerLayout drawer;
    private ProgressBar loadingProgressBar;
    private NavigationView navigationView;
    private ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        context = BaseActivity.this;
        setContentView(R.layout.activity_main);
    }

    @Override
    public void setContentView(int layoutResID) {
        DrawerLayout fullView = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main, null);
        FrameLayout activityContainer = fullView.findViewById(R.id.activity_content);
        getLayoutInflater().inflate(layoutResID, activityContainer, true);

        super.setContentView(fullView);
        initToolBar();
    }

    private void initToolBar() {
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setUpNavigation();
        toggle.syncState();
    }

    private void setUpNavigation() {
        drawer = findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(BaseActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) {
            throw new NullPointerException("ActionBar is null");
        }
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

        navigationView = findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                // Handle navigation view item clicks here.

                if (item.isChecked())
                    item.setChecked(false);
                else
                    item.setChecked(true);

                drawer.closeDrawers();

                Intent intent;
                switch (item.getItemId()) {
                    case R.id.nav_home:
                        intent = new Intent(BaseActivity.this, EntryList.class);
                        startActivity(intent);
                        return true;
                }

                return false;
            }
        });
        toggle.syncState();
    }


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

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item))
            return true;

        // 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

适配器:

public class EntryListAdapter extends RecyclerView.Adapter<EntryListAdapter.EntryListViewHolder> {

    private List<ExampleEntryItem> items;

    public static class EntryListViewHolder extends RecyclerView.ViewHolder {

        private TextView specieNameRuTextView;
        private TextView entryDateTextView;
        private TextView specieNameLatTextView;
        private TextView entryPlaceTextView;

        public EntryListViewHolder(@NonNull View itemView) {
            super(itemView);
            specieNameRuTextView = itemView.findViewById(R.id.specie_name_ru);
            entryDateTextView = itemView.findViewById(R.id.entry_date);
            specieNameLatTextView = itemView.findViewById(R.id.specie_name_lat);
            entryPlaceTextView = itemView.findViewById(R.id.entry_place);
        }
    }

    public EntryListAdapter(List<ExampleEntryItem> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public EntryListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.example_entry_item, parent, false);
        return new EntryListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull EntryListViewHolder entryListViewHolder, int position) {
        ExampleEntryItem item = this.items.get(position); // TODO: 06.05.2019 test for null
        entryListViewHolder.entryPlaceTextView.setText(item.getEntryPlace());
        entryListViewHolder.entryDateTextView.setText(item.getEntryDate());
        entryListViewHolder.specieNameRuTextView.setText(item.getSpecieNameRu());
        entryListViewHolder.specieNameLatTextView.setText(item.getSpecieNameLat());
    }

    @Override
    public int getItemCount() {
        return this.items.size();
    }

RecycleView 控件类:

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

    List<ExampleEntryItem> items = new ArrayList<>();
    adding items...

    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    RecyclerView.Adapter adapter = new EntryListAdapter(items);
    adapter.notifyDataSetChanged();
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

}

谢谢大家,谁会帮助我

【问题讨论】:

    标签: android android-recyclerview scroll navigation-drawer


    【解决方案1】:

    你首先需要删除

    adapter.notifyDataSetChanged()
    

    在此之后,如果您在嵌套滚动中使用回收器视图 您需要添加一行 recyclerView.setNestwdScrollingenable(false)

    【讨论】:

      【解决方案2】:

      我解决了这个问题。我在 frament 中替换了我的 recyclerview 逻辑,主要是更改,我所做的是 - 在布局中使用 appbar,我将 &lt;include layout="@layout/content"/&gt; 替换为 AppBarLayout 之外并更改行:

      android:layout_height="match_parent"
      android:background="@color/colorWhite"
      

      到这里:

      android:layout_height="wrap_content"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-03
        • 2021-07-09
        • 1970-01-01
        相关资源
        最近更新 更多