Can I create an ExpandableListView with the swipe effect?

6

I have a ListView that is composed of 3 ListView's chained. The first is a Year Release list, which has a list of genres that in turn stores a list of movies. I wanted to have the option to expand and collapse the lists by the year and genre of the movies. I already made Adapter's to Swipe and I used this library: link

From what I read, I need to have a label and expand the list from it, but in my case, the label is going to be an item from ListView "parent". I made a prototype of the screen to be easier to visualize:

TheSwipefeaturewillonlyexistinthemovielist.SomyMovieAdapterextendsBaseSwipeAdapter,butsinceIwantittobeanExpandablelist,itwouldhavetoextendBaseExpandableListAdapter.

AdapterFilme.java:

publicclassAdaptadorFilmeextendsBaseSwipeAdapter{String[]nomeFilmeint[]imgFilme;Contextcontext;privatestaticLayoutInflaterinflater=null;publicAdaptadorFilme(Contextcontext,int[]imgFilme,String[]nomeFilme){this.nomeFilme=nomeFilme;this.imgFilme=imgFilme;inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@OverridepublicintgetCount(){returnnomeFilme.length;}@OverridepublicObjectgetItem(intposition){returnposition;}@OverridepubliclonggetItemId(intposition){returnposition;}publicclassHolderextendsAppCompatActivity{ImageViewimgFilme;TextViewnomeFilme;SwipeLayoutswipeLayout;LinearLayoutlinearLayout;SwipeLayoutitem;}@OverridepublicintgetSwipeLayoutResourceId(intposition){returnR.id.swipeProd;}@OverridepublicViewgenerateView(finalintposition,ViewGroupparent){Viewv;finalHolderh;v=inflater.inflate(R.layout.lista_filmes,null);h=newHolder();h.imgFilme=(CheckBox)v.findViewById(R.id.img);h.nomeFilme=(ImageView)v.findViewById(R.id.nome);h.swipeLayout=(SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));h.imgFilme.setImageResource(imgFilme[position]);h.nomeFilme.setText(nomeFilme[position]);h.swipeLayout.addSwipeListener(newSwipeLayout.SwipeListener(){@OverridepublicvoidonStartOpen(SwipeLayoutlayout){}@OverridepublicvoidonOpen(SwipeLayoutlayout){}@OverridepublicvoidonStartClose(SwipeLayoutlayout){}@OverridepublicvoidonClose(SwipeLayoutlayout){}@OverridepublicvoidonUpdate(SwipeLayoutlayout,intleftOffset,inttopOffset){}@OverridepublicvoidonHandRelease(SwipeLayoutlayout,floatxvel,floatyvel){}});returnv;}@OverridepublicvoidfillValues(intposition,ViewconvertView){}

}

AdapterGenero.java

publicclassAdaptadorGeneroextendsBaseAdapter{finalHolderh=newHolder();String[]nomeFilme;int[]imgFilme;Contextcontext;AdaptadorFilmesadaptadorFilmesActivity;privatestaticLayoutInflaterinflater=null;publicAdaptadorGenero(inttipoAdap,String[]nomeFilme,String[]nomeGenero,String[]precosProdutos,int[]imgFilme,int[]qtd,Contextcontext){this.nomeFilme=nomeFilme;this.imgFilme=imgFilme;this.context=context;this.adaptadorFilmesActivity=newAdaptadorFilmes(context,imgFilme,nomeFilme);inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@OverridepublicintgetCount(){returnimgFilme.length;}@OverridepublicObjectgetItem(intposition){returnposition;}@OverridepubliclonggetItemId(intposition){returnposition;}publicclassHolder{TextViewnomeGenero;ImageViewsetinha;ListViewlistaFilmes;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){Viewv;v=inflater.inflate(R.layout.lista_cat,null);h.nomeGenero=(TextView)v.findViewById(R.id.nomeitem);h.setinha=(ImageView)v.findViewById(R.id.setinha);h.nomeGenero.setText(nomeGenero[position]);h.listaFilmes.setAdapter(adaptadorFilmesActivity);calculeHeightListView();returnv;}privatevoidcalculeHeightListView(){inttotalHeight=0;ListAdapteradapter=h.listaFilmes.getAdapter();intlenght=adapter.getCount();for(inti=0;i<lenght;i++){ViewlistItem=adapter.getView(i,null,h.listaFilmes);listItem.measure(0,0);totalHeight+=listItem.getMeasuredHeight();}ViewGroup.LayoutParamsparams=h.listaFilmes.getLayoutParams();params.height=totalHeight+(h.listaFilmes.getDividerHeight()*(adapter.getCount()-1));h.listaFilmes.setLayoutParams(params);h.listaFilmes.requestLayout();}

}

TheYearAdapterfollowsthesameideaastheGenderAdapter.HowdoIclickonthelittleboxofListView"parent" to ListView "child" to be displayed / hidden?

    
asked by anonymous 10.10.2016 / 17:48

1 answer

1

I had a problem similar to yours, and I was able to solve it using a RecyclerView and a custom adapter. I was able to build on the android-advancedrecyclerview library and I was changing what I needed for my project.

for yours, for your case, I believe the Expandable & Draggable & Swiping solve your problem.

Fragment with the call of the ExpandableListView and the creation of the Adapter

ExpandableDraggableSwipeableExampleFragment

Adapter that controls the swipe (and also the drag) that allows you to delete the line after expanding the ELV

ExpandableDraggableSwipeableExampleAdapter

Within the adapter you have specific methods to control the swipe of the "parent"

@Override
    public SwipeResultAction onSwipeGroupItem(MyGroupViewHolder holder, int groupPosition, int result) {
        Log.d(TAG, "onSwipeGroupItem(groupPosition = " + groupPosition + ", result = " + result + ")");
    switch (result) {
        // swipe right
        case Swipeable.RESULT_SWIPED_RIGHT:
            if (mProvider.getGroupItem(groupPosition).isPinned()) {
                // pinned --- back to default position
                return new GroupUnpinResultAction(this, groupPosition);
            } else {
                // not pinned --- remove
                return new GroupSwipeRightResultAction(this, groupPosition);
            }
            // swipe left -- pin
        case Swipeable.RESULT_SWIPED_LEFT:
            return new GroupSwipeLeftResultAction(this, groupPosition);
        // other --- do nothing
        case Swipeable.RESULT_CANCELED:
        default:
            if (groupPosition != RecyclerView.NO_POSITION) {
                return new GroupUnpinResultAction(this, groupPosition);
            } else {
                return null;
            }
    }
}

and the "child"

@Override
public SwipeResultAction onSwipeChildItem(MyChildViewHolder holder, int groupPosition, int childPosition, int result) {
    Log.d(TAG, "onSwipeChildItem(groupPosition = " + groupPosition + ", childPosition = " + childPosition + ", result = " + result + ")");

    switch (result) {
        // swipe right
        case Swipeable.RESULT_SWIPED_RIGHT:
            if (mProvider.getChildItem(groupPosition, childPosition).isPinned()) {
                // pinned --- back to default position
                return new ChildUnpinResultAction(this, groupPosition, childPosition);
            } else {
                // not pinned --- remove
                return new ChildSwipeRightResultAction(this, groupPosition, childPosition);
            }
            // swipe left -- pin
        case Swipeable.RESULT_SWIPED_LEFT:
            return new ChildSwipeLeftResultAction(this, groupPosition, childPosition);
        // other --- do nothing
        case Swipeable.RESULT_CANCELED:
        default:
            if (groupPosition != RecyclerView.NO_POSITION) {
                return new ChildUnpinResultAction(this, groupPosition, childPosition);
            } else {
                return null;
            }
    }
}
    
27.10.2016 / 19:01