1

I have a ListView, and when you tap on an item, I want to check whether the item was clicked on on the left or the right half. This is the code I have:

package com.testapp.toroco;

import android.support.v4.app.ListFragment;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragmentA extends ListFragment implements OnClickListener {

int width;
public void onCreate(Bundle b) {
    super.onCreate(b);
    getSize();
}
public void getSize(){
    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    width = size.x;
}

@Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] itemlist = {"A","b","C","d","E","f"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>  (getActivity(),R.layout.layoutrow,R.id.name,itemlist);
    setListAdapter(adapter);
  }
@Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    // do something with the data
  }
public boolean dispatchTouchEvent(MotionEvent event) {
    int actionX = (int) event.getX();

int middlePoint = width/2 ; 

if(actionX >middlePoint ){
    Log.d("Touch", "Right");
}else if (actionX <middlePoint ){
    Log.d("Touch", "Left");
}

                return false;
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
}

I am aware that I haven't properly added the listener and that The onListItemClick and dispatchTouchEvent are currently two seperate events. But unfortunately I have now idea how I can accomplish that. Is there a better way to find out on which half it was clicked on? If not, on which View do I have to register the ClickListener and how can I make one Event (onListItemClick and dispatchTouchEvent) check the outcome of the other?

Thank you very much for your help!

petroni
  • 766
  • 1
  • 13
  • 29

2 Answers2

2

Tweek! You can create a custom listview with custom layout, in which you can add two views to left and right half and setOnClickListeners() for both these views.

Edit:

Ill give you an example on how to do it.

create an xml layout for list item : listItem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/leftPart"
        android:layout_width="0dip"
        android:layout_height="10dp"
        android:orientation="vertical"
        android:layout_weight="0.5"
        android:background="#ff0000" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/rightPart"
        android:layout_width="0dip"
        android:layout_height="10dp"
        android:orientation="vertical"
        android:layout_weight="0.5"
        android:background="#00ff00" >
    </LinearLayout>

</LinearLayout>

Write a custom adapter for your listView: CustomListAdapter.java

public class CustomListAdapter extends ArrayAdapter<String> {
    int resource;

    // change String to your required datatype 
    public CustomListAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
        this.resource = resource;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(resource, parent, false);
        }

        final int pos = position;
        convertView.findViewById(R.id.leftPart).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Do your work here
                Toast.makeText(getContext(), "Left Side of " + pos + "th element clicked", Toast.LENGTH_SHORT).show();

            }
        });

        convertView.findViewById(R.id.rightPart).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Do your work here
                Toast.makeText(getContext(), "Right Side of " + pos + "th element clicked", Toast.LENGTH_SHORT).show();

            }
        });

        return convertView;
    }

}

Set adapter to your ListView like this,

yourListView.setAdapter(getActivity(),R.id.listItem, /*your data*/);

I haven't tested this code, but this should work.

Vijeesh
  • 181
  • 10
  • Since I'm new to Android, how do I add the Listeners to the View? Or better: How do I get the View in my class? If I just use "getActivity().findViewById(R.id.rowleft)" it has a NPE at that point – petroni Oct 21 '14 at 18:13
  • Works for the most part! But now it won't use my Array to put in the text into the listitems. So every text is now the one from the layout file. I add the Adapter like this: `ArrayAdapter adapter = new DrinkListAdapter(getActivity(),R.layout.layoutrow,R.id.name,itemlist); setListAdapter(adapter)`; – petroni Oct 29 '14 at 14:52
  • Add an int field `textViewId` and set text like this inside `getView` before returning `converView`. `((TextView)convertView.findViewById(textViewId)).setText(getItem(position));` – Vijeesh Nov 01 '14 at 10:17
  • Dont forget to initialize it inside constructor: `textViewId= textViewResourceId;' – Vijeesh Nov 01 '14 at 10:27
0

In your dispatchTouchEvent method :

public boolean dispatchTouchEvent(MotionEvent event) {

   switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:

                x1 = event.getX();
                y1 = event.getY();
                System.out.println("x1-- "+x1);
                System.out.println("y1-- "+y1);
                dx = x1 - y1 ;
                break ;

            case MotionEvent.ACTION_UP:

                x2 = event.getX();
                y2 = event.getY();
                System.out.println("x2-- "+x2);
                System.out.println("y2-- "+y2);
                dy = x2 - y2 ;

            }


             if(dx > 1)
                {
                    System.out.println(" clicked on right side ");

                }else 
                {
                    System.out.println(" clicked on left side ");

                }

            return false;
}
Harshad07
  • 608
  • 7
  • 23