2011-10-05 13 views
11

मेरे पास एक लेआउट है। इस लेआउट में सूची सूची में wrap_content डेटा की ऊंचाई के साथ 3 सूची दृश्य शामिल हैं ठीक नहीं हैं। जब लिस्टव्यू के पास उस समय एक बड़ा डेटा होता है तो डेटा नीचे जाता है और डेटा देखने में सक्षम नहीं होता है, इसलिए मैं दृश्य को स्क्रॉल करना चाहता हूं, सभी तीन सूचीदृश्यों के साथ यह कैसे संभव है।लेआउट को स्क्रॉल कैसे करें जिसमें 3 सूची दृश्य

किसी को भी इस बारे में कोई विचार है?

यह मेरे विचार जो 3 सूची दृश्य होते हैं, अब यह कम डेटा के साथ है, लेकिन जब डेटा पिछले सूची दृश्य उस समय बहुत बड़ा कर देगा देखने पर एक समस्या है है। मैं ग्रे रंग का दृश्य स्क्रॉल करने के लिए चाहते हैं ...

enter image description here

उत्तर

7

उपयोग रैखिक लेआउट के बजाय अपने xml फ़ाइल में सूचीदृश्य। यह आपकी एक्सएमएल फ़ाइल है।

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView android:id="@+id/scr" android:layout_height="fill_parent" 
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout android:layout_width="fill_parent" 
     android:id="@+id/r2" android:orientation="vertical" 
     android:layout_height="fill_parent" android:paddingTop="100dip" 
     android:paddingBottom="100dip"> 
     <LinearLayout android:layout_width="fill_parent" 
      android:id="@+id/l1" android:orientation="vertical" 
      android:layout_height="wrap_content" android:layout_marginTop="10dip" 
      android:background="#00B2EE"> 
     </LinearLayout> 
     <LinearLayout android:layout_width="fill_parent" 
      android:id="@+id/l2" android:orientation="vertical" 
      android:layout_height="wrap_content" android:layout_marginTop="10dip" 
      android:background="#00EE76"> 

     </LinearLayout> 
     <LinearLayout android:layout_width="fill_parent" 
      android:id="@+id/l3" android:orientation="vertical" 
      android:layout_height="wrap_content" android:layout_marginTop="10dip" 
      android:background="#7171C6"> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

और एक और एक्सएमएल डालें जो सूची द्वारा फुलाया जाएगा।

यह आपकी मुख्य गतिविधि कक्षा है।

package com.list.add; 

import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 
import android.widget.TextView; 

public class NewlistActivity extends Activity { 
    /** Called when the activity is first created. */ 
    LinearLayout l1,l2,l3; 
    ScrollView scrollView; 
    ViewGroup contentView; 
    List<String> list = new ArrayList<String>(); 
    List<String> list1 = new ArrayList<String>(); 
    List<String> list2 = new ArrayList<String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     l1 = (LinearLayout) findViewById(R.id.l1); 
     l2 = (LinearLayout) findViewById(R.id.l2); 
     l3 = (LinearLayout) findViewById(R.id.l3); 
     scrollView = (ScrollView) findViewById(R.id.scr); 
     contentView = (ViewGroup) findViewById(R.id.r2); 
     scrollView.setOnTouchListener(new ScrollPager(scrollView, contentView)); 
     scrollView.post(new Runnable() { 
      public void run() { 
       scrollView.scrollTo(0, contentView.getPaddingTop()); 
      } 
     }); 
     list.add("Parth"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Parth"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Shah"); 
     list.add("Parth"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Parth"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Chirag"); 
     list.add("Shah"); 

     list1.add("Parth"); 
     list1.add("Parth"); 
     list1.add("Parth"); 
     list1.add("Parth"); 
     list1.add("Parth"); 
     list1.add("Parth"); 

     list2.add("Kalpesh"); 
     list2.add("Kalpesh"); 
     list2.add("Kalpesh"); 
     list2.add("Kalpesh"); 
     list2.add("Kalpesh"); 
     list2.add("Kalpesh"); 
     list2.add("Kalpesh"); 
     System.out.println(list); 
     System.out.println(list1); 
     System.out.println(list2); 
     for (int i=0; i<list.size(); i++) 
     { 
     LayoutInflater inflater = getLayoutInflater(); 
     View vi = inflater.inflate(R.layout.raw, null); 
     TextView tv = (TextView) vi.findViewById(R.id.textView1); 
     tv.setText(list.get(i)); 
     l1.addView(vi); 
     } 
     for (int i=0; i<list1.size(); i++) 
     { 
     LayoutInflater inflater = getLayoutInflater(); 
     View vi = inflater.inflate(R.layout.raw, null); 
     TextView tv = (TextView) vi.findViewById(R.id.textView1); 
     tv.setText(list1.get(i)); 
     l2.addView(vi); 
     } 
     for (int i=0; i<list2.size(); i++) 
     { 
     LayoutInflater inflater = getLayoutInflater(); 
     View vi = inflater.inflate(R.layout.raw, null); 
     TextView tv = (TextView) vi.findViewById(R.id.textView1); 
     tv.setText(list2.get(i)); 
     l3.addView(vi); 
     } 
    } 

} 

और बनाने के एक इस तरह स्क्रोलर वर्ग:

public class ScrollPager implements OnTouchListener 
public ScrollPager(ScrollView aScrollView, ViewGroup aContentView) 
    { 
     mScrollView = aScrollView; 
     mContentView = aContentView; 
     scroller = new Scroller(mScrollView.getContext(), new OvershootInterpolator()); 
     task = new Runnable() 
     { 
      public void run() 
      { 
       scroller.computeScrollOffset(); 
       mScrollView.scrollTo(0, scroller.getCurrY()); 

       if (!scroller.isFinished()) 
       { 
        mScrollView.post(this); 
       } 
      } 
     }; 
    } 
public boolean onTouch(View v, MotionEvent event) 
    { 
     // Stop scrolling calculation. 
     scroller.forceFinished(true); 
     // Stop scrolling animation. 
     mScrollView.removeCallbacks(task); 

     if (event.getAction() == MotionEvent.ACTION_UP) 
     { 
+0

यह मेरे लिए काम करता है। मैं समझता हूं कि यह अधिक स्मृति कुशल नहीं हो सकता है क्योंकि हमें इसे स्पष्ट रूप से करना होगा, लेकिन मेरे मामले में मैं 5 से अधिक वस्तुओं का उपयोग नहीं कर रहा हूं इसलिए मैं अच्छा हूं। धन्यवाद – lazyguy

1

आप वजन का उपयोग करना चाहिए एक्सएमएल लेआउट में प्रत्येक ListView में android:layout_weight="1" जोड़कर देखते हैं। ताकि यह प्रत्येक सूचीदृश्य के लिए स्क्रीन को समान स्थान के साथ विभाजित कर दे और आपकी स्क्रॉलिंग प्रत्येक ListView के लिए काम करेगी।

+0

हैलो सुजीत लेकिन मुझे लगता है कि एंड्रॉयड में सापेक्ष लेआउट का उपयोग करें: layout_weight संपत्ति का समर्थन नहीं कर रहा है। –

+0

आप रिलेवेटिवआउट का उपयोग क्यों कर रहे हैं? कोई विशिष्ट कारण? आपको LinearLayout का उपयोग करना चाहिए क्योंकि आपके पास 3 सूची लंबवत गठबंधन है ??? – Sujit

+0

लेकिन इस दृश्य के साथ एप्लिकेशन मामले का डिज़ाइन इसलिए मुझे इस डिज़ाइन को सापेक्ष लेआउट में चाहिए .. –

1

आप एक खड़ी या क्षैतिज स्टैक में सामान प्रस्तुत कर रहे हैं, तो आप LinearLayout का उपयोग कर किया जाना चाहिए और फिर layout_weight विशेषता का उपयोग कंटेनर के भीतर अलग-अलग पंक्तियों/स्तंभों के अनुपात को नियंत्रित करने के लिए।

यदि आप स्क्रीन आकार चाहते हैं, तो layout_width और layout_height से fill_parent पर सेट करें अन्यथा आपको सभी उपलब्ध स्क्रीन आयाम नहीं मिलेगा। यदि आप ऊंचाई के लिए wrap_content का उपयोग करने का प्रयास करते हैं, तो सब कुछ पतन हो जाएगा, जब तक आप अतिरिक्त लेआउट बाधाओं का सहारा लेते हैं, उदा। minHeight

हम इसे हर जगह उपयोग करते हैं और यह काफी विश्वसनीय है। तीन वस्तुओं के लिए, आप 1/1/1 या 3/3/3 का उपयोग कर सकते हैं।

वजन भी बराबर होना नहीं है! आप जिस तरह से चाहें अनुपात को विभाजित कर सकते हैं; वजन पूरे काल (चौड़ाई/ऊंचाई) के सापेक्ष अनुपात हैं। जैसे यदि आप मध्य तत्व को दो बार आकार चाहते हैं, तो 1/2/1 का उपयोग करें; यदि आप चाहते हैं कि यह 40% 30/40/30 या 3/4/3 का उपयोग करें।

एक अच्छी "चाल" layout_weight = 1 का उपयोग बिल्कुल एक पंक्ति/कॉलम (अन्य डिफ़ॉल्ट रूप से शून्य पर) का उपयोग करना है, और यह किसी भी शेष स्थान को "भरने" में रखेगी। यह एक आम लेआउट परिदृश्य है।

आप ढेर की जरूरत है स्क्रॉल हो सकता है, आप इसे एक ScrollView में डाल सकते हैं। इस मामले में, आपको LinearLayout से wrap_content पर सेट करना होगा, और लेआउट सिस्टम की इच्छा के आधार पर आप विषय गिरने होंगे (जिसका अर्थ है कि आपको न्यूनतम/अधिकतम बाधाओं का उपयोग करने की आवश्यकता है)।

1

इसके बजाय, मुझे कार्यान्वयन का एक ही प्रकार के लिए मेरे सबसे अच्छे विचार दे सकते हैं।

क्या आपने कभी भी उसी तरह के कार्यान्वयन के लिए ExpandableListView का उपयोग करने का विचार किया है? बस विस्तार के रूप में पहला समूह आइटम दिखाएं और अन्य 2 समूहों को ध्वस्त कर दें, जब उपयोगकर्ता किसी विशेष समूह पर क्लिक करते हैं तो इसे विस्तृत करें।

यहाँ उसी के लिए कुछ उदाहरण हैं:

  1. http://techdroid.kbeanie.com/2010/09/expandablelistview-on-android.html
  2. http://coderzheaven.com/2011/04/expandable-listview-in-android-using-simpleexpandablelistadapter-a-simple-example/

मुझे यकीन है कि इस विचार अच्छा नहीं है हूँ, लेकिन अगर आप ऐसा करते हैं तो लागू क्या आप वाकई में लाभकारी पाया जाएगा अपने मामला।

1

इस विधि का प्रयोग करें और अपने सूचीदृश्य scrollview के अंदर स्क्रॉल बन: -

ListView lstNewsOffer.setAdapter(new ViewOfferAdapter(
         ViewNewsDetail.this, viewOfferList)); 
       getListViewSize(lstNewsOffer); 

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

}

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