2013-04-11 71 views
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); 
    } 

} 

回答

0

而Android是不是最好的,在你的情況下,滾動視圖滾動的看法,像片段。 您必須嘗試避免這種情況,請嘗試修改佈局以避免這些衝突。

a **方法中的一個痛處是會攔截父級的觸摸事件(也添加一個手勢偵聽器),並在用戶在listfragment上進行滾動時將事件傳遞給listfragment(您必須知道座標屏幕內部的碎片等)。聽起來好像很容易,之後會引發很多問題,所以我建議改爲改寫你的佈局。