2015-07-02 28 views
10

पर अतिरिक्त पैडिंग जोड़ने वाले सजावटी देखें, इसलिए, मुझे अपनी परियोजना में रीसाइक्लर व्यू को लागू करते समय एक अजीब समस्या का सामना करना पड़ रहा है। मेरे पास एक स्थिर शीर्ष और नीचे पैडिंग को लागू करने के लिए एक कस्टम सजावट है और तत्वों के बीच पैडिंग का एक अलग मूल्य है। जब भी मैं रीफ्रेश पर टैप करता हूं, यानी बैक एंड से डेटा लाता है और रीसाइक्लर व्यू को फिर से प्रदर्शित करता है, तो पैडिंग बढ़ जाती है और यह प्रत्येक रीफ्रेश पर बढ़ती रहती है।पुनर्चक्रण देखें रीफ्रेश

This is the correct way

जब मैं ताज़ा हिट अंतरिक्ष (के कारण AsyncTask फिर से निष्पादित करने के लिए), के बीच आइटम वृद्धि हुई है। और यह प्रत्येक ताज़ा करने के साथ बढ़ता रहता है।

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/cv" 
android:layout_marginLeft="9dp" 
android:layout_marginRight="9dp" 
> 

    <RelativeLayout 
     android:id="@+id/itemLinearTop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingTop="17dp" 
     android:paddingRight="10dp" 
     android:paddingLeft="10dp"> 

     <TextView 
      android:id="@+id/tv1" 
      android:textColor="@color/logo_black" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 


     <TextView 
      android:id="@+id/tv2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/tv1" 
      android:textColor="@color/logo_black" 
      android:layout_alignParentRight="true" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <!-- More elements --> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 

मैं आरंभ कर रहा हूँ और इस तरह RecyclerView पॉप्युलेट:

Padding between items increasing

मैं RecyclerView अंदर इस

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="false" 
    android:focusableInTouchMode="false"> 
</android.support.v7.widget.RecyclerView> 

से पॉप्युलेट सामग्री की तरह एक ठेठ RecyclerView निम्नलिखित लेआउट के साथ CardView है:

// In PostExecute of AsyncTask 
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview); 
RecycleViewAdapter adapter = new FuelPricesRecycleViewAdapter(data); 
rv.setAdapter(adapter); 
rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1)); 

और ये मेरे RecyclerViewItemDecoration वर्ग ऐसा दिखाई देता है:

public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration { 
    private int space = 0; 
    private int item_count = 0; 
    private int ADDITIONAL_PADDING = 20; 

    public RecyclerViewItemDecoration(int space, int item_count) { 
     this.space = space; 
     this.item_count = item_count; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     if(parent.getChildPosition(view) != item_count) 
      outRect.bottom = space; 

     // Add top margin only for the first item to avoid double space between items 
     if(parent.getChildPosition(view) == 0) 
      outRect.top = space + ADDITIONAL_PADDING; 
     if(parent.getChildPosition(view) == item_count) 
      outRect.bottom = space + ADDITIONAL_PADDING; 
    } 
} 

आपकी मदद के लिए धन्यवाद!

+0

कुछ भी मिला है तो इसे ठीक करें? – codevscolor

+0

लगभग एक साल बाद, मुझे एक ही समस्या का सामना करना पड़ रहा है क्या आप रीफ्रेशर के बाद RecyclerView.scrollToPosition() को कॉल करते हैं? – Fraid

+0

अरे, क्या आपको कोई समाधान मिला? वर्तमान में मैं 'adapter.notifyItemRangeChanged (0, adapter.getItemCount(), खाली सूची()) को कॉल कर रहा हूं; 'एक वर्कअराउंड के रूप में।यह दृश्यों को दोबारा बिना सभी वस्तुओं को अमान्य कर देगा क्योंकि एक पेलोड है। (पेलोड कोई गैर-शून्य वस्तु हो सकती है)। मैं अभी भी एक बेहतर तरीका खोजना चाहूंगा। –

उत्तर

4

आप प्रत्येक बार रीसाइक्लिंगव्यू लोड होने पर Decoration जोड़ रहे हैं।

एक प्रयोग के बजाय झंडा:

private flagDecoration = false; 

if(!flagDecoration) 
{ 
    rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1)); 
    flagDecoration = true;  
} 
+0

नहीं, यह काम नहीं किया। –

+0

मैंने इस सजावट फ़ाइल का उपयोग किया है: https://gist.github.com/alexfu/0f464fc3742f134ccd1e – codevscolor

5

आप नए डेटा reseting से पहले आइटम सजावट को दूर करने के लिए है।

rv.removeItemDecoration(yourDecorator)

मुझे पता है अगर यह काम करता है!

+0

यह मेरी समस्या हल हो गया, धन्यवाद ^^ – devchimp

0

मैं एक ही मुद्दे का सामना करना पड़ रहा था, सबसे अच्छा समाधान अपने मद सजावट बनाने के लिए है, जबकि आपके विचार धारक में अपने लेआउट प्रबंधक आरंभ कर रहे हैं, इस समस्या को आप उदाहरण

public class GridViewHolder extends ViewHolder { 

    RecyclerView grid_list; 

    public GridViewHolder(View v) { 
     super(v); 
     this.grid_list = (RecyclerView) v.findViewById(R.id.home_screen_item_grid_recycler_view); 
     if (grid_list.getLayoutManager() == null) { 
      RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 4); 
      grid_list.setLayoutManager(mLayoutManager); 
      grid_list.addItemDecoration(new GridSpacingItemDecoration(4, 3, false)); 
     } 
    } 
} 
1

सामना सामना कर रहे हैं निराकरण एक ही मुद्दे और onPreExecute() विधि में

mLayoutManager = new LinearLayoutManager(current_activity); 
recyclerView.setLayoutManager(mLayoutManager); 

रखकर यह संकल्प लिया।

बस इसे आजमाइए अगर यह काम करता है

0

मैं एक ही मुद्दे का सामना। अगर

if (recyclerView.getItemDecorationAt(0) == null) { // check decoration here 
     FlexboxItemDecoration agentDividerItemDecoration = new FlexboxItemDecoration(rv.getContext()); 
     agentDividerItemDecoration.setDrawable(recyclerView.getContext().getResources().getDrawable(R.drawable.shape_divider_flex_normal)); 
     recyclerView.addItemDecoration(agentDividerItemDecoration); 
    } else { 
     Log.i(TAG, "initTagsView: ItemDecoration not null"); 
    } 
संबंधित मुद्दे