Reputation: 21
good afternoon,Today i want a gridView scroll in scrollview. Perhaps like this: There are LinearLayout in scrollview,Then I inserted a GridView to LinearLayout. Question gridView is displaying only one item.
My xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/record_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dip">
<LinearLayout
android:id="@+id/record_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
My java code:
public class MainActivity extends Activity {
private LinearLayout linearLayout = null;
private ScrollView scrollView =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout)findViewById(R.id.record_Layout);
scrollView = (ScrollView)findViewById(R.id.record_scrollview);
GridView gd = new GridView(this);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "huangli1");
list.add(map);
map = new HashMap<String, String>();
map.put("name", "huangli2");
list.add(map);
map = new HashMap<String, String>();
map.put("name", "huangli3");
list.add(map);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.list_item, new String[]{"name"}, new int[]{R.id.tv}){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);
}
};
gd.setAdapter(simpleAdapter);
linearLayout.addView(gd);
}
Upvotes: 0
Views: 519
Reputation: 318
All you need to do is just make a custom scroll view as shown below: (The name of this class is MitsScrollView.java)
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class MitsScrollView extends ScrollView
{
public MitsScrollView(Context context)
{
super(context);
}
public MitsScrollView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MitsScrollView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
final int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false");
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false");
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false");
return false;
default:
Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action);
break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
super.onTouchEvent(ev);
Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction());
return true;
}
}
And in you xml you need to put the linear layout and grid view inside this custom scroll view:
<your package name.MitsScrollView
android:id="@+id/scrollViewOther"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#c4c4c4"
android:descendantFocusability="blocksDescendants"
android:fadeScrollbars="false"
android:fadingEdge="horizontal"
tools:ignore="ScrollViewCount" >
here you can put your linear layout or grid view or any views
</your package name.MitsScrollView>
Please note your package name indicates the package in which you have put the above custom scroll view class.
Upvotes: 2
Reputation: 8488
It is not good practice to put GridView in ScrollView. But you can do it though it is bit painful.
Now, here is how you can do this. Check the answer here. It is an expandable height GridView, which you will want to import / create in your project. What that basically means is that as more items are added to the GridView, it will just expand its height, as opposed to keeping its height set and using scrolling. This is exactly what you want.
Once you have the ExpandableHeightGridView in your project, go to your XML layout where you want the GridView to be. You can then do something like this (paraphrasing):
<ScrollView ...>
<RelativeLayout ...>
<com.example.ExpandableHeightGridView ... />
<other view items />
</RelativeLayout>
</ScrollView>
Then, in your activity where you set the GridView's adapter, you want to make sure you set it to expand. So:
ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId);
gridView.setAdapter(yourAdapter);
gridView.setExpanded(true);
The reason you want this expandable GridView is because, the fact that a standard GridView doesn't expand is what causes it to scroll. It sticks to a certain height, and then as more items fill it past its view bounds, it becomes scrollable. Now, with this, your GridView will always expand its height to fit the content within it, thus never allowing it to enter its scrolling mode. This enables you to use it inside of the ScrollView and use other view elements above or below it within the ScrollView, and have them all scroll.
Upvotes: 1