简体   繁体   English

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

[英]How can I make one part of my view scroll together with ListView?

I have two spinners separately, which help me selecting the options and criterias for the ListView to generate, ie I need to select a bus and a stop to show the timetable(ListView) which fit the query. 我分别有两个微调器,这可以帮助我选择要生成ListView的选项和条件,即,我需要选择一辆巴士和一个车站来显示适合查询的时间表(ListView)。 Now straight to the problem: I have two spinners in ScrollView and ListView itself. 现在直接解决问题:ScrollView和ListView本身有两个微调器。 I need to ensure, that it would scroll smoothly when the device is flipped horizontally. 我需要确保在水平翻转设备时它可以平滑滚动。 All I can is to scroll ListView in fixed height, while it could fill and cover it's whole height, but it's shrinked... Any ideas? 我所能做的就是以固定的高度滚动ListView,虽然它可以填充和覆盖整个高度,但是缩小了……有什么想法吗?

PublicTransport java code: 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 code: 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>

在此处输入图片说明

Don't use ListView inside ScrollView ...instead, just use ListView and add header view at the top of ListView ... http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View) 不要在ScrollView使用ListView ...而是使用ListView并在ListView的顶部添加标题视图... http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android .view.View)

EDIT: 编辑:

Here is the example i just created. 是我刚刚创建的示例。 I hope it helps... 希望对您有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM