【问题标题】:actionbar is not showing hide feature when i use grid layout with appbar layout当我将网格布局与 appbar 布局一起使用时,操作栏未显示隐藏功能
【发布时间】:2016-08-28 18:42:08
【问题描述】:

我正在使用 AppBarLayout。它有一个 ViewPager,我在其中添加了四个片段,其中两个片段中有 recyclerView。一个有GridView。当我在带有 RecyclerView 的片段上时,当我向上滚动列表时,ActionBar 会隐藏,但在 gridview 片段的情况下,这不会发生。 ActionBar 仍然在它的位置。 请帮我!提前致谢。

MainActivity
package com.bajpays.financemanager;

import android.support.design.internal.NavigationMenu;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity implements         View.OnClickListener{
TabLayout tabLayout;
DrawerLayout drawerLayout;
NavigationView navigationView;
ViewPager viewPager;
Toolbar toolbar;
CircleImageView img_user1,img_user2,img_user3,img_user4,img_user5,img_activeuser;
NavigationView navview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar=(Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final ActionBar actionBar=getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.grouppic);
    actionBar.setDisplayShowHomeEnabled(true);
    viewPager=(ViewPager)findViewById(R.id.viewpager);
    viewPager.setOffscreenPageLimit(3);
    if (viewPager!=null){
    setUpViewPager(viewPager);
    }
    tabLayout=(TabLayout)findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    navigationView=(NavigationView) findViewById(R.id.navigatioview);
    if (navigationView != null) {
        setupDrawerContent(navigationView);
    drawerLayout=(DrawerLayout)findViewById(R.id.drawer);
    }
    navview=(NavigationView)findViewById(R.id.navigatioview);
    View headerview=navview.getHeaderView(0);
    RelativeLayout header=(RelativeLayout) headerview.findViewById(R.id.header);
    img_activeuser=(CircleImageView)headerview.findViewById(R.id.img_icon_activeuser);
    img_user1=(CircleImageView)headerview.findViewById(R.id.img_icon_user1);
    img_user2=(CircleImageView)headerview.findViewById(R.id.img_icon_user2);
    img_user3=(CircleImageView)headerview.findViewById(R.id.img_icon_user3);
    img_user4=(CircleImageView)headerview.findViewById(R.id.img_icon_user4);
    img_user5=(CircleImageView)headerview.findViewById(R.id.img_icon_user5);
    img_user1.setTag(R.drawable.deep);
    img_user2.setTag(R.drawable.grouppic);
    img_user3.setTag(R.drawable.indelhi);
    img_user4.setTag(R.drawable.kanishk);
    img_user5.setTag(R.drawable.tshirt);
    img_user1.setOnClickListener(this);
    img_user2.setOnClickListener(this);
    img_user3.setOnClickListener(this);
    img_user4.setOnClickListener(this);
    img_user5.setOnClickListener(this);

}
 static class MyAdapter extends FragmentPagerAdapter {
    ArrayList<Fragment> fragments=new ArrayList<>();
    ArrayList<String>  titles=new ArrayList<>();
    public MyAdapter(FragmentManager fm) {
        super(fm);

    }
    public void addFragment(Fragment fragment,String title){
        fragments.add(fragment);
        titles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}
public void setUpViewPager(ViewPager viewPager){
    MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
    adapter.addFragment(new Fragment_Home(),"Home");
    adapter.addFragment(new Fragment_Catagory(),"Category");
    adapter.addFragment(new Fragment_Top5(),"Top 5 Expences");
    adapter.addFragment(new Fragment_Statistics(),"Statistics");
    viewPager.setAdapter(adapter);
}
private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
                    return true;
                }
            });
}
}

带有网格布局的片段 包 com.bajpays.financemanager;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by Bajpay's on 7/22/2016.
 */
public class Fragment_Home extends Fragment {
View view;
ArrayList<Model_Grid> grids;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup         container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.layout_home, container, false);
    grids=makegrids();
    GridAdapter adapter=new GridAdapter(this.getActivity(),grids);
    GridView gridView=(GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(adapter);

    Toast.makeText(this.getActivity(), "oncreate of home rns", 200).show();
    return view;
}
public ArrayList<Model_Grid> makegrids()
{
    ArrayList<Model_Grid> grids=new ArrayList<>();
    grids.add(new Model_Grid(R.drawable.cheese_1, "food"));
    grids.add(new Model_Grid(R.drawable.cheese_2,"cloth"));
    grids.add(new Model_Grid(R.drawable.cheese_3,"outing"));
    grids.add(new Model_Grid(R.drawable.cheese_4,"bank"));
    grids.add(new Model_Grid(R.drawable.cheese_5,"travel"));
    grids.add(new Model_Grid(R.drawable.cheese_1,"rent"));
    grids.add(new Model_Grid(R.drawable.cheese_2,"health"));
    grids.add(new Model_Grid(R.drawable.cheese_3,"makeup"));
    grids.add(new Model_Grid(R.drawable.cheese_4,"home"));
    grids.add(new Model_Grid(R.drawable.cheese_5,"daily"));
    grids.add(new Model_Grid(R.drawable.cheese_1,"study"));
    grids.add(new Model_Grid(R.drawable.cheese_5,"electronics"));
    grids.add(new Model_Grid(R.drawable.cheese_4,"mobile"));
    grids.add(new Model_Grid(R.drawable.ic_forum,"add new"));
    return grids;
}
}


class GridAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
View view;
ImageView imageView;
TextView gridname;
ArrayList<Model_Grid> grids=new ArrayList<>();
GridAdapter(Context context,ArrayList<Model_Grid> gridlist){
    this.context=context;
    grids=gridlist;
    inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
    return grids.size();
}

@Override
public Object getItem(int position) {
    return grids.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    view=inflater.inflate(R.layout.dapter_for_home,null);
    imageView=(ImageView)view.findViewById(R.id.grid_img);
    gridname=(TextView)view.findViewById(R.id.grid_name);
    imageView.setImageResource(grids.get(position).getImageid());
    gridname.setText(grids.get(position).getGridName());
    return view;
}
}

MainActivity的布局

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#659eaf"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="enterAlways|scroll|snap" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff75cc"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

<android.support.design.widget.NavigationView
    android:id="@+id/navigatioview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/header"
    app:menu="@menu/menu"/>
</android.support.v4.widget.DrawerLayout>

片段布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:columnWidth="120dp"
    android:stretchMode="spacingWidthUniform"
    android:paddingBottom="50dp">
    </GridView>


</LinearLayout>

【问题讨论】:

    标签: android gridview android-appbarlayout


    【解决方案1】:

    它仅适用于 Lollipop 添加此代码-

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             gridview.setNestedScrollingEnabled(true);
        }
    

    否则尝试将您的网格布局放在 NestedScrollview 中。希望这会有所帮助。

    【讨论】:

    • 如果是这样,那么在回收站视图的情况下它是如何工作的。如果问题与设备兼容性有关,也应该存在相同的问题。而且我滚动网格视图没有问题。问题是当我向上滚动它时,actionBar 并没有像回收站视图那样隐藏。
    • 是的,它只适用于 recycleview,而对于 listview/gridview,您必须将其放入 NestedScrollview 或尝试该代码
    猜你喜欢
    • 1970-01-01
    • 2012-01-03
    • 2022-10-19
    • 2012-05-21
    • 2012-08-03
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多