2013-12-18 10 views
5

私のアプリケーションでは、スクロールビュー内でリストビューを使用しています。しかし、リストビューはスクロールしていません。誰かが私に何をすべきか教えてもらえますか? 私はそれを探して、リストビューがスクロールビュー内でスクロールしないことを確認します。
スクロールビュー内のリストビューのスクロール機能

+0

すでにスクロール可能なリストビューにスクロールビューを追加する特別な理由はありますか? –

答えて

8

独自のリストビューを作成し、展開をfalseに設定できます。ここであなたは、スクロールビュー内でリストビューの場所でExpandableHeightListViewを使用することができ、あなたのactivity.clas

ExpandableHeightListView listview = (ExpandableHeightListView) findViewById(R.id.list); 
    listview.setExpanded(true); 

レイアウトファイルで、次のように使用できるサンプルクラス

public class ExpandableHeightListView extends ListView { 

boolean expanded = false; 

public ExpandableHeightListView(Context context) { 
    super(context); 
} 

public ExpandableHeightListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public ExpandableHeightListView(Context context, AttributeSet attrs, 
     int defStyle) { 
    super(context, attrs, defStyle); 
} 

public boolean isExpanded() { 
    return expanded; 
} 

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // HACK! TAKE THAT ANDROID! 
    if (isExpanded()) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(
       Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = getMeasuredHeight(); 
    } else { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
} 

public void setExpanded(boolean expanded) { 
    this.expanded = expanded; 
} 
} 

です。スクロールします。

+3

アイテムリサイクルを許可しない場合、ListViewを使用するポイントは何ですか?その後、垂直のLinearLayoutを使用してください。 – BladeCoder

1

スクロールビュー内にリストビューを配置しないでください。

リストビューは既にスクロールを処理しているので、スクロールビューの内側にある必要はありません。

これを反映するようにレイアウトを変更する必要があります。

5

リストビューにはすでにスクロール機構があり、ScrollViewからListViewを除外します。

+2

私はスクロールビューからlistViewを除外することはできませんので、フォームのように。 – Sandy

+0

私はなぜ問題解決から離れて最大の票が得られるのか分かりません:P – IronManGill

+0

@IronManGillは嫉妬? :P –

0

私はこれをしなければならなかったときと同じように非常に特殊なケースを想定しています。私はこれを行うListViewコントロール用のアダプタを初期化した後、その後

public class ScrollViewListHelper { 
public static void getListViewSize(ListView myListView) { 
    ListAdapter myListAdapter = myListView.getAdapter(); 
    if (myListAdapter == null) { 
     //do nothing return null 
     return; 
    } 
    //set listAdapter in loop for getting final size 
    int totalHeight = 0; 
    for (int size = 0; size < myListAdapter.getCount(); size++) { 
     View listItem = myListAdapter.getView(size, null, myListView); 
     listItem.measure(0, 0); 
     totalHeight += listItem.getMeasuredHeight(); 
    } 
    //setting listview item in adapter 
    ViewGroup.LayoutParams params = myListView.getLayoutParams(); 
    params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1)); 
    myListView.setLayoutParams(params); 
    // print height of adapter on log 
    Log.i("height of listItem:", String.valueOf(totalHeight)); 
} 

}

と:私はこのようになりますヘルパークラスを持っているスクロールビュー内

ScrollViewListHelper.getListViewSize(listViewMeasurements); 
1

ListViewコントロールが動作しません。 。それをそれの側に置いてください。両方のスクロール機能を持​​っているので、スクロールは一緒に来るときに動作しません。

0
import android.content.Context; 
import android.widget.ListView; 

public class MyListView extends ListView { 

    public MyListView(Context context) { 
     super(context); 
    } 

    public MyListView(Context context, android.util.AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
    } 
} 

使用この

1

このクラスを使用します。

public class CustomScrollView extends ScrollView { 
    public CustomScrollView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
    } 

    public boolean onTouchEvent(MotionEvent ev) { 
     return true; 
    } 

    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return false; 
    } 
} 

xmlレイアウトで、scrollScrollタグをCustomScrollViewのパッケージ名とクラスで変更します。すなわちcom.test.CustomScrollViewに変更します。

あなたの内部では、カスタムスクロールビューのIDが取得され、このコードが含まれます。

private int currentX, currentY; 
private CustomScrollView customScrollView; 

customScrollView.setSmoothScrollingEnabled(true); 
customScrollView.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 
     switch(event.getAction()){ 
     case MotionEvent.ACTION_DOWN: { 
      currentX = (int) event.getRawX(); 
      currentY = (int) event.getRawY(); 
      break; 
     } 
     case MotionEvent.ACTION_MOVE: { 
       @SuppressWarnings("unused") 
       int x2 = (int) event.getRawX(); 
       int y2 = (int) event.getRawY(); 
       customScrollView.scrollBy(0 , currentY - y2); 
       currentY = y2; 
       break; 
      } 
      case MotionEvent.ACTION_UP: { 
       break; 
      } 
     } 
     return true; 
    } 
}); 
0

スクロールビューは、すべてのタッチ...あなたは別のスクロール要素(スクロールビュー)へのスクロール要素(リストビュー)を挿入することは避けてください を「食べる」、ユーザーが夢中になることができます。これらのケースでは

私はListViewコントロールの代わりにのLinearLayoutを使用して、私はそれにのCustomViewを膨らませる各リスト要素を表すために作成し、ここでの例で、より多くの私に尋ねること自由に落ちたが:

LinearLayout containerLinearLayout= (LinearLayout)findViewById(R.id.containerLinearLayout); 
containerLinearLayout.removeAllViews(); 
for(int i=0;i<array.length();i++){ 
JSONObject convocazione=array.getJSONObject(i); 
View child = getLayoutInflater().inflate(R.layout.list_element_layout,null); 
TextView txtDettaglioLuogo=(TextView)child.findViewById(R.id.txtDettaglioLuogo); 
TextView txtOra=(TextView)child.findViewById(R.id.txtOra); 


txtOra.setText("text"); 
txtData.setText("more text"); 


containerLinearLayout.addView(child); 
} 
1

ドンScrollView内にlistViewを配置しないでください。あなたは、スクロールビューからリストビューを除外することができ

How can I put a ListView into a ScrollView without it collapsing?

: あなたは上記のロマン・ガイの答えとコメントをお読みください。 scrollViewの中に「リスト」を入れたい場合は、LinearLayoutを使うことができます。このようなもの:

public class MyListLayout extends LinearLayout implements 
     View.OnClickListener { 

    private Adapter list; 
    private View.OnClickListener mListener; 

    public MyListLayout(Context context) { 
     super(context); 
    } 

    public MyListLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

    } 

    public EvernoteListLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public void onClick(View v) { 
     if (mListener!=null) 
      mListener.onClick(v); 
    } 

    public void setList(Adapter list) { 
     this.list = list; 

     //Popolute list 
     if (this.list!=null){ 
      for (int i=0;i<this.list.getCount();i++){ 
       View item= list.getView(i, null,null); 
       this.addView(item); 
      } 
     } 

    } 

    public void setmListener(View.OnClickListener mListener) { 
     this.mListener = mListener; 
    } 
} 



// Create ArrayAdapter 
MyListAdapter mListAdapter = new MyListAdapter(); 
MyListLayout mLay = (MyListLayout) findViewById(R.id.box_list_ev); 
if (mLay != null) { 
     mLay.setList(mListAdapter); 
} 
+0

'Mylistlayout'をlistviewのように動作させるだけですか? –

0

最後に私はスクロールの問題の解決策を得ました。スクロールビューを管理するカスタムのscrollviewクラスがあります。リストビューのonMeasureのソースコードは、あなたが高さmeasureSpecが指定されていないならば、リストビューの高さは一つだけのアイテムに加えて、いくつかのパディングの高さであることがわかります

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.widget.ScrollView; 

public class VerticalScrollview extends ScrollView{ 

public VerticalScrollview(Context context) { 
    super(context); 
} 

public VerticalScrollview(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public VerticalScrollview(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; 
} 
} 
0

チェック、下記を参照してください:

if (heightMode == MeasureSpec.UNSPECIFIED) { 
     heightSize = mListPadding.top + mListPadding.bottom + childHeight + 
       getVerticalFadingEdgeLength() * 2; 
    } 

したがって、単純に高さSpecModeをAT_MOSTに変更できます。高さサイズは、親の左の高さサイズに簡単に変更できます。 以下は解決策です:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), 
      MeasureSpec.AT_MOST); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
+1

このコードスニペットは問題を解決するかもしれませんが、[説明を含む](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)は本当にあなたの投稿の質を向上させるのに役立ちます。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。 – DimaSan

関連する問題