2015-02-23 48 views
2

我有兩個單獨的spinners,它們幫助我選擇ListView生成的選項和標準,即我需要選擇一個總線和一個stop來顯示適合查詢的時間表(ListView)。現在直接解決問題:我有兩個ScrollView和ListView本身的spinners。我需要確保,當設備水平翻轉時,它會平滑滾動。我所能做的就是在固定高度上滾動ListView,同時它可以填充並覆蓋整個高度,但它會縮小...任何想法?如何使我的視圖的一部分與ListView一起滾動?

公共交通java代碼:

public class PublicTransport extends Fragment { 

    private View rootView; 
    private Spinner routes_spinner, stops_spinner; 
    private Button submit; 
    private String spinner_stop_name; 
    private int bus_id; 
    private String table_names[] = { "Bus_2", "Bus_2B", "Bus_3", "Bus_4", "Bus_6", "Bus_6B", "Bus_9", "Bus_10", "Bus_11", "Bus_16", "Bus_16B" }; 
    private List<PublicTransportItem> items = new ArrayList<PublicTransportItem>(); 
    private List<String> uniqueStops = new ArrayList<String>(); 
    private CustomPublicTransportListAdapter adapter; 
    private ListView listView; 
    private static Locale myLocale; 
    private Context context; 
    private EasyTracker easyTracker = null; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.publictransport, container, false); 
     context = getActivity().getApplicationContext(); 
     easyTracker = EasyTracker.getInstance(context); 
     setLanguage(); 
     routes_spinner = (Spinner) rootView.findViewById(R.id.routes_spinner); 
     ArrayAdapter<CharSequence> routes_adapter = ArrayAdapter.createFromResource(getActivity(), 
       R.array.routes_array, android.R.layout.simple_spinner_item); 
     routes_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     routes_spinner.setAdapter(routes_adapter); 
     stops_spinner = (Spinner) rootView.findViewById(R.id.stops_spinner); 
     routes_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
       bus_id = i; 
       easyTracker.send(MapBuilder.createEvent("Public_transport", 
         "Rout", String.valueOf(i), null).build());//Routes i + 1 
       PublicTransportDatabaseHandler ptdb = new PublicTransportDatabaseHandler(context); 
       uniqueStops = ptdb.getAllStops(table_names[bus_id]); 
       ArrayAdapter<String> stops_adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, 
         uniqueStops); 
       stops_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       stops_spinner.setAdapter(stops_adapter); 
       items = ptdb.getAllPublicTransportItems(table_names[bus_id], spinner_stop_name); 
       adapter = new CustomPublicTransportListAdapter (context, items, bus_id); 
       listView = (ListView) rootView.findViewById(R.id.resultsList); 
       listView.setAdapter(adapter); 
       listView.setOnTouchListener(new View.OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         int action = event.getAction(); 
         switch (action) { 
          case MotionEvent.ACTION_DOWN: 
           v.getParent().requestDisallowInterceptTouchEvent(true); 
           break; 

          case MotionEvent.ACTION_UP: 
           v.getParent().requestDisallowInterceptTouchEvent(false); 
           break; 
         } 

         v.onTouchEvent(event); 
         return true; 
        } 
       }); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> adapterView) { 

      } 
     }); 
     stops_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
       easyTracker.send(MapBuilder.createEvent("Public_transport", 
         "Stop", String.valueOf(i), null).build());//Stops i + 1 
       spinner_stop_name = stops_spinner.getItemAtPosition(i).toString(); 
       PublicTransportDatabaseHandler ptdb = new PublicTransportDatabaseHandler(context); 
       items = ptdb.getAllPublicTransportItems(table_names[bus_id], spinner_stop_name); 
       adapter = new CustomPublicTransportListAdapter (context, items, bus_id); 
       listView = (ListView) rootView.findViewById(R.id.resultsList); 
       listView.setAdapter(adapter); 
       listView.setOnTouchListener(new View.OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         int action = event.getAction(); 
         switch (action) { 
          case MotionEvent.ACTION_DOWN: 
           v.getParent().requestDisallowInterceptTouchEvent(true); 
           break; 

          case MotionEvent.ACTION_UP: 
           v.getParent().requestDisallowInterceptTouchEvent(false); 
           break; 
         } 

         v.onTouchEvent(event); 
         return true; 
        } 
       }); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> adapterView) { 

      } 
     }); 

     return rootView; 
    } 
    public static final PublicTransport newInstance(){ 
     PublicTransport publicTransport = new PublicTransport(); 
     Bundle bdl = new Bundle(2); 
     publicTransport.setArguments(bdl); 
     return publicTransport; 
    } 
    private void setLanguage() { 
     SharedPreferences sp = this.getActivity().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); 
     String lang = sp.getString("languages", "lt"); 
     myLocale = new Locale(lang); 
     Resources res = getResources(); 
     DisplayMetrics dm = res.getDisplayMetrics(); 
     Configuration conf = res.getConfiguration(); 
     conf.locale = myLocale; 
     res.updateConfiguration(conf, dm); 
    } 
} 

公共交通XML代碼:

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

     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:orientation="horizontal" 
      android:gravity="center_vertical" 
      android:weightSum="1" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/blue_rectangle"> 

      <TextView 
       android:text="@string/bus_route" 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="0.4" 
       android:textSize="18sp" 
       android:fontFamily="sans-serif-condensed" 
       android:textColor="#ffffff" /> 

      <Spinner 
       android:id="@+id/routes_spinner" 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="0.6" 
       android:entries="@array/routes_array" 
       android:prompt="@string/route" /> 
     </LinearLayout> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#ffffff" /> 

     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:orientation="horizontal" 
      android:gravity="center_vertical" 
      android:weightSum="1" 
      android:layout_marginBottom="10dp" 
      android:background="@drawable/blue_rectangle"> 

      <TextView 
       android:text="@string/bus_stop" 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="0.4" 
       android:textSize="18sp" 
       android:fontFamily="sans-serif-condensed" 
       android:textColor="#ffffff" /> 

      <Spinner 
       android:id="@+id/stops_spinner" 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="0.6" 
       android:prompt="@string/stop" /> 

     </LinearLayout> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#ffffff" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/results" 
      android:padding="8dp" 
      android:textSize="18dp" 
      android:textStyle="normal" 
      android:textColor="@android:color/white" 
      android:fontFamily="sans-serif-condensed" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#ffffff" /> 

     <ListView 
      android:id="@+id/resultsList" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="5.0sp"> 

     </ListView> 

    </LinearLayout> 
</ScrollView> 

enter image description here

回答

5

不要使用ListViewScrollView ...相反,只是使用ListView並添加在頂部的標題視圖3210 ... http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)

編輯:

Here是我剛剛創建的例子。我希望它可以幫助...

+0

我嘗試過使用它,但應用程序帶有徒勞的屏幕(僅適用於背景)。我想知道,如果一個標題真的可以點擊...我認爲,該標題可以附加到一個生成的ListView只... – Sharptax 2015-02-23 13:21:51

+1

你是對的。我只是測試它(用我的ExpandableListView)...如果我不設置列表適配器什麼都沒有顯示...所以我設置新的ExpandableListAdapter沒有方法重寫和它的工作(並與標題視圖,按鈕點擊等進行交互。 )。所以基本上我認爲你應該添加HeaderView並設置列表適配器(例如BaseAdapter),當用戶選擇適當的過濾器,然後設置自定義適配器...我認爲它應該工作..但我沒有測試它。 PS我認爲即使列表爲空也需要設置適配器,因爲標題視圖必須被誇大並因此顯示 – 2015-02-23 13:35:09

+0

您是否可以發佈您的代碼,因爲我沒有得到主要想法:/ Soz – Sharptax 2015-02-23 13:40:33

相關問題