2013-12-18 15 views
5

स्क्रॉल दृश्य में मेरे एप्लिकेशन में, मैं सूची दृश्य का उपयोग कर रहा हूं। लेकिन सूची दृश्य स्क्रॉल नहीं कर रहा है। क्या कोई मुझे सुझाव दे सकता है कि मुझे क्या करना चाहिए। मैंने इसकी खोज की और पता चला कि सूची दृश्य स्क्रॉल दृश्य के भीतर स्क्रॉल नहीं करते हैं।
कोई समाधान?स्क्रॉल व्यू के भीतर सूचीदृश्य की स्क्रॉल सुविधा

+0

पहले से स्क्रॉल सक्षम सूचीदृश्य को स्क्रॉलव्यू जोड़ने के लिए कोई विशेष कारण? –

उत्तर

8

आप अपनी खुद की सूची दृश्य बना सकते हैं और गलत पर विस्तार कर सकते हैं। यहाँ नमूना वर्ग

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

आप अपने activity.clas

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

अपने लेआउट फ़ाइल में इस तरह उपयोग कर सकते हैं आप एक स्क्रॉल दृश्य के भीतर सूची दृश्य के स्थान पर ExpandableHeightListView का उपयोग कर सकते है। यह स्क्रॉल करेगा।

+3

यदि आप कोई आइटम रीसाइक्लिंग की अनुमति नहीं देते हैं तो ListView का उपयोग करने का क्या मतलब है? बेहतर एक लंबवत रैखिक Layout का उपयोग करें। – BladeCoder

1

स्क्रॉलव्यू के अंदर एक सूचीदृश्य न डालें।

सूचीदृश्य पहले ही स्क्रॉलिंग को संभालता है, इसलिए इसे स्क्रॉलव्यू के अंदर होने की आवश्यकता नहीं है।

आपको इसे प्रतिबिंबित करने के लिए अपने लेआउट बदलना चाहिए।

5

स्क्रॉलव्यू से ListView को बाहर निकालें, क्योंकि ListView में पहले से स्क्रॉलिंग तंत्र है।

+2

इसकी तरह एक फॉर्म है इसलिए मैं स्क्रॉल व्यू से सूची दृश्य को बाहर नहीं कर सकता। – Sandy

+0

मुझे नहीं पता कि समस्या समाधान से दूर क्यों चलना अधिकतम वोट प्राप्त होता है: पी – IronManGill

+0

@IronManGill ईर्ष्या? : पी –

0

मुझे लगता है कि आपके पास एक बहुत ही खास मामला है जैसा मैंने किया था जब मैंने ऐसा किया था।

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

}

और फिर मैं यह करने के बाद मैं ListView के लिए एडाप्टर प्रारंभ:

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

और अपने एक्सएमएल लेआउट में अपने स्क्रॉलव्यू टैग को पैकेज नाम और कस्टमस्क्रॉल व्यू के वर्ग के साथ बदलें। यानी com.test.CustomScrollView में बदलें।

और आपके अंदर गतिविधि कस्टम स्क्रॉल दृश्य की आईडी प्राप्त करें और इस कोड को शामिल करें।

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

डॉन:

इन मामलों मैं ListView के स्थान पर एक LinearLayout का उपयोग में है और मैं इसे में बढ़ customview प्रत्येक सूची तत्व का प्रतिनिधित्व करने के लिए बनाया है, यहाँ एक उदाहरण है, मुझे और अधिक पूछने के लिए स्वतंत्र गिर गया एक स्क्रॉलव्यू के अंदर एक सूची देखें नहीं। आप रोमेन लड़का जवाब और ऊपर टिप्पणी पढ़ना चाहिए:

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

आप स्क्रॉल देखने से ListView बाहर कर सकते हैं। यदि आप स्क्रॉल व्यू के अंदर "सूची" रखना चाहते हैं तो आप एक लीनियरलाउट का उपयोग कर सकते हैं।इस तरह कुछ:

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' को सूचीदृश्य की तरह काम करना होगा? –

0

अंत में मुझे स्क्रॉलिंग समस्या का समाधान मिला, मुझे स्क्रॉलव्यू का प्रबंधन करने के लिए कस्टम स्क्रॉलव्यू क्लास मिला।

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

चेक ListView की onMeasure स्रोत कोड, तो आप पाएंगे कि यदि ऊंचाई measureSpec अनिर्दिष्ट है तो सूचीदृश्य की ऊंचाई केवल एक आइटम प्लस कुछ गद्दी की ऊंचाई है, नीचे देखें:

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

तो हम ऊंचाई SPCMode को 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

संबंधित मुद्दे