列出onItemClickListener无法使用

时间:2016-09-07 08:57:13

标签: java android 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="match_parent"
 android:paddingLeft="10dp"
 android:paddingStart="10dp"
 android:paddingRight="10dp"
 android:paddingEnd="10dp"
 android:focusable="false"
 android:focusableInTouchMode="false">

<android.support.v7.widget.CardView
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="60dp"
    android:padding="5dp"
    android:elevation="6dp">

    <TextView
        android:id="@+id/major_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_large"
        android:text="Computer Science"
        android:gravity="center" />

</android.support.v7.widget.CardView>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/subject_scroll"
    android:paddingBottom="8dp"
    android:paddingTop="10dp"
    android:paddingLeft="15dp"
    android:paddingStart="15dp"
    android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>

</LinearLayout>

`这是dropdown.xml,它是将在listview中的视图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
    android:id="@+id/courses_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.7"
    android:orientation="vertical"
    android:clickable="false">
</ListView>

</LinearLayout>

main.xml中 带有id couses_list的listview,包含dropdown.xml视图

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

          Log.d("ListView", "onItemClick: true");

});

我已经尝试将setFocusable()设置为false到listview中的每个视图以及堆栈溢出中的珍贵解决方案中显示的其他方法但是没有任何对我有效。我在这里做错了什么..

ListView使用扩展BindableAdapter的MajorAdapter

public class MajorAdapter extends BindableAdapter<Major> {

public class ViewHolder {
    TextView majorName;
    private boolean isOpen = false;
    private RecyclerView subjectRecyclerView;
    Major major;
    List<Subject> subjects = new ArrayList<>();

    ViewHolder(View view){
        majorName = (TextView)view.findViewById(R.id.major_name);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        subjectRecyclerView = (RecyclerView)view.findViewById(R.id.subject_scroll);
        subjectRecyclerView.setLayoutManager(linearLayoutManager);

        SubjectAdapter subjectAdapter = new SubjectAdapter(subjects);
        subjectRecyclerView.setAdapter(subjectAdapter);
        subjectRecyclerView.addItemDecoration(new RecyclerListDecorater(view.getContext()));

    }
    public void toggle(){
        if(this.isOpen)
            this.isOpen = false;
        else
            this.isOpen = true;
    }

    public boolean isOpen(){
        return this.isOpen;
    }
    public Model getMajor(){
        return major;
    }

    public RecyclerView getRecyclerView(){
        return this.subjectRecyclerView;
    }
}

public MajorAdapter(Context context){
    super(context);
}

@Override
public View getNewView(LayoutInflater inflater, int position, ViewGroup container) {
    View view = inflater.inflate(R.layout.majors,null,false);
    view.setFocusable(false);
    ViewHolder holder = new ViewHolder(view);
    view.setTag(holder);

    return view;
}

@Override
public void bindView(Major item, int position, View view) {
    ViewHolder holder= (ViewHolder)view.getTag();
    holder.major = item;
    holder.majorName.setText(item.getCourseName());
   }
}

这是可绑定的适配器

public abstract class BindableAdapter<T> extends ArrayAdapter<T> {


private LayoutInflater inflater;

public BindableAdapter(Context context){
    super(context,0);
    setup(context);
}

private void setup(Context context){
    inflater = LayoutInflater.from(context);
}

@Override
public final View getView(int position, View view, ViewGroup container){
    if(view == null){
        view = getNewView(inflater, position, container);
            if(view == null)
                throw new IllegalStateException("View created cannot be null");
    }

    bindView(getItem(position), position, view);
    return view;
}


public abstract View getNewView(LayoutInflater inflater, int position, ViewGroup container);
public abstract void bindView(T item, int position, View view);

@Override
public View getDropDownView(int position, View view, ViewGroup parent){
    return getView(position, view, parent);
  }
}

0 个答案:

没有答案