使用ButterKnife处理ListView项目单击

时间:2016-09-22 07:28:01

标签: android listview onitemclicklistener onitemclick

我正在尝试使用常规AdapterView.OnItemClickedListener或ButterKnife @OnItemSelected注释来处理列表视图上的项目点击,但它们都不起作用。

我已尝试登录以查看是否有任何点击通过:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.e(Overview.class.getSimpleName(), String.valueOf(position));
}

    @OnItemClick(R.id.tracks)
    void onItemSelected(int position){
        Log.e("Tracks", String.valueOf(position));
    }

但它们都不起作用。可能是什么问题?

我班的代码是:

public class Overview extends Fragment implements AdapterView.OnItemClickListener {

private static final String DATA = "data";

public static final int ARTIST = 1;
public static final int ALBUM = 2;
public static final int PLAYLIST = 3;

int source;

RealmList<Track> trackList;
RealmList<Album> albumList;

@BindView(R.id.tracks)
ExpandableHeightListView tracks;
@BindView(R.id.albums)
RecyclerView albums;
@BindView(R.id.albumsLayout)
LinearLayout albumsLayout;

@OnItemClick(R.id.tracks)
void onItemSelected(int position){
    Log.e("Tracks", String.valueOf(position));
}

@OnClick({R.id.all_tracks, R.id.all_albums})
public void showMore(View view) {
    switch (view.getId()) {
        case R.id.all_tracks:

            break;
        case R.id.all_albums:

            break;
    }
}

public Overview newInstance(Wrapper wrapper) {
    Bundle bundle = new Bundle();
    Overview overview = new Overview();
    bundle.putParcelable(DATA, Parcels.wrap(wrapper));
    overview.setArguments(bundle);

    return overview;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.overview, container, false);

    ButterKnife.bind(this, view);

    tracks.setExpanded(true);
    tracks.setOnItemClickListener(this);
    albums.setLayoutManager(new LinearLayoutManager(getActivity(),
            LinearLayoutManager.HORIZONTAL, false));
    albums.setNestedScrollingEnabled(false);

    Wrapper wrapper = Parcels.unwrap(getArguments().getParcelable(DATA));

    trackList = wrapper.getTracks();
    albumList = wrapper.getAlbums();
    source = wrapper.getSource();

    setItems(source);

    return view;
}

private void setItems(int source) {
    if (!trackList.isEmpty()) {
        TrackListAdapter trackListAdapter;

        switch (source) {
            case ARTIST:
                RealmList<Track> realmList = new RealmList<>();
                ArrayList<Track> list = new ArrayList<>(trackList.size() > 10 ?
                        trackList.subList(0, 10) : trackList);

                realmList.addAll(list);

                trackListAdapter = new TrackListAdapter(getActivity(),
                        R.layout.track_list_item, realmList);
                tracks.setAdapter(trackListAdapter);
                break;
            case PLAYLIST:
                trackListAdapter = new TrackListAdapter(getActivity(),
                    R.layout.track_list_item, trackList);
                tracks.setAdapter(trackListAdapter);
                break;
            case ALBUM:
                trackListAdapter =  new TrackListAdapter(getActivity(),
                        R.layout.album_track_list_item, trackList);
                tracks.setAdapter(trackListAdapter);
                break;
        }
    }

    if (albumList != null && !albumList.isEmpty()) {
        albumsLayout.setVisibility(View.VISIBLE);

        AlbumsAdapter albumsAdapter = new AlbumsAdapter(getActivity(), albumList);
        albums.setAdapter(albumsAdapter);
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Log.e(Overview.class.getSimpleName(), String.valueOf(position));
}
}

它的布局文件是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/DarkBodyBackground"
        android:orientation="vertical">

        <TextView
            style="@style/DarkThemeHeaderText"
            android:text="Top Tracks" />

        <com.radioafrica.music.view.ExpandableHeightListView
            android:id="@+id/tracks"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/DarkCardBackgroundColor"
            android:dividerHeight="1dip"
            android:footerDividersEnabled="true"
            android:headerDividersEnabled="true" />

        <TextView
            android:id="@+id/all_tracks"
            style="@style/ShowAllText" />

        <LinearLayout
            android:id="@+id/albumsLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            android:visibility="gone">

            <TextView
                style="@style/DarkThemeHeaderText"
                android:text="Albums" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/albums"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/all_albums"
                style="@style/ShowAllText" />

        </LinearLayout>

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

0 个答案:

没有答案