Can you create an Expandablelistview with the Swipe effect?

Asked

Viewed 350 times

6

I have a ListView which is composed of 3 ListView's chained. The first is a list of Year of release, which has a list of genres that in turn stores a list of movies. I wanted to have the option to expand and contract lists by year and genre of films. I’ve already done the Adapter's for Swipe and I used this library: https://github.com/daimajia/AndroidSwipeLayout

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

inserir a descrição da imagem aqui

The functionality of Swipe will exist only in the movie list. Then my Adapter der movies extends BaseSwipeAdapter, only since I want it to be an expandable list, I’d have to extend it BaseExpandableListAdapter.

Java adapter.:

public class AdaptadorFilme extends BaseSwipeAdapter {
String [] nomeFilme
int [] imgFilme;
Context context;
private static LayoutInflater inflater = null;

public AdaptadorFilme(Context context, int[] imgFilme,String[] nomeFilme) {
    this.nomeFilme = nomeFilme;
    this.imgFilme = imgFilme;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return nomeFilme.length;
}

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

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

public class Holder extends AppCompatActivity {
    ImageView imgFilme;
    TextView nomeFilme;
    SwipeLayout swipeLayout;
    LinearLayout linearLayout;
    SwipeLayout item;
}

@Override
public int getSwipeLayoutResourceId(int position) {
    return R.id.swipeProd;
}

@Override
public View generateView(final int position, ViewGroup parent) {
    View v;
    final Holder h;
        v = inflater.inflate(R.layout.lista_filmes, null);
        h = new Holder();
        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(new SwipeLayout.SwipeListener() {
        @Override
        public void onStartOpen(SwipeLayout layout) {

        }

        @Override
        public void onOpen(SwipeLayout layout) {

        }

        @Override
        public void onStartClose(SwipeLayout layout) {

        }

        @Override
        public void onClose(SwipeLayout layout) {

        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

        }
    });
    return v;
}


@Override
public void fillValues(int position, View convertView) {

}

}

Java adapter.

public class AdaptadorGenero extends BaseAdapter {
final Holder h = new Holder();
String[] nomeFilme;
int []imgFilme;
Context context;
AdaptadorFilmes adaptadorFilmesActivity;
private static LayoutInflater inflater = null;

public AdaptadorGenero(int tipoAdap,String[] nomeFilme, String[] nomeGenero, String[] precosProdutos, int[] imgFilme, int[] qtd, Context context) {
    this.nomeFilme = nomeFilme;
    this.imgFilme = imgFilme;
    this.context = context;
    this.adaptadorFilmesActivity =  new AdaptadorFilmes(context,imgFilme,nomeFilme);
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return imgFilme.length;
}

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

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

public class Holder{
    TextView nomeGenero;
    ImageView setinha;
    ListView listaFilmes;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    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();
    return v;
}

private void calculeHeightListView() {
    int totalHeight = 0;
    ListAdapter adapter = h.listaFilmes.getAdapter();
    int lenght = adapter.getCount();
    for (int i = 0; i < lenght; i++) {
        View listItem = adapter.getView(i, null, h.listaFilmes);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = h.listaFilmes.getLayoutParams();
    params.height = totalHeight
            + (h.listaFilmes.getDividerHeight() * (adapter.getCount() - 1));
    h.listaFilmes.setLayoutParams(params);
    h.listaFilmes.requestLayout();
}

}

The Year adapter follows the same idea of gender. How do I click on the arrow ListView "father" to ListView "child" is displayed/hidden?

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 do it based on the library android-advancedrecyclerview and I was changing what I needed for my project.

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

Fragment with Expandablelistview call and Adapter creation

ExpandableDraggableSwipeableExampleFragment

Adapter that controls Swipe (and also drag) that lets you delete the line after expanding ELV

ExpandableDraggableSwipeableExampleAdapter

Inside the Adapter you have specific methods to control the "father’s Swipe"

@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 "son"

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.