0

Я потратил больше дня, пытаясь понять это. Я совершенно уверен, что мой фокус что-то украл чем-то в макете.My OnItemClicked не называется

Adapter я использую:

public class AudioAdapter extends ArrayAdapter<MusicContent> 
{ 
private Context context; 

private ImageView albumArt; 
private TextView songName; 
private TextView artistName; 
private TextView albumName; 
private TextView genre; 
private TextView duration; 

private CheckBox checkbox; 

private List<MusicContent> content = new ArrayList<MusicContent>(); 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View row = convertView; 

    if (row == null) 
    { 
     // ROW INFLATION 
     LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.testing_cell, parent, false); 
    } 

    // Get item 
    MusicContent item = getItem(position); 
    RelativeLayout root = (RelativeLayout) row.findViewById(R.id.list_cell_layout); 

    // perform a series of checks to maintain customizability 
    albumArt = (ImageView) row.findViewById(R.id.list_cell_image); 
    if (albumArt != null) 
    { 
     if (item.hasAlbumArt()) 
      albumArt.setImageBitmap(item.getAlbumArt()); 
     else 
      albumArt.setImageDrawable(context.getResources().getDrawable(
        R.drawable.ic_music_album)); 
     albumArt.setClickable(false); 
     albumArt.setFocusable(false); 
    } 

    LinearLayout checkLL = (LinearLayout) row.findViewById(R.id.list_cell_music_info); 
    if (checkLL != null) 
    { 
     // display some song info 
     songName = (TextView) checkLL.findViewById(R.id.list_cell_title); 
     if (songName != null) 
     { 
      songName.setText(item.getDisplayName()); 
      songName.setVisibility(View.VISIBLE); 
     } 

     checkLL = (LinearLayout) row.findViewById(R.id.list_cell_artist_info); 
     if (checkLL != null) 
     { 
      // display artist info too 
      artistName = (TextView) checkLL.findViewById(R.id.list_cell_artist_name); 
      if (artistName != null) 
       artistName.setText(item.getArtist()); 

      albumName = (TextView) checkLL.findViewById(R.id.list_cell_album); 
      if (albumName != null) 
       albumName.setText(item.getAlbum()); 

      duration = (TextView) checkLL.findViewById(R.id.list_cell_duration); 
      if (duration != null) 
       duration.setText(item.getDurationString()); 

      genre = (TextView) checkLL.findViewById(R.id.list_cell_genre); 
      if (genre != null) 
       genre.setText(item.getGenre()); 
     } 

     FrameLayout checkFL = (FrameLayout) row.findViewById(R.id.endoflineinfo); 
//   checkLL = (LinearLayout) row.findViewById(R.id.endoflineinfo); 
     if (checkFL != null) 
     { 
      checkbox = (CheckBox) checkFL.findViewById(R.id.in_playlist); 
      if (checkbox != null) 
       checkbox.setChecked(!item.getPlaylist().isEmpty()); 

      checkbox.setFocusable(false); 
      checkbox.setClickable(false); 
     } 
    } 

    root.setDescendantFocusability(RelativeLayout.FOCUS_BLOCK_DESCENDANTS); 
    return row; 
} 

Мой макет XML:

<RelativeLayout 
    android:id="@+id/list_cell_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@+id/list_cell_menu_button" 
    android:clickable="true" 
    android:descendantFocusability="blocksDescendants" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:orientation="horizontal" 
    android:paddingBottom="@dimen/playlistItemPadding" 
    android:paddingTop="@dimen/playlistItemPadding" > 
    > 

    <ImageView 
     android:id="@+id/list_cell_image" 
     android:layout_width="@dimen/playlistItemSize" 
     android:layout_height="@dimen/playlistItemSize" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="@dimen/playlistMargin" 
     android:layout_marginRight="@dimen/playlistMargin" 
     android:clickable="false" 
     android:focusable="false" 
     android:scaleType="centerCrop" 
     android:tag="@string/image_type_browsing_list_item" /> 

    <ImageView 
     android:id="@+id/list_cell_overlay" 
     android:layout_width="@dimen/playlistItemSize" 
     android:layout_height="@dimen/playlistItemSize" 
     android:layout_marginLeft="@dimen/playlistMargin" 
     android:layout_marginRight="@dimen/playlistMargin" 
     android:clickable="false" 
     android:focusable="false" 
     android:padding="8.0dip" 
     android:src="@drawable/ic_mc_play_overlay" 
     android:visibility="gone" /> 

    <FrameLayout 
     android:id="@+id/endoflineinfo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="@dimen/playlistMargin" 
     android:layout_marginRight="@dimen/playlistMargin" 
     android:clickable="false" 
     android:focusable="false" > 

     <ImageView 
      android:id="@+id/list_cell_item_locked" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="false" 
      android:focusable="false" 
      android:src="@drawable/ic_lock" 
      android:visibility="invisible" /> 

     <CheckBox 
      android:id="@+id/in_playlist" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:button="@drawable/btn_check" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:visibility="invisible" /> 
    </FrameLayout> 

    <LinearLayout 
     android:id="@+id/list_cell_music_info" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@id/endoflineinfo" 
     android:layout_toRightOf="@id/list_cell_image" 
     android:clickable="false" 
     android:focusable="false" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/list_cell_title" 
      style="@style/Text.Listview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left" 
      android:clickable="false" 
      android:ellipsize="marquee" 
      android:focusable="false" 
      android:marqueeRepeatLimit="marquee_forever" /> 

     <LinearLayout 
      android:id="@+id/list_cell_artist_info" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="@dimen/playlistMargin" 
      android:clickable="false" 
      android:focusable="false" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/list_cell_artist_name" 
       style="@style/Text.Listview.Small" 
       android:layout_width="0.0sp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left" 
       android:layout_weight="0.5" 
       android:clickable="false" 
       android:ellipsize="marquee" 
       android:focusable="false" 
       android:marqueeRepeatLimit="marquee_forever" /> 

      <TextView 
       android:id="@+id/list_cell_album" 
       style="@style/Text.Listview.Small" 
       android:layout_width="0.0sp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left" 
       android:layout_weight="0.5" 
       android:clickable="false" 
       android:ellipsize="marquee" 
       android:focusable="false" 
       android:marqueeRepeatLimit="marquee_forever" /> 

      <TextView 
       android:id="@+id/list_cell_genre" 
       style="@style/Text.Listview.Small" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right" 
       android:layout_weight="0.0" 
       android:clickable="false" 
       android:focusable="false" 
       android:gravity="right" 
       android:visibility="gone" /> 

      <TextView 
       android:id="@+id/list_cell_duration" 
       style="@style/Text.Listview.Small" 
       android:layout_width="60.0sp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right" 
       android:layout_weight="0.0" 
       android:clickable="false" 
       android:focusable="false" 
       android:gravity="right" /> 
     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

<ImageView 
    android:id="@+id/list_cell_menu_button" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:background="@drawable/button_background_square" 
    android:clickable="true" 
    android:descendantFocusability="blocksDescendants" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:padding="19.0dip" 
    android:scaleType="fitCenter" 
    android:src="@drawable/ic_menu_source_config" 
    android:visibility="gone" /> 

Установка onItemClick

public void onCreate(Bundle blahblah) 
{ 
    setContentView(R.layout.fragment_list); 
    contentListView = (ListView) findViewById(R.id.frag_item_browser_list); 
    contentListView.setItemsCanFocus(true); 
    contentListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    contentListView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS); 

    contentListView.setOnItemClickListener(new ListView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      Log.d("SHARK", "click on " + contentAdapter.getItem(arg2)); 
     } 
    }); 
    contentListView.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      Log.d("SHARK", "Long click on " + contentAdapter.getItem(arg2)); 
      return true; 
     } 
    }); 

    contentListView.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      Log.d("SHARK", "something = " + arg1 + "\t" + arg0.getAdapter().getItem(arg2)); 
      // Log.d("SHARK", "onClick happened ? "+arg1.callOnClick()); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      Log.d("SHARK", "nothing"); 
     } 
    }); 

    contentListView.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Log.d("SHARK", "touching = " + v); 
      return false; 
     } 
    }); 

    contentListView.setAdapter(contentAdapter); 
    contentListView.postInvalidate(); 
    contentAdapter.notifyDataSetChanged(); 
} 

OnTouch всегда возвращает ListView. OnSelectChange корректно возвращает RelativeLayouts. OnItemClick и OnLongItemClick просто не срабатывают.

Я пробовал использовать очень простой ArrayAdapter, и когда данные были в списке в его простой форме toString() - нажав обработанный. Таким образом, я уверен, что это либо пользовательский код адаптера, который питается onClick - , который является своего рода сомнением * - и XML, который может делать что-то фанк в фоновом режиме.

Пожалуйста, помогите.

ответ

0

В конце концов, я прибег к перекрестно на щелчок подход, ссылаясь на мой предназначенный onItemClickListener обратный вызов во внутреннем onClick() слушателя, присвоенным каждому зрения через адаптер ....

Несколько нравится эта

public SomeAdapter(Context context, int textViewResourceId, List<Content> objects, 
     OnItemClickListener clickListener) 
{ 
    super(context, textViewResourceId, objects); 
    this.context = context; 
    this.content = objects; 
    this.clickListener = clickListener; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View row = convertView; 

    if (row == null) 
    { 
     // ROW INFLATION 
     LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.list_cell, parent, false); 
    } 
    // initiate helpers for onClick hack 
    final AdapterView fParent = (AdapterView) parent; 
    final View fView = row; 
    final int fInt = position; 
    final long fLong = row.getId(); 

    // Get item 
    Content item = getItem(position); 
    if (item == null) 
     return row; 

    RelativeLayout root = (RelativeLayout) row.findViewById(R.id.list_cell_layout); 

    /** .... find views, do stuff .... */ 

    // magic happens where we bind an OnItemClick call to OnClick 
    root.setDescendantFocusability(RelativeLayout.FOCUS_BLOCK_DESCENDANTS); 

    if (clickListener != null) 
    { 
     OnClickListener cross = new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("SHARK", "internal onClick from adapter!!"); 
       clickListener.onItemClick(fParent, fView, fInt, fLong); 
      } 
     }; 
     // assign this listener 
     root.setOnClickListener(cross); 
    } 
    return row; 
} 

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

  • Нет связанных вопросов^_^