2015-05-02 4 views
28

मुझे प्रत्येक पंक्ति में तत्वों को केंद्रित करने की आवश्यकता है ताकि वे इस नकली में पसंद करेंगे। मैं खोज रहा हूं कि कोई लेआउट है जो बिना किसी तरीके से काम करता है। आइटम अपनी पंक्तियों में केंद्रित हैं।पुनर्नवीनीकरण के आइटम कैसे केंद्रित करें?

mockup

यह कैसे यह मेरे कोड में अब दिखता है। enter image description here

+0

यह http://stackoverflow.com/questions/34206078/center-a-cardview-in-a-recyclerview-with-only-one-element –

+0

के समान ही लगता है क्या यह समस्या हल हो गई है? –

+0

आपको केवल कस्टम लेआउट बनाने की आवश्यकता हो सकती है क्योंकि GridLayoutManager इससे मदद नहीं करता है। मैंने एक उत्तर पोस्ट किया लेकिन मुझे यकीन नहीं है कि यह वही है जो आप खोज रहे हैं। – Mo1989

उत्तर

22

के लिए चौड़ाई निर्दिष्ट करने के लिए है आइटम पंक्ति लेआउट android:layout_gravity="center"LinearLayout पर दें।

छवियों की प्रत्येक पंक्ति के लिए आपको अलग-अलग LinearLayout लेना होगा।

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/image1" 
       android:layout_width="64dp" 
       android:layout_height="64dp" 
       android:layout_marginRight="10dp" 
       android:layout_weight="1" 
       android:src="@drawable/image1" /> 

      <ImageView 
       android:id="@+id/image2" 
       android:layout_width="64dp" 
       android:layout_height="64dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_weight="1" 
       android:src="@drawable/image2" /> 

      <ImageView 
       android:id="@+id/image3" 
       android:layout_width="64dp" 
       android:layout_height="64dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_weight="1" 
       android:src="@drawable/image3" /> 

    </LinearLayout> 
+0

मुझे match_parent के साथ रीसाइक्लर व्यू लेआउट_विड्थ = "wrap_content" को संशोधित करना था, साथ ही – jernkuan

+0

@jemkuan यह उपयोगकर्ता स्क्रॉलिंग को अक्षम कर देगा जब उपयोगकर्ता माता-पिता के किनारे या बाहरी क्षेत्र पर स्वाइप करता है, क्योंकि RecyclerView की चौड़ाई चौड़ाई से कम होगी माता-पिता का –

7

मुझे लगता है कि आप LinearLayoutManagerRecyclerView के साथ ListView-शैली प्रभाव के लिए उपयोग कर रहे हैं। उस स्थिति में, प्रत्येक सामग्री के लिए android:gravity="center" के साथ horizontalLinearLayout का उपयोग करें, इसकी सामग्री के केंद्र में android:gravity="center" के साथ।

+0

असल में मैं ग्रिडलेआउट का उपयोग कर रहा हूं लेकिन मुझे इसे आज़माएं। –

+2

@cgomezmendez: मुझे लगता है कि 'GridLayoutManager' के माध्यम से ऐसा करने का कोई तरीका होगा, या फिर कस्टम' RecyclerView.LayoutManager' के माध्यम से। मैं नहीं देखता, एक त्वरित नज़र में, इसे 'GridLayoutManager' के साथ कैसे करें। – CommonsWare

+0

क्या आपने समस्या तय की है? –

-1

अपने पुनर्नवीनीकरण को xml में linearlayout में डालें। और

android:layout_gravity="center"

और recyclerview की चौड़ाई (दोनों recyclerview और LinearLayout के लिए) का उपयोग wrap_content जानी चाहिए, जबकि LinearLayout math_parent होना चाहिए और आप अपने RecyclerView में LinearLayout ले लो अपनी row_item

+0

इससे उसकी मदद नहीं होगी। केंद्रित तत्वों को केंद्रित करने वाले बच्चे तत्व, ग्रिडलेआउट के अंदर हैं। –

3

आप कुछ लेआउट गतिशील रूप में जोड़ सकते हैं, उदाहरण के लिए:

  • एडाप्टर वस्तु एक क्षैतिज LinearLayout

1- एक LinearLayout बनाने हो सकता है

यहाँ उदाहरण कोड है जावा कोड में और इसे कस्टमाइज़ करें (गुरुत्वाकर्षण, ...)

2- LinearLayout

3- करने के लिए कुछ प्रतीक जोड़ने के अपने एडाप्टर के लिए LinearLayout जोड़ने

4- दोहराने 1,2,3


// : declare new horizontal linearLayout 
    ImageView myIcons[nomOfIcons]; 
    // : add all icons to myIcons 
    for(int i=1; i<=nomOfIcons; i++){ 
     linearLayout.addView(myIcons[i - 1]); 
     if(i%numOfIconsInOneHorizontalLinearLayout==0) { 
      results.add(linearLayout); // add linearLayout to adapter dataSet 
      // : declare new horizontal linearLayout 
     } 
    } 
    if(n%numOfIconsInOneHorizontalLinearLayout!=0) // add last linearLayout if not added in the loop 
     results.add(linearLayout); 
    mAdapter.notifyDataSetChanged(); // update adapter 

0

उपयोग gridLayoutManager = new GridLayoutManager(context,6) और setSpanSizeLookup ओवरराइड ।

उदाहरण:

int totalSize=list.size(); 
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
        @Override 
        public int getSpanSize(int position) { 
         int span; 
         span = totalSize % 3; 
         if (totalSize < 3) { 
          return 6; 
         } else if (span == 0 || (position <= ((totalSize - 1) - span))) { 
          return 2; 
         } else if (span == 1) { 
          return 6; 
         } else { 
          return 3; 
         } 
        } 
       }); 

यदि आप एक पंक्ति में 3 आइटम प्रदर्शित करना चाहते हैं और ग्रिड के केंद्र में शेष काम करेंगे।

अपनी आवश्यकता के अनुसार ग्रिड लेआउट प्रबंधक अवधि गणना करें।

15

मेक recyclerview widthwrap_content और इसके कंटेनर की layout_gravitycenter_horizontal

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycrer_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" /> 
</LinearLayout> 
1

आप गूगल FlexLayoutManager कहीं भी होगी से नए layoutManager उपयोग कर सकते हैं करने के लिए आप recyclerView आइटम पर नियंत्रण और लचीलेपन का एक बहुत कुछ देता है

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