onItemClick无法使用特定适配器

时间:2016-06-15 16:18:07

标签: android listview android-fragments android-adapter

我得到了一个片段:

1 TextView

1 ListView

1个带3个按钮的LinearLayout

当我点击一个按钮时,没有任何问题,我的片段类中的onClick功能正常工作。

当我尝试点击listview中的元素时,没有任何事情发生(我把2个onItemClick()方法,一个放在我的片段类中,另一个放在我的适配器类中,但是没有人工作。

我发现了这个相关主题:Onclicklistner not working in fragment listview

但是我的listview中只有一个元素(只有imageview),即使我把android:clickable和focusable设为false,也没有任何改变

我的listview项目只包含一个imageview:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/item_operation_img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="false"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />
    </LinearLayout>
</ScrollView>

这是我的片段类:

public class FragmentOperation extends Fragment {

private ListView listViewSelectOperation;
private SelectOperationAdapter adapter4;
private ArrayList<ListOperation> listOperations;
private Button buttonFuturOperation;
private Button buttonCurrentOperation;
private Button buttonPastOperation;


public FragmentOperation(){

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_operation, container, false);


    buttonFuturOperation = (Button)rootView.findViewById(R.id.futurOperationButton);
    buttonCurrentOperation = (Button)rootView.findViewById(R.id.currentOperationButton);
    buttonPastOperation = (Button)rootView.findViewById(R.id.pastOperationButton);

    listViewSelectOperation = (ListView)rootView.findViewById(R.id.lv_operations);
    listOperations = new ArrayList<>();


    int[] imageOperation = {R.drawable.operations_1, R.drawable.operations_2, R.drawable.operations_3, R.drawable.operations_4, R.drawable.operations_5, R.drawable.operations_6};

    for (int i = 0; i < imageOperation.length; i++) {
        if( (i % 2) == 0){
            listOperations.add(new ListOperation(imageOperation[i]));

        } else {
            listOperations.add(new ListOperation(imageOperation[i]));
        }
    }

    adapter4 = new SelectOperationAdapter(this.getActivity().getApplicationContext(), listOperations);
    listViewSelectOperation.setAdapter(adapter4);

    buttonFuturOperation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("CLICK BUTTON1", "BUTTON1");
        }
    });

    buttonCurrentOperation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("CLICK BUTTON2", "BUTTON2");
        }
    });

    buttonPastOperation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("CLICK BUTTON3", "BUTTON3");
        }
    });

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

            Log.i("CLICK position", String.valueOf(position));
            Log.i("CLICK id", String.valueOf(id));

            final FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.main_content, new FragmentOperationDetail());
            ft.commit();
        }
    });


    return rootView;
}
}

这是我的适配器类:

public class SelectOperationAdapter extends BaseAdapter {

ArrayList<ListOperation> myList = new ArrayList<>();
Context context;

public SelectOperationAdapter(Context context, ArrayList<ListOperation> myList) {
    this.context = context;
    this.myList = myList;
}

@Override
public int getCount() {
    return myList.size();
}

@Override
public Object getItem(int position) {
    return myList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    if (row == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        row = mInflater.inflate(R.layout.item_select_operation, parent, false);


        mViewHolder = new MyViewHolder();
        mViewHolder.item_operation_img = (ImageView) row
                .findViewById(R.id.item_operation_img);
        row.setTag(mViewHolder);
    } else {
        mViewHolder = (MyViewHolder) row.getTag();
    }
    ListOperation listItem = (ListOperation) getItem(position);

    mViewHolder.item_operation_img.setImageResource(listItem.getImgOperation());

    row.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //the row has been clicked
            //do whatever you want with
            Log.i("CLICK", "CLICKDED");
        }
    });
    return row;
}

private class MyViewHolder {
    ImageView item_operation_img;
}
}

1 个答案:

答案 0 :(得分:0)

您可以通过在行XML中设置android:clickable="true"来使LinearLayout可以点击。