GridView를 ScrollView에 넣는 방법
나는 전체 레이아웃은 내가에서 관련 콘텐츠 표시해야 스크롤 내부에 배치해야하도록 레이아웃 설계해야 Grid
내가 사용하기로 결정 그래서 양식을 GridView
나누었다 문제 것은 내가 사용하는 드릴 수 없습니다 것입니다 GridView
내부 ScrollView
나는 문서를 읽고 (문서는 또한 우리가해야한다는 GridView
내부에 사용하지 마십시오. ScrollView
) 그러나 나는 이것을해야하므로 이것에 대해 나에게 약간의 아이디어를 줄 수 있습니다.
고유 한 스크롤 외에 GridView에는 확실히 이점이 있습니다. 예를 들어, 전달하는 데이터를 기반으로 확장 및 축소되는 일관된 동적 셀 레이아웃입니다. 따라서 사람들이 그러한 기능을 원하는 것이 좋지 않다고 말할 때, 스크롤링 뷰 내부에 이미지 (뷰)의 동적 그리드를 원할 수 있지만 전체 스크롤 뷰가 그리드 이외의 것을 포함하기를 원하기 때문에 잘못된 것 같습니다. 세포.
자, 이것을 할 수있는 방법이 있습니다. 여기 에서 답을 확인 하십시오 . 프로젝트에서 가져 오거나 만들려는 확장 가능한 높이 GridView입니다. 이것이 기본적으로 의미하는 것은 GridView에 더 많은 항목이 추가되면 높이 설정을 유지하고 스크롤을 사용하는 대신 높이가 확장된다는 것입니다. 이것이 바로 당신이 원하는 것입니다.
프로젝트에 ExpandableHeightGridView가 있으면 GridView를 배치 할 XML 레이아웃으로 이동합니다. 그런 다음 다음과 같이 할 수 있습니다 (패러 프레이징).
<ScrollView ...>
<RelativeLayout ...>
<com.example.ExpandableHeightGridView ... />
<other view items />
</RelativeLayout>
</ScrollView>
그런 다음 GridView의 어댑터를 설정 한 활동에서 확장하도록 설정해야합니다. 그래서:
ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId);
gridView.setAdapter(yourAdapter);
gridView.setExpanded(true);
이 확장 가능한 GridView를 원하는 이유는 표준 GridView가 확장되지 않기 때문에 스크롤이 발생하기 때문입니다. 특정 높이에 고정 된 다음 더 많은 항목이 뷰 경계를 넘어서 채워지면 스크롤 가능해집니다. 이제 GridView는 항상 콘텐츠에 맞게 높이를 확장하므로 스크롤 모드로 들어갈 수 없습니다. 이를 통해 ScrollView 내에서 사용하고 ScrollView 내에서 위 또는 아래의 다른 뷰 요소를 사용하고 모두 스크롤 할 수 있습니다.
이것은 당신이 찾고있는 결과를 제공 할 것입니다. 궁금한 점이 있으면 알려주세요.
내가 늦었다는 것을 알고 있지만 공유해야하고 완벽하게 작동하는 또 다른 솔루션이 있습니다. 여기서 메서드는 포함 된 항목 수를 기준으로 GridView 높이를 계산하고 런타임에 GridView에 높이를 설정합니다.
public void setGridViewHeightBasedOnChildren(GridView gridView, int columns) {
ListAdapter listAdapter = gridView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int items = listAdapter.getCount();
int rows = 0;
View listItem = listAdapter.getView(0, null, gridView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
float x = 1;
if( items > columns ){
x = items/columns;
rows = (int) (x + 1);
totalHeight *= rows;
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
setAdapter
gridview를 호출 한 후
setGridViewHeightBasedOnChildren( <yourGridView> , <no of grid view columns> )
작동합니다.
평소처럼 xml에서 gridview를 정의 할 수 있습니다.
코드가 처리하도록합니다. :)
내가 줄 수있는 방법을 발견 의 GridView에게 고정 된 크기 있는 ScrollView 내부 및 스크롤 가능 을. 그러면 GridView의 모든 요소를 스크롤하지 않고도 전체 ScrollView를 볼 수 있으며, ExpandableHeightGridView 를 사용하는 것이 더 합리적 입니다.
이렇게하려면 GridView를 확장하는 새 클래스를 구현하고 onTouchEvent () 를 재정 의하여 requestDisallowInterceptTouchEvent (true) 를 호출 해야합니다 . 따라서 상위 뷰는 그리드 인터셉트 터치 이벤트를 떠납니다.
GridViewScrollable.java :
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.GridView;
public class GridViewScrollable extends GridView {
public GridViewAdjuntos(Context context) {
super(context);
}
public GridViewAdjuntos(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridViewAdjuntos(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent ev){
// Called when a child does not want this parent and its ancestors to intercept touch events.
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
}
}
ScrollView 내에서 원하는 특성과 여백으로 레이아웃에 추가하십시오.
<ScrollView 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"
android:isScrollContainer="true" >
<com.example.GridViewScrollable
android:id="@+id/myGVS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</ScrollView>
그리고 당신의 활동에서 얻으십시오.
GridViewScrollable myGridView = (GridViewScrollable) findViewById(R.id.myGVS);
나는 그것이 도움이되기를 바랍니다 =)
To use gridview inside scrollview, set scrollview android:fillViewport="true"
I know its late to answer here but may be useful to some new developers.
I was also looking to make a grid view scrollable inside a Nested Scroll View.
This answer on another question helped me: https://stackoverflow.com/a/38612612
Enable your GridView Property as
android:nestedScrollingEnabled="true"
Original poster: Droid_Dev
If you have used dennisdrew's answer and you have content above your grid and it gets hidden when you open your view simply add the last line below
ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId);
gridView.setAdapter(yourAdapter);
gridView.setExpanded(true);
gridView.setFocusable(false);
It's better to use ExpandableHeightlistView
or ExpandableHeightGridView
inside scrollview
.
Logic
Calculate entire height by providing a very large height hint. But do not use the highest 2 bits of this integer; those are reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
for further information explore the example below with demo
i would not force the issue. GridView does not work well in a scrollView. instead convert your gridview into a recyclerview and use a gridLayoutManager to resolve the issue.
gridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
// Setting on Touch Listener for handling the touch inside ScrollView
참고URL : https://stackoverflow.com/questions/12859250/how-to-put-gridview-inside-scrollview
'IT Share you' 카테고리의 다른 글
Java 상수 풀의 목적은 무엇입니까? (0) | 2020.11.16 |
---|---|
파이썬에서 확장자가없는 파일 유형을 확인하는 방법은 무엇입니까? (0) | 2020.11.16 |
그룹별로 첫 번째 행 선택 (0) | 2020.11.16 |
공유 호스트의 " 'LOG_BACKUP'으로 인해 데이터베이스의 트랜잭션 로그가 가득 찼습니다." (0) | 2020.11.16 |
JDK8-Maven javadoc 플러그인을 사용하여 javadoc을 생성하려고 할 때 "javax.interceptor.InterceptorBinding에 대한 클래스 파일을 찾을 수 없음"오류 발생 (0) | 2020.11.16 |