2016-08-02 2 views
2

Я разрабатываю приложение для Android.Java Android: onItemLongClick для listView не отвечает

Я хочу, чтобы отобразить список контактов (с использованием ListView), и я хочу добавить 2 предупреждений диалогов: один за onItemClickListener и другой путем onItemLongClickListener.

Но onItemLongClickListener никогда не называют, только onItemClickListener. я уже сделал это для другого приложения, но здесь это не работает, и я не считаю, почему:/

Мой код:

public class ContactActivity extends AppCompatActivity { 
    private ListView listView; 
    private ContactAdapter adapter; //my adapter 
    private ArrayList<Contact> contacts; //my list of contacts 

    //... 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_contact); 

     //here i init my "listView" and "contacts" 

     adapter = new ContactAdapter(this, contacts); 
     listView.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 

     listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
       Log.e("longClick", "ok"); 
       return true; 
      } 
     }); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> a, View v, final int position, long id) { 
       //here i display a Dialog 
      } 
     }); 



    //some code 

Так что, когда я «простым нажатием кнопки» на элементе списка, появится диалоговое окно (ok). Но когда я «долго нажмите» на элементе списка, то появится диалоговое окно, и я не вижу журнала ...

(Элемент, который я использую для моих ListView получил только 2 EditText)

PS: XML, где показан мой ListView

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="@color/white"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="100" 
     android:id="@+id/screen"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView4" 
       android:background="@drawable/w_aff2" /> 
     </FrameLayout> 

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

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="20" 
       android:weightSum="100"> 

       <ListView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/listView" /> 
      </LinearLayout> 

      <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="80"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="3"> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:layout_margin="14dp"> 

        <ImageButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/button_back" 
         android:layout_gravity="center" 
         android:background="@drawable/grey_button" 
         android:src="@drawable/i_back_blue" /> 
       </FrameLayout> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:layout_margin="14dp"> 

       </FrameLayout> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:layout_margin="14dp"> 

        <ImageButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/button_add" 
         android:src="@drawable/i_add_blue" 
         android:background="@drawable/grey_button" 
         android:layout_gravity="center" /> 
       </FrameLayout> 
      </LinearLayout> 
     </FrameLayout> 

    </LinearLayout> 

</LinearLayout> 
    </RelativeLayout> 

PS2: не смотрите на журнал, я положил, что, например, но если я ставлю что-нибудь еще ничего не происходит, появляется только диалог ...

PS3: XML-я использую для каждого элемента ListView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/item_screen" 
    android:background="@color/white"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="3" 
     android:id="@+id/item_layout" 
     android:layout_marginRight="16dp" 
     android:layout_marginLeft="16dp" 
     android:padding="7dp"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/layout_label"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="New Text" 
       android:id="@+id/item_label" 
       android:layout_weight="1" 
       android:textColor="@color/grey_dark" 
       android:gravity="center_vertical" 
       android:textStyle="bold" 
       android:textSize="16dp" /> 
     </FrameLayout> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/empty_layout"> 

     </FrameLayout> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/layout_value" 
      android:paddingTop="8dp" 
      android:paddingBottom="8dp"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="New Text" 
       android:id="@+id/item_value" 
       android:textColor="@color/grey_dark" 
       android:textStyle="bold" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="16dp" /> 
     </FrameLayout> 

    </LinearLayout> 

</LinearLayout> 

РЕШЕНИЕ Я имел onSwipeRight() на мой слушатель, он блокирует onItemLongClickListener

здесь код, блокировало слушателю

listView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) { 
    public void onSwipeRight(){ 
     //some code 
    } 
}); 

Большое спасибо DDB для своего времени :)

+0

Как вы видите журнал, вы делаете журнал уровня ошибок. –

+1

Не могли бы вы поделиться xml, где будет показан список? –

+0

, вы должны, вероятно, изменить фильтр logcat с отладки на ошибку. – br00

ответ

1

Почему вы передаёте new AdapterView.OnItemLongClickListener?

просто сделать, как это ниже вместо

listView.setOnItemLongClickListener(new OnItemLongClickListener() { 
       @Override 
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
        Log.e("longClick", "ok"); 
        return true; 
       } 
      }); 
+0

Android Studio добавляет «AdapterView» сам по себе, если я его удалю, мне нужно добавить ** import android.widget.AdapterView.OnItemLongClickListener ** и он по-прежнему не работает:/ – Option

+0

ОК, чем пытаться передать 'новый ListView.OnItemLongClickListener' – ddb

+0

он все еще не работает :( запускается только onItemClickListener – Option

0

Не устанавливайте listView.setLongClickable(true);, listView.setClickable(true); в вашей Яве.

Ни

android:longClickable="true" 
android:clickable="true" 

Внутри вашего XML

+0

Я добавил это, потому что увидел сообщение, которое произвело это: / – Option

Смежные вопросы