vodailuong2
vodailuong2

Reputation: 31

Can't implement OnItemClickListener in android-swipelistview

I use android-swipelistview in https://github.com/47deg/android-swipelistview but I can't implement OnItemClickListener. It not running when I write:

swipeListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            startActivity(new Intent(getActivity().getApplicationContext(),ComposeActivity.class));
        }
    });

Upvotes: 3

Views: 2620

Answers (4)

itzhar
itzhar

Reputation: 13031

when you use onClickFrontView with setSwipeListViewListener the swipe become disabled.

i found this solution useful:

wrap you front view with another layout witout tag=front nor id = front:

<LinearLayout
    android:id="@+id/front"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:tag="front" >

    <LinearLayout
        android:id="@+id/frontView"
        style="@style/MyListFrontContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="15dp"
        >
          ...your front view elements
       </LinearLayout>
    </LinearLayout>

then in your Adapter setOnClickListener in that layout

holder.frontView= (LinearLayout)convertView.findViewById(R.id.frontView);
holder.frontView.setOnClickListener(this);

Upvotes: 0

Beyka
Beyka

Reputation: 1372

try this:

swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
    @Override
    public void onClickFrontView(int position) {
         super.onClickFrontView(position);
         your code...
    }
}

Upvotes: 16

shfshrf
shfshrf

Reputation: 391

SwipeListView have two Views as FrontView and BackView.It should be designed in xml as follows

package_raw.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/back"             <===
    style="@style/ListBackContent"
    android:layout_width="wrap_content"
    android:layout_height="110dp"   
    android:tag="back" >

<LinearLayout
    android:id="@+id/backshowportion"
    style="@style/ListBackContent"
    android:layout_width="250dp"
    android:layout_height="110dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/bg"
    android:gravity="right|center" >

<TextView
     ..... />

<TextView
     ..... />

<TextView 
     ...../>

</LinearLayout>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/front"            <===
    style="@style/ListFrontContent"
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:orientation="vertical"
    android:tag="front" >

<TextView
     ..... />

<TextView
     ..... />

<TextView
     ..... />

<TextView
     ..... />

and then specify where you need to click exactly on "FrontView" or "BackView" as doing in the following code segment:

protected void onCreate(Bundle savedInstanceState) 
{
    swipeListView=(SwipeListView)findViewById(R.id.lstRequests);
    if (Build.VERSION.SDK_INT >= 11) 
    {
        swipeListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
    {
        swipeListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() 
        {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position,long id, boolean checked)
            {
                mode.setTitle("Selected (" + swipeListView.getCountSelected() + ")");                  
            }
            @Override
            public void onDestroyActionMode(ActionMode mode) 
            {
                swipeListView.unselectedChoiceStates();
            }
        @Override
        public boolean onActionItemClicked(ActionMode mode,MenuItem item) 
        {
            return false;
        }
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) 
        {
            return false;
        }
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu)
        {
            return false;
        }
    });
}
}
public void onStart()
{
    super.onStart();        
    listner(); 
}
private void listner()
{
 swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() 
 {          
        @Override
        public void onStartOpen(int position, int action, boolean right)
        {
             Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
        }

        @Override
        public void onStartClose(int position, boolean right)
        {
            Log.d("swipe", String.format("onStartClose %d", position));                 
        }   

        @Override
        public void onOpened(int position, boolean toRight)
        {   

        }   

        @Override
        public void onMove(int position, float x)
        {

        }   

        @Override
        public void onListChanged()
        {

        }

        @Override
        public void onLastListItem()
        {   

        }   

        @Override
        public void onFirstListItem()
        {   

        }   

        @Override
        public void onDismiss(int[] reverseSortedPositions) 
        {

        }

        @Override           
        public void onClosed(int position, boolean fromRight) 
        {   

        }   

        @Override
        public void onClickFrontView(int position)        //<===================FrontView
        {
            Log.d("swipe", String.format("onClickFrontView %d", position));
            swipeListView.openAnimate(position);
        }   

        @Override
        public void onClickBackView(int position)         //<===================BackView
        {
            Log.d("swipe", String.format("onClickBackView %d", position));
            swipeListView.closeAnimate(position);
        }

        @Override
        public void onChoiceStarted() 
        {

        }

        @Override
        public void onChoiceEnded() 
        {   

        }   

        @Override
        public void onChoiceChanged(int arg0, boolean arg1) 
        {

        }   

        @Override
        public int onChangeSwipeMode(int arg0)
        {
            return 0;
        }
    });   

Hope I could help you.

Upvotes: 5

Eugen
Eugen

Reputation: 1543

You can try implementing it with OnTouchListener and write your own code for identifying swipes versus single-clicks.

Upvotes: -1

Related Questions