0

こんにちは、今日はスクロールビューで gridView スクロールが必要です。おそらくこのように:スクロールビューにLinearLayoutがあり、GridViewをLinearLayoutに挿入しました。質問 gridView に 1 つのアイテムしか表示されていません。

私の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>

私のJavaコード:

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);
}
4

2 に答える 2

2

以下に示すように、カスタム スクロール ビューを作成するだけです (このクラスの名前は 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;
}
}

xml では、線形レイアウトとグリッド ビューをこのカスタム スクロール ビュー内に配置する必要があります。

<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>

パッケージ名は、上記のカスタム スクロール ビュー クラスを配置したパッケージを示していることに注意してください。

于 2013-08-27T08:15:44.523 に答える
1

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 内でその上または下にある他のビュー要素を使用して、それらをすべてスクロールさせることができます。

于 2013-08-27T06:12:57.443 に答える