Reputation: 167
I'm trying to do filter section by gridView. In a wide screen it's fine. But when it's small screen is not working correctly. (One view is two or three times, some is dissapearing) It's because gridView item's shrinking and scroll is appearing. So i trying to remove gridView scroll.
My activity is.
<?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>
And i programmatically adding gridView items:
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() + " $");
}
}
});
}
}
And Filter Button Adapter
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;
}
}
Filter class:
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;
}
My android studio emulator is 2.7 QVGA API 25
Upvotes: 0
Views: 4104
Reputation: 21766
Don't use GridView
inside ScrollView
, because both has scrolling functionality and due to this it will not work properly.
Use NestedScrollView
instead of ScrollView
.
NestedScrollView
is just likeScrollView
, but it supports acting as both anested
scrollingparent
andchild
on both new and old versions of Android. Nested scrolling is enabled by default.
See documentation.
Update your layout structure as below:
<?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"
android:id="@+id/layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/layout_scorll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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"
android:nestedScrollingEnabled="false"/>
<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"
android:nestedScrollingEnabled="false"/>
</LinearLayout>
<Button
android:text="See result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:background="@color/colorAccent"
android:textColor="@android:color/white" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Upvotes: 1
Reputation: 1842
Try this
ViewCompat.setNestedScrollingEnabled(gridView, true);
Upvotes: 0