0

I got a fragment with :

1 TextView

1 ListView

1 LinearLayout with 3 buttons

When I click on a button, there is no problems and the function onClick from my fragment class works.

When I try to click on an element from my listview, nothing happend ( I put 2 onItemClick() methods, one in my fragment class, and the other one in my adapter class, but no one works.

I found this related topic : Onclicklistner not working in fragment listview

But I have only one element in my listview (only imageview), and even if I put android:clickable and focusable to false, nothing change

my listview item only contain an 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>

Here's my fragment class:

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;
}
}

and here's my adapter class :

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;
}
}
Community
  • 1
  • 1
gamerounet
  • 279
  • 4
  • 22
  • Have you tried setting `row.setClickable(true);` on your `getView` method? – antonio Jun 15 '16 at 16:22
  • Like I say in the other answer, I can reach the row.onclick now (if I put row.setClickable(true). But Is there a way to reach my listViewSelectOperation.onItemClick() in my fragment class? to access position and id of the clicked item? – gamerounet Jun 16 '16 at 09:41

1 Answers1

0

You can make the LinearLayout clickable by setting android:clickable="true" in your row XML.

jaibatrik
  • 6,770
  • 9
  • 33
  • 62
  • finally I can reach my row.onclick after adding clickable=true in my linearlayout. Is there a way to reach my listViewSelectOperation.onItemClick() in my fragment class? to access position and id of the clicked item – gamerounet Jun 16 '16 at 09:32