2013-02-20 77 views
3

請幫助我在垂直放置在線性佈局中的兩個列表視圖的佈局。問題是,如果list1的數據多於list2的數據不可見且不可滾動,則使用scollview不是解決方案。線性佈局中的兩個ListViews

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/backrepeat" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/shape_calendar_top" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/prevMonth" 
      android:layout_width="50dp" 
      android:layout_height="55dp" 
      android:src="@drawable/button_events_previous" > 
     </ImageView> 

     <TextView 
      android:id="@+id/currentMonth" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="0.6" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="Janauary" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#d458b1" 
      android:textStyle="bold" > 
     </TextView> 

     <ImageView 
      android:id="@+id/nextMonth" 
      android:layout_width="50dp" 
      android:layout_height="55dp" 
      android:src="@drawable/button_events_next" /> 
    </LinearLayout> 


     <TextView 
      android:id="@+id/direction_label" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginTop="10dp" 
      android:background="#DA81F5" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="PRESS RELEASES" 
      android:textColor="#FFFFFF" 
      android:textSize="13dip" /> 

     <ListView 
      android:id="@+id/eventsList" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/direction_label" 
      android:background="#ffffff" > 
     </ListView> 


     <TextView 
      android:id="@+id/direction_label1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="#DA81F5" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="SPEECHES" 
      android:textColor="#FFFFFF" 
      android:textSize="13dip" /> 

     <ListView 
      android:id="@+id/eventsList1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/direction_label1" 
      android:background="#ffffff" > 
     </ListView> 


    <TextView 
     android:id="@+id/empty_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

如果我使用scrollview,那麼它僅包含第一行的listview。

請提出建議。

編輯:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/backrepeat" 
    android:orientation="vertical" > 



    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/shape_calendar_top" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/prevMonth" 
      android:layout_width="50dp" 
      android:layout_height="55dp" 
      android:src="@drawable/button_events_previous" > 
     </ImageView> 

     <TextView 
      android:id="@+id/currentMonth" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="0.6" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="Janauary" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#d458b1" 
      android:textStyle="bold" > 
     </TextView> 

     <ImageView 
      android:id="@+id/nextMonth" 
      android:layout_width="50dp" 
      android:layout_height="55dp" 
      android:src="@drawable/button_events_next" /> 
    </LinearLayout> 


     <TextView 
      android:id="@+id/direction_label" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginTop="10dp" 
      android:background="#DA81F5" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="PRESS RELEASES" 
      android:textColor="#FFFFFF" 
      android:textSize="13dip" /> 

     <ListView 
      android:id="@+id/eventsList" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:layout_below="@+id/direction_label" 
      android:background="#ffffff" > 
     </ListView> 

     <TextView 
      android:id="@+id/direction_label1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="#DA81F5" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="SPEECHES" 
      android:textColor="#FFFFFF" 
      android:textSize="13dip" /> 

     <ListView 
      android:id="@+id/eventsList1" 
      android:layout_width="fill_parent" 
       android:layout_height="0dp" 
      android:layout_weight="1" 
      android:layout_below="@+id/direction_label1" 
      android:background="#ffffff" > 
     </ListView> 

    <TextView 
     android:id="@+id/empty_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

如果我用這個,什麼也看不見了:(

ASMI

編輯:CODE爲兩名列表視圖:

  { 
       arrayofWebData.add(cn); 
       listAdapter = new SelectArralAdapter(getActivity(), 
         arrayofWebData); 
       List_events.setAdapter(listAdapter); 
      } 

對於第二ListView如下:

if ((year == event_year) && (event_month == month)) 

      { 
      arrayofWebDataPress.add(cn); 
      listAdapter_press = new SelectArralAdapter_Press(getActivity(),arrayofWebDataPress); 
       List_events_press.setAdapter(listAdapter_press); 
       i++; 

      } 

兩個列表視圖數組適配器:

 class SelectArralAdapter_Press extends ArrayAdapter<PressDB> { 

    private LayoutInflater inflater; 

    public SelectArralAdapter_Press(Context context, 
      ArrayList<PressDB> arrayofWebDataPress) { 

     super(context, R.layout.speech_list_item, R.id.event_title, 
       arrayofWebDataPress); 
     inflater = LayoutInflater.from(context); 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder_Press holder; 
     if (convertView == null) { 

      convertView = inflater.inflate(R.layout.speech_list_item, null); 

      holder = new ViewHolder_Press(convertView); 
      convertView.setTag(holder); 

     } else { 

      holder = (ViewHolder_Press) convertView.getTag(); 
     } 
     holder.populateFrom(arrayofWebDataPress.get(position)); 

     return (convertView); 
    } 

} 

    class ViewHolder_Press{ 
    public TextView event_name = null; 
    public TextView event_date = null; 

    public ViewHolder_Press(View row) { 
     event_name = (TextView) row.findViewById(R.id.event_title); 
     event_date = (TextView) row.findViewById(R.id.event_date_time); 

    } 

    void populateFrom(PressDB eventsMainDB) { 
     event_name.setText(eventsMainDB.press_name); 
     event_date.setText(eventsMainDB.press_date + " "); 
    } 
} 

對於第二Adaper:

 class SelectArralAdapter extends ArrayAdapter<SpeechDB> { 

    private LayoutInflater inflater; 

    public SelectArralAdapter(Context context, 
      ArrayList<SpeechDB> arrayofWebData) { 

     super(context, R.layout.speech_list_item, R.id.event_title, 
       arrayofWebData); 
     inflater = LayoutInflater.from(context); 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder; 
     if (convertView == null) { 

      convertView = inflater.inflate(R.layout.speech_list_item, null); 

      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 

     } else { 

      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.populateFrom(arrayofWebData.get(position)); 

     return (convertView); 
    } 

} 

class ViewHolder { 
    public TextView event_name = null; 
    public TextView event_date = null; 

    public ViewHolder(View row) { 

     event_name = (TextView) row.findViewById(R.id.event_title); 

     event_date = (TextView) row.findViewById(R.id.event_date_time); 

    } 

    void populateFrom(SpeechDB eventsMainDB) { 
     event_name.setText(eventsMainDB.speech_name); 

     event_date.setText(eventsMainDB.speech_date + " "); 


    } 

} 
+0

類似的問題在這裏http://stackoverflow.com/questions/14952155/relativelayout-content-can-not-be-seen/14952256#14952256 – 2013-02-20 04:27:43

+0

哪種解決方案,你建議我試着用相對佈局,它不工作 – Asmi 2013-02-20 04:38:38

+0

你可以嘗試在兩個'ListView'上放置'layout_weight = 1'屬性,並且兩者都會採用相同的比例。請記得將'layout_height'改爲'0'而不是'wrap_content'。 – Aprian 2013-02-20 04:41:19

回答

0

你可以採取這樣的事情。

<LinearLayout> 
    <ScrollView> 
    <LinearLayout> 
     {Your All code of controls} 
    </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

或UO可以採取<ScrollView>作爲父標籤..
你可以做這樣的事情。按我的知識......

0

我已經使用,如:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFF"> 

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

// first linearlayout which is holding one textview and a listview 
<LinearLayout 
    ">  
    <TextView 
     />   
    <ListView 
     android:id="@+id/failed_list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="5dp"/> 
</LinearLayout> 

// second linearlayout which is holding one textview and a listview 
<LinearLayout 
    ">  
    <TextView 
     />   
    <ListView 
     android:id="@+id/failed_list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="5dp"/> 
</LinearLayout> 

// third linearlayout which is holding one textview and a listview 
<LinearLayout 
    ">  
    <TextView 
     />   
    <ListView 
     android:id="@+id/failed_list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="5dp"/> 
</LinearLayout> 

// and finally i have added a button in the parent linearlayout 
<Button 
    /> 

</LinearLayout> 
</ScrollView> 

你可以試試這個。

+0

我添加了Java代碼 – Asmi 2013-02-20 05:25:53

2

試試這個。

如果你想要去的XML設計,那麼你必須給固定大小,無論是通過使用重量(這是最優先)或在DP給予修復大小。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/backrepeat" 
    android:weightSum="1" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/shape_calendar_top" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/prevMonth" 
      android:layout_width="50dp" 
      android:layout_height="55dp" 
      android:src="@drawable/button_events_previous" > 
     </ImageView> 

     <TextView 
      android:id="@+id/currentMonth" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="0.6" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="Janauary" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#d458b1" 
      android:textStyle="bold" > 
     </TextView> 

     <ImageView 
      android:id="@+id/nextMonth" 
      android:layout_width="50dp" 
      android:layout_height="55dp" 
      android:src="@drawable/button_events_next" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/direction_label" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginTop="10dp" 
     android:background="#DA81F5" 
     android:paddingLeft="5dp" 
     android:paddingTop="5dp" 
     android:text="PRESS RELEASES" 
     android:textColor="#FFFFFF" 
     android:textSize="13dip" /> 

    <ListView 
     android:id="@+id/eventsList" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:layout_below="@+id/direction_label" 
     android:background="#ffffff" > 
    </ListView> 

    <TextView 
     android:id="@+id/direction_label1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:background="#DA81F5" 
     android:paddingLeft="5dp" 
     android:paddingTop="5dp" 
     android:text="SPEECHES" 
     android:textColor="#FFFFFF" 
     android:textSize="13dip" /> 

    <ListView 
     android:id="@+id/eventsList1" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:layout_below="@+id/direction_label1" 
     android:background="#ffffff" > 
    </ListView> 

    <TextView 
     android:id="@+id/empty_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

我得到這個屏幕在設計時。

enter image description here

+0

如果我使用這個,沒有任何顯示:( – Asmi 2013-02-20 05:09:43

+0

現在在Listview中沒有顯示任何值,它們是空的 – Asmi 2013-02-20 05:12:08

+0

你的意思是當你運行這個代碼,那麼你正面臨着這個問題 – 2013-02-20 05:13:05

0

試用如下:

這裏是相同的佈局文件中的溶液,以一個以上的ListView。您可以通過爲LinearLayout內的每個佈局提供相同的權重,將佈局文件設置爲以下佈局。

<LinearLayout android:layout_weight="1" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"> 

       <ListView android:id="@+id/list1" 
          android:layout_height="fill_parent" 
          android:layout_width="fill_parent"> 

       </ListView> 
    </LinearLayout> 

<LinearLayout android:layout_weight="1" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent"> 

      <ListView android:id="@+id/list2" 
         android:layout_height="fill_parent" 
         android:layout_width="fill_parent"> 

      </ListView> 
</LinearLayout> 

Java代碼的

public class MainActivity extends Activity { 
private ListView lv1 = null; 
private ListView lv2 = null; 
private String s1[] = {"a", "b", "c", "d", "e", "f"}; 
private String s2[] = {"r", "s", "t", "u", "v", "w", "x"}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 
    lv1 = (ListView) findViewById (R.id.list1); 
    lv2 = (ListView) findViewById (R.id.list2); 

    lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1)); 
    lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2)); 

} 
} 

欲瞭解更多詳細信息,您可以參考已經給出了相同的解決方案Link

我希望它能幫助你。

謝謝。

+0

顯示我將兩個相對佈局合併爲一個東西 – Asmi 2013-02-20 05:17:58

+0

查看我的更新答案 – GrIsHu 2013-02-20 06:02:42

+0

這使得屏幕分爲兩部分,一個用於1個listview,另一個用於其他one1,每個部分H大大自己的listview滾動,我可以有像整體佈局滾動而不是單獨滾動的列表視圖。因此,一旦listview1完成Listview2啓動 – Asmi 2013-02-20 06:18:03

0

我解決了這個問題,使用節標題並將兩個listviews結合到一個listview中,並通過Section標題的幫助對它們進行分類。