1

在我的应用程序的一个屏幕中,我有一个没有响应手势的 ListFragment。在其他屏幕中,我的 ListFragments 表现得恰到好处 - 根据触摸上下移动。但是这个实例不会响应手势而移动。我尝试将高度设置为特定的倾角,将 ListFragment 包含在 ScrollView 中,但没有任何效果。日志证明列表中有 3 个条目,但只有第一个和第二个的一半可见。我可以从列表中选择第一个条目。

xml如下。由“android:name="com.chex.control.LabelList"”标识的内部片段不滚动。

有没有人遇到过这个问题并解决了?

谢谢

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

        <TextView
            android:id="@+id/new_interval_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Interval" />

        <EditText
            android:id="@+id/days_in_interval"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="days in interval"
            android:inputType="number"
            android:singleLine="true" />

        <EditText
            android:id="@+id/goodi_goal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="goodi goal"
            android:inputType="number"
            android:singleLine="true" />

        <LinearLayout
            android:id="@+id/label_selector"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <LinearLayout
                android:id="@+id/default_label_pane"
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Default Labels" />

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:scrollbars="vertical" >

                    <fragment
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/default_labels_fragment"
                        android:name="com.chex.control.LabelList"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/white"
                        android:focusable="true" >
                    </fragment>
                </ScrollView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/add_remove_label_selector"
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/add_label_button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Add" />

                <Button
                    android:id="@+id/remove_label_button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Remove" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/label_pane"
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Inteval Labels" />

                <fragment
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/interval_labels_fragment"
                    android:name="com.chex.control.IntervalLabelList"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:focusable="true" >
                </fragment>

                <EditText
                    android:id="@+id/new_label"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="new label"
                    android:inputType="text"
                    android:singleLine="true" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/keep_discard_button_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/keep_interval_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Keep" />

            <Button
                android:id="@+id/discard_interval_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Discard" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

这是片段 .java

package com.chex.control;


import java.util.ArrayList;

import com.chex.R;
import com.chex.storage.Child;
import com.chex.storage.DatabaseConstants;
import com.chex.storage.DatabaseHelper;
import com.chex.storage.DatabaseIO;
import com.chex.storage.Label;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * presents all the labels on the device.
 */
public class LabelList extends ListFragment implements DatabaseConstants,
        GoodiList {

    private final String TAG = "LabelList";

    ArrayAdapter<Label> adapter = null;

    private ListParent parentActivity;

    private Label[] values;

    private Label currentItem;

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

        Log.e(TAG, "on Create");
        ArrayList<Label> labelList = Label.getLabelsForInterval();

        Log.i(TAG,labelList.size()+" labels");
        // adapter = new IntervalListAdapter(getActivity(), values);

        adapter = new IntervalListAdapter(getActivity(), labelList);

        setListAdapter(adapter);

        Log.e(TAG, "finished on Create");
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        LongClickListener longClickListener = new LongClickListener();

        getListView().setOnItemLongClickListener(longClickListener);
    }

    /**
     * create and return a CursorLoader that will take care of creating a Curso
     * for the data being displayed.
     */

    /**
     * this used to delete. However, we need it to select usually. Now delete is
     * done by long click.
     */
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        // delete child - remove child from list and database
        Object item = l.getItemAtPosition(position);

        currentItem = (Label) item;

        Log.e(TAG,
                "selected " + currentItem.getLabel() + " id: "
                        + currentItem.getId() + " at position " + position);

        parentActivity.listItemSelected(this, position, currentItem.getId());

    }

    /**
     * Forces cursor to requery database and list to be update. Used when a new
     * child is entered in parent activity's EditText field.
     */
    public void notifyDataChanged() {

        Log.e(TAG, "told adapter that data changed");

        adapter.notifyDataSetChanged();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.Fragment#onAttach(android.app.Activity)
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        parentActivity = (ListParent) activity;

        parentActivity.setList(this);

    }

    class LongClickListener implements OnItemLongClickListener {

        public boolean onItemLongClick(AdapterView<?> adapterView, View view,
                int position, long id) {
            // delete child - remove child from list and database

            // adapter view. Can always call parent's getItemAtPosition
            final Object item = adapterView.getItemAtPosition(position);

            final Label label = (Label) item;

            final String name = label.getLabel();

            Log.e(TAG, "selected " + name + " at position " + position
                    + " now we'll delete them");

            // make sure user wants to delete
            AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

            alert.setTitle("Delete " + name + "?");

            alert.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Label.deleteLabel(name);

                            adapter.remove(label);

                            // update the list
                            notifyDataChanged();

                        }
                    });

            alert.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            // nothing happens - child remains

                        }
                    });

            alert.show();

            return true;
        }

    }

    @Override
    public void addListItem(int itemId) {
        Label labelToAdd = Label.lookupLabel(itemId);

        if (labelToAdd == null) {
            Log.e(TAG, "didn't find a label for label id " + itemId);

        } else {
            Log.e(TAG, "adding label: " + labelToAdd.getLabel() + "  "
                    + labelToAdd.getId() + "  id argument: " + itemId);

            // breaks because the adapter is currently strings
            adapter.add(labelToAdd);

            notifyDataChanged();
        }
    }

    @Override
    public void removeListItem(int itemId) {
        // TODO Auto-generated method stub

    }

    @Override
    public void removeListItem() {
        // TODO Auto-generated method stub

    }


    @Override
    public void highlightListIndex(int index) {
        ListView listView = getListView();
        listView.setItemChecked(index, true);
    }

} 
4

1 回答 1

0

在滚动视图中滚动视图的android并不是最好的,就像你的情况一样。你必须尽量避免这种情况,尽量修改布局来避免这些冲突。

a** 方法的一个痛点是拦截父级的触摸事件(也添加一个手势侦听器)并在用户在 listfragment 上滚动时将事件传递给 listfragment(您必须知道内部片段的坐标屏幕等)。这听起来容易得多,之后会导致很多问题,所以我建议改写你的布局。

于 2013-04-11T11:08:40.997 回答