如何使我的视图的一部分与ListView一起滚动?

时间:2015-02-23 13:07:07

标签: java android android-layout android-listview

我分别有两个微调器,它帮助我选择ListView生成的选项和标准,即我需要选择一个总线和一个停止来显示适合查询的时间表(ListView)。现在直接问题了:我在ScrollView和ListView本身有两个微调器。我需要确保,当设备水平翻转时,它会平滑滚动。我所能做的就是在固定高度滚动ListView,同时它可以填充并覆盖它的整个高度,但它会缩小......有什么想法吗?

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

PublicTransport 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

1 个答案:

答案 0 :(得分:5)

请勿在{{1​​}}内使用ListView ...而是使用ScrollView并在ListView的顶部添加标题视图... http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)

编辑:

Here是我刚刚创建的示例。我希望它有所帮助...