Android:gridView设置不可滚动?

时间:2017-07-05 04:59:29

标签: android android-gridview

我试图通过gridView进行过滤部分。在宽屏幕上它很好。但是当它的小屏幕无法正常工作时。 (一个视图是两次或三次,一些是消失的)这是因为gridView项目的缩小和滚动正在出现。所以我试图删除gridView滚动。

我的活动是。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="fill_parent"
     android:layout_width="fill_parent"
    android:fillViewport="true"
    android:scrollbars="vertical">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:titleTextColor="@android:color/white"
        app:contentInsetStartWithNavigation="0dp">
    </android.support.v7.widget.Toolbar>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="11"
            android:orientation="vertical"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp">

        <TextView
            android:text="Price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start|bottom"
            android:textStyle="bold"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="20dp" />

        <TextView
            android:id="@+id/seekbar_value"
            android:text="Any"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|bottom"
            android:textStyle="bold"/>


        <org.florescu.android.rangeseekbar.RangeSeekBar
            android:id="@+id/range_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:showLabels="false"
            app:valuesAboveThumbs="false"
            app:barHeight="5dp"
            app:alwaysActive="true"
            app:thumbDisabled="@drawable/seekbar_button"
            app:textAboveThumbsColor="@color/colorPrimary"
            app:defaultColor="@color/colorPrimary"
            app:activeColor="@color/colorButton" />

        <TextView
            android:text="Block"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start|bottom"
            android:textStyle="bold"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="20dp"/>

            <GridView
                android:id="@+id/gridViewBlock"
                android:numColumns="auto_fit"
                android:gravity="center"
                android:columnWidth="60dp"
                android:stretchMode="columnWidth"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"/>

            <TextView
            android:text="Floor"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start|bottom"
            android:textStyle="bold"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="20dp"/>

           <!--  This Grid View is my problem -->
            **<GridView
                android:id="@+id/gridViewFloor"
                android:numColumns="auto_fit"
                android:gravity="center"
                android:columnWidth="60dp"
                android:stretchMode="columnWidth"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"/>**

        </LinearLayout>

        <Button
            android:text="See result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:textStyle="bold"
            android:background="@color/colorButton"
            android:textColor="@android:color/white" />

    </LinearLayout>

</LinearLayout>
</ScrollView>

我以编程方式添加gridView项目:

public class FIlterActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private RangeSeekBar seekbar;
    private TextView seekbarValue;
    private GridView gridViewBlock;
    private GridView gridViewFloor;
    private FilterButtonAdapter filterBlockAdapter;
    private FilterButtonAdapter filterFloorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitle(getString(R.string.activity_filter));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        gridViewBlock = (GridView) findViewById(R.id.gridViewBlock);

        filterBlockAdapter = new FilterButtonAdapter(this, Filter.filter_blocks, Filter.blocks_selected);
        gridViewBlock.setAdapter(filterBlockAdapter);

        gridViewBlock.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                final TextView textView = (TextView)view.findViewById(R.id.filter_button);
                String value = textView.getText().toString();
                boolean contain = false;

                for(int i = 0; i < Filter.blocks_selected.size(); i++){
                    if(value.equals(Filter.blocks_selected.get(i).toString())) {
                        view.setBackgroundResource(R.drawable.button_disabled);
                        Filter.blocks_selected.remove(i);
                        if(Filter.blocks_selected.size() == 0){
                            Filter.blocks_selected.add(Filter.filter_blocks[0]);
                            adapterView.getChildAt(0).setBackgroundResource(R.drawable.button_enabled);
                        }
                        contain = true;
                        break;
                    }
                }
                if(!contain){
                    if(value.equals("Any")){
                        Filter.blocks_selected.clear();
                        Filter.blocks_selected.add(Filter.filter_blocks[0]);
                        for(int i = 1; i < Filter.filter_blocks.length; i++){
                            adapterView.getChildAt(i).setBackgroundResource(R.drawable.button_disabled);
                        }
                    }
                    else{
                        for(int i = 0; i < Filter.blocks_selected.size(); i++){
                            if(Filter.blocks_selected.get(i).toString().equals("Any")){
                                adapterView.getChildAt(0).setBackgroundResource(R.drawable.button_disabled);
                                Filter.blocks_selected.remove(i);
                                break;
                            }
                        }
                    }
                    view.setBackgroundResource(R.drawable.button_enabled);
                    Filter.blocks_selected.add(value);}
                filterBlockAdapter.notifyDataSetChanged();
            }
        });

        gridViewFloor = (GridView) findViewById(R.id.gridViewFloor);
        filterFloorAdapter = new FilterButtonAdapter(this, Filter.filter_floors, Filter.floor_selected);
        gridViewFloor.setAdapter(filterFloorAdapter);


        gridViewFloor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {


                final TextView textView = (TextView)view.findViewById(R.id.filter_button);
                String value = textView.getText().toString();
                boolean contain = false;

                for(int i = 0; i < Filter.floor_selected.size(); i++){
                    if(value.equals(Filter.floor_selected.get(i).toString())) {
                        view.setBackgroundResource(R.drawable.button_disabled);
                        Filter.floor_selected.remove(i);
                        if(Filter.floor_selected.size() == 0){
                            Filter.floor_selected.add(Filter.filter_floors[0]);
                            adapterView.getChildAt(0).setBackgroundResource(R.drawable.button_enabled);
                        }
                        contain = true;
                        break;
                    }
                }
                if(!contain){
                    if(value.equals("Any")){
                        Filter.floor_selected.clear();
                        Filter.floor_selected.add(Filter.filter_floors[0]);
                        for(int i = 1; i < Filter.filter_floors.length; i++){
                            adapterView.getChildAt(i).setBackgroundResource(R.drawable.button_disabled);
                        }
                    }
                    else{
                        for(int i = 0; i < Filter.floor_selected.size(); i++){
                            if(Filter.floor_selected.get(i).toString().equals("Any")){
                                adapterView.getChildAt(0).setBackgroundResource(R.drawable.button_disabled);
                                Filter.floor_selected.remove(i);
                                break;
                            }
                        }
                    }
                    view.setBackgroundResource(R.drawable.button_enabled);
                    Filter.floor_selected.add(value);}
                filterFloorAdapter.notifyDataSetChanged();
            }
        });



        seekbar = (RangeSeekBar)findViewById(R.id.range_seekbar);
        seekbarValue = (TextView)findViewById(R.id.seekbar_value);

        seekbar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener() {
            @Override
            public void onRangeSeekBarValuesChanged(RangeSeekBar bar, Object minValue, Object maxValue) {
                if(String.valueOf(bar.getAbsoluteMaxValue()).equals(maxValue.toString())){

                    if(String.valueOf(bar.getAbsoluteMinValue()).equals(minValue.toString()))
                        seekbarValue.setText("Бүх");
                    else {
                        seekbarValue.setText(minValue.toString() + " $ - +");
                    }
                } else if(String.valueOf(bar.getAbsoluteMinValue()).equals(minValue.toString())){
                    seekbarValue.setText(maxValue.toString() + " $ - -");
                } else{
                    seekbarValue.setText(minValue.toString() + " $ - " + maxValue.toString() + " $");
                }
            }
        });
    }
}

和过滤器按钮适配器

public class FilterButtonAdapter extends BaseAdapter {

    private Context context;
    private final String[] filterValues;
    private List<String> checkedFilterValues = new ArrayList<String>();


    public FilterButtonAdapter(Context context, String[] filterValues, List<String> checkedFilterValues){
        this.context = context;
        this.filterValues = filterValues;
        this.checkedFilterValues = checkedFilterValues;
    }
    @Override
    public int getCount() {
        return filterValues.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        if(convertView == null){

            gridView = new View(context);
            gridView = inflater.inflate(R.layout.layout_button, null);

            final TextView textView = (TextView)gridView.findViewById(R.id.filter_button);
            textView.setText(filterValues[position]);

            boolean contain = false;
            for(int i = 0; i < Filter.blocks_selected.size(); i++){
                if(filterValues[position].equals(checkedFilterValues.get(i)))
                {
                    gridView.setBackgroundResource(R.drawable.button_enabled);
                    contain = true;
                    break;
                }
            }
            if(!contain){
                gridView.setBackgroundResource(R.drawable.button_disabled);
            }
        } else gridView = (View)convertView;
        return gridView;
    }
}

过滤类:

public class Filter {

    public static String price = "any";
    public static long priceMax = 0;
    public static long priceMin = 0;
    public static final String[] filter_blocks = new String[]{
            "any", "A", "B", "C"
    };
    public static final String[] filter_floors = new String[]{
            "any", "1", "2", "3", "4", "5", "6",
            "7", "8", "9", "10", "11", "12+"
    };

    public static List<String> blocks_selected = new ArrayList<String>(){{
        add("any");
    }
    };

    public static List<String> floor_selected = new ArrayList<String>(){{
        add("any");
    }
    };

    public static int floor = 10;

}

我的android studio仿真器是2.7 QVGA API 25

2 个答案:

答案 0 :(得分:0)

试试这个

ViewCompat.setNestedScrollingEnabled(gridView, true);

答案 1 :(得分:0)

请勿在{{1​​}}内使用GridView,因为两者都具有滚动功能,因此无法正常使用。

使用ScrollView代替NestedScrollView

  

ScrollView就像NestedScrollView一样,但它支持表演   作为ScrollView滚动nestedparent同时适用于新旧版本   Android版本。默认情况下启用嵌套滚动。

请参阅documentation

更新您的布局结构,如下所示:

child