2015-11-24 18 views
5

मैं एक RecyclerView एक ScrollView अंदर लागू करने कर रहा हूँ। पूरे पृष्ठ पर केवल एक स्क्रॉलिंग व्यवहार करने के लिए मैं NonScrollRecyclerView संस्करण लागू करता हूं। कार्यान्वयन इस प्रकार है:Android SDK में गैर स्क्रॉल RecyclerView स्क्रॉल मुद्दा 23

public class NonScrollRecyclerView extends RecyclerView { 
    public NonScrollRecyclerView(Context context) { super(context); } 

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

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

    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev){ 

     if(ev.getAction() == MotionEvent.ACTION_MOVE) 
      return true; 

     return super.dispatchTouchEvent(ev); 
    } 
} 

एक बार मैं अपने निर्माण और लक्ष्य सेटिंग को अपडेट 23 SDK करने के लिए, मैं मुसीबत पृष्ठ NonScrollRecyclerView शामिल स्क्रॉल है। विशिष्ट समस्या यह है कि जब तक मैं रीसाइक्लर व्यू भाग तक नहीं पहुंच जाता तब तक पेज स्क्रॉल ठीक हो जाता है और एक बार जब मैं इस दृश्य पर स्क्रॉल करता हूं तो मैं ऊपर या नीचे या तो स्क्रॉल करने में असमर्थ हूं। - @ लेआउट/आर वी

एक्सएमएल@layout/rv recycler दृश्य

<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:background="@color/background_gray"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/background_gray" 
    android:orientation="vertical"> 

    <include 
     layout="@layout/row_mall_header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_header" 
     android:layout_marginTop="8dp"/> 

    <include 
     layout="@layout/row_mall_shops" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_shops" 
     android:layout_marginTop="8dp"/> 

    <include 
     layout="@layout/row_mall_coupons" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_coupons" 
     android:layout_marginTop="8dp"/> 

    <include 
     layout="@layout/rv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_feeds" 
     android:layout_marginTop="8dp"/> 

    </LinearLayout> 
</ScrollView> 

एक्सएमएल शामिल हैं:

मैं एसडीके 22 के साथ और नीचे

मेरे एक्सएमएल इस समस्या का सामना DONOT इस प्रकार है

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/background_gray" 
    android:id="@+id/ll_mall_feeds"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white" 
    android:paddingTop="6dp" 
    android:paddingBottom="6dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tv_feedcount" 
     android:textColor="@color/semi_theme_blue" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="12dp" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

<com.project.ui.NonScrollRecyclerView 
    android:id="@+id/nrv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:divider="@android:color/transparent" 
    android:listSelector="@android:color/transparent" /> 

</LinearLayout> 
+2

आप क्यों नहीं बस एक LinearLayout बजाय एक RecyclerView का उपयोग नहीं करते हैं? आप अनिवार्य रूप से एक RecyclerView को एक LinearLayout में बदल रहे हैं, लेकिन बहुत अधिक ओवरहेड के साथ। –

+0

उपर्युक्त टिप्पणी के साथ सहमत हैं, बस रीसाइक्लर व्यू से छुटकारा पाएं, और, यदि आप इसे मानते हैं तो आप अब XML: // stackoverflow के बारे में पूछने के लिए नहीं जा रहे हैं।कॉम/प्रश्न/2 9 5 9 6014/क्यों-चाहिए-हम-उपयोग-एक्सएमएल-लेआउट – Nanoc

+0

आप अपनी आवश्यकता के अनुसार अपना खुद का रैखिक लेआउट प्रबंधक बना सकते हैं ... इस लिंक को जांचें https://github.com/serso/android-linear- लेआउट प्रबंधक/ब्लॉब/मास्टर/lib/src/मुख्य/जावा/org/Solovyev/एंड्रॉयड/विचारों/एलएलएम/LinearLayoutManager.java –

उत्तर

2

पुनर्नवीनीकरण दृश्य और सूची दृश्य को स्क्रॉलव्यू के अंदर अनुशंसित नहीं किया जाता है क्योंकि स्क्रॉलव्यू को प्रस्तुत करते समय तत्वों की गणना की गणना नहीं की जाती है। इसका अर्थ यह है कि, स्क्रॉलव्यू दिखाए जाने पर आपका एडाप्टर पॉप्युलेट नहीं किया जा सकता है और बाद में जब रीसाइक्लर व्यू डेटा परिवर्तनों के बारे में अधिसूचित किया जाता है (उदाहरण के लिए जब आप अपना एडाप्टर प्रारंभ करते हैं), तो तत्वों की ऊंचाई को फिर से समझने का कोई तरीका नहीं है।

यह वास्तव में गर्दन में दर्द है क्योंकि आपको तत्वों की ऊंचाई की गणना करने की कोशिश करनी है और यह कभी भी सटीक नहीं है, इसलिए स्क्रॉलव्यू के अंदर ListView या RecyclerView दिखाते समय आपके पास विसंगतियां होंगी। तत्वों की ऊंचाई की गणना करने के तरीके के कुछ उदाहरण here और here चेक किए जा सकते हैं।

मेरी सलाह एक LinearLayout में अपने RecyclerView कर देते हैं और तत्वों प्रोग्राम के रूप में जोड़ने के लिए है, तो आप ListView या RecyclerView व्यवहार emulates:

LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.files); 
layout.removeAllViews(); 

for (int i = 0; i < fileAdapter.getCount(); i++) { 
    final View item = fileAdapter.getView(i, null, null); 
item.setClickable(true); 

    item.setId(i); 

    item.setOnClickListener(new View.OnClickListener() { 
     @Override 
      public void onClick(View v) { 

       fileContentRowPosition = v.getId(); 

     # Your click action here 


    } 
}); 


layout.addView(item); 

} 

यहां फ़ाइलें परिभाषा के साथ अपने एक्सएमएल:

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

... 

    <LinearLayout 
     android:id="@+id/files" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:orientation="vertical"> 
    </LinearLayout> 


</ScrollView> 

पूरे जावा कोड को here और पूरे लेआउट here चेक किया जा सकता है।

दूसरी ओर, अगर आप अभी भी अपने मुद्दे के बारे में अपने कार्यान्वयन के साथ जारी रखने के लिए, और चाहते हैं, तो आप इस लेख के बारे में जाँच कर सकते हैं Handling Scrollable Controls in Scrollview

पूरे पृष्ठ में सादर,

1

केवल एक स्क्रॉल के लिए आप के बजाय NestedScrollView का उपयोग कर सकते हैं ScrollView और रीसाइक्लिंग View.setNestedScrollingEnabled (false) सेट करें;

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