Skip to content

Commit

Permalink
listview card implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-vasiliev committed Mar 16, 2017
1 parent 53fcd1a commit daae27c
Show file tree
Hide file tree
Showing 18 changed files with 321 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,34 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ramotion.expandingcollection.ECCardContentListAdapter;

import java.util.List;

import ramotion.com.expandingcollection.examples.simple.R;

public class CommentsArrayAdapter extends ArrayAdapter<CommentPOJO> {
public class CommentsArrayAdapter extends ECCardContentListAdapter<CommentPOJO> {

private final Context context;
private final CommentPOJO[] values;
public CommentsArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<CommentPOJO> objects) {
super(context, resource, objects);
}

public CommentsArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull CommentPOJO[] values) {
super(context, resource, values);
this.context = context;
this.values = values;
@Override
public boolean isEnabled(int position) {
return false;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

ViewHolder viewHolder;
View rowView = convertView;

if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(getContext());

rowView = inflater.inflate(R.layout.list_element, null);
// configure view holder
Expand All @@ -45,7 +47,7 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
viewHolder = (ViewHolder) rowView.getTag();
}

CommentPOJO objectItem = values[position];
CommentPOJO objectItem = getItem(position);

if (objectItem != null) {
viewHolder.line1.setText(objectItem.getCommentPersonName());
Expand All @@ -62,8 +64,4 @@ static class ViewHolder {
ImageView icon;
}

@Override
public int getCount() {
return values.length;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.ramotion.expandingcollection.examples.simple;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.StrictMode;
Expand All @@ -10,19 +9,19 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ramotion.expandingcollection.ECCardData;
import com.ramotion.expandingcollection.ECPagerAdapter;
import com.ramotion.expandingcollection.ECPagerViewAdapter;
import com.ramotion.expandingcollection.ECBackgroundView;
import com.ramotion.expandingcollection.ECPagerCardContentList;
import com.ramotion.expandingcollection.ECPagerView;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ramotion.com.expandingcollection.examples.simple.R;
Expand All @@ -48,56 +47,48 @@ protected void onCreate(Bundle savedInstanceState) {
List<ECCardData> exampleDataSet = getExampleDataSet(displaySize.x, displaySize.y);

// Create adapter for pager
ECPagerAdapter adapter = new ECPagerAdapter(this, exampleDataSet) {
ECPagerViewAdapter adapter = new ECPagerViewAdapter(this, exampleDataSet) {

@Override
public void instantiateCard(LayoutInflater inflaterService, ViewGroup cardHead, ViewGroup cardBody, ECCardData data) {
PagerCardDataPOJO cardDataHolder = (PagerCardDataPOJO) data;

// inflate custom layout and place it in card body
// inflaterService.inflate(R.layout.simple_body, cardBody);
//
// ImageView testListHeader = new ImageView(getApplicationContext());
// testListHeader.setBackgroundColor(Color.RED);
// testListHeader.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));
//
// ListView commentsList = (ListView) cardBody.findViewById(R.id.body_list_view);
// commentsList.addHeaderView(testListHeader);
// commentsList.setAdapter(new CommentsArrayAdapter(getApplicationContext(), R.layout.list_element, ((PagerCardDataPOJO) data).getComments()));

// do the same for header
inflaterService.inflate(R.layout.simple_head, cardHead);

TextView title = (TextView) cardHead.findViewById(R.id.title);
public void instantiateCard(LayoutInflater inflaterService, ViewGroup head, ECPagerCardContentList list, final ECCardData data) {
final PagerCardDataPOJO cardDataHolder = (PagerCardDataPOJO) data;

// Create adapter for list inside a card and set adapter to card content
CommentsArrayAdapter commentsArrayAdapter = new CommentsArrayAdapter(getApplicationContext(), R.layout.list_element, cardDataHolder.getComments());
list.setEcArrayAdapter(commentsArrayAdapter);

// Inflate header layout and attach it to header
inflaterService.inflate(R.layout.simple_head, head);

// Set header data
TextView title = (TextView) head.findViewById(R.id.title);
title.setText(cardDataHolder.getHeadTitle());

ImageView avatar = (ImageView) cardHead.findViewById(R.id.avatar);
ImageView avatar = (ImageView) head.findViewById(R.id.avatar);
avatar.setImageDrawable(cardDataHolder.getPersonPicture());

TextView name = (TextView) cardHead.findViewById(R.id.name);
TextView name = (TextView) head.findViewById(R.id.name);
name.setText(cardDataHolder.getPersonName());

TextView message = (TextView) cardHead.findViewById(R.id.message);
TextView message = (TextView) head.findViewById(R.id.message);
message.setText(cardDataHolder.getPersonMessage());

TextView viewsCount = (TextView) cardHead.findViewById(R.id.socialViewsCount);
TextView viewsCount = (TextView) head.findViewById(R.id.socialViewsCount);
viewsCount.setText(" " + cardDataHolder.getPersonViewsCount());

TextView likesCount = (TextView) cardHead.findViewById(R.id.socialLikesCount);
TextView likesCount = (TextView) head.findViewById(R.id.socialLikesCount);
likesCount.setText(" " + cardDataHolder.getPersonLikesCount());

TextView commentsCount = (TextView) cardHead.findViewById(R.id.socialCommentsCount);
TextView commentsCount = (TextView) head.findViewById(R.id.socialCommentsCount);
commentsCount.setText(" " + cardDataHolder.getPersonCommentsCount());

// add toggling card by tap on card head
cardHead.setOnClickListener(new View.OnClickListener() {
head.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
ecPagerView.toggle();
}
});


}
};

Expand All @@ -107,10 +98,10 @@ public void onClick(final View v) {

// Tune pager view
ecPagerView
.withCardSize(500, 600)
.withCardExpandedHeaderHeight(500)
.withCardSize(500, 550)
.withCardExpandedHeaderHeight(400)
.withBackgroundImageSwitcher(bgView)
.withPagerAdapter(adapter);
.withPagerViewAdapter(adapter);
}

@Override
Expand All @@ -122,6 +113,24 @@ public void onBackPressed() {
private List<ECCardData> getExampleDataSet(int imageWidth, int imageHeight) {
List<ECCardData> dataset = new ArrayList<>();

PagerCardDataPOJO item5 = new PagerCardDataPOJO();
item5.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item5.setHeadTitle("FiftH");
item5.setPersonMessage("Vae, nix!");
item5.setPersonName("Frederich Nilson");
item5.setPersonPicture(placePenguin(100, 100));
item5.setComments(prepareCommentsArray());
dataset.add(item5);

PagerCardDataPOJO item4 = new PagerCardDataPOJO();
item4.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item4.setHeadTitle("FOurTH");
item4.setPersonMessage("Celery can be covered with heated peanut butter.");
item4.setPersonName("Peter Jackson");
item4.setPersonPicture(placePenguin(100, 100));
item4.setComments(prepareCommentsArray());
dataset.add(item4);

PagerCardDataPOJO item3 = new PagerCardDataPOJO();
item3.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item3.setHeadTitle("AWESOMENESS");
Expand Down Expand Up @@ -152,14 +161,19 @@ private List<ECCardData> getExampleDataSet(int imageWidth, int imageHeight) {
return dataset;
}

private CommentPOJO[] prepareCommentsArray() {
return new CommentPOJO[]{
new CommentPOJO(placePenguin(100, 100), "Bob Marley", "No womans, no cry", "9 apr. 2014"),
new CommentPOJO(placePenguin(100, 100), "John Avon", "For Zendikar!", "12 jan. 2014"),
new CommentPOJO(placePenguin(100, 100), "Rebecca Guay", "Faeries are awesome", "1 jun. 2015"),
new CommentPOJO(placePenguin(100, 100), "Peter Jackson", "There and Back Again", "21 sep. 1937")
};

private List<CommentPOJO> prepareCommentsArray() {
return Arrays.asList(new CommentPOJO(placePenguin(100, 100), "CASEY AFFLECK", "Manchester by the Sea", "9 apr. 2014"),
new CommentPOJO(placePenguin(100, 100), "MAHERSHALA ALI", "Moonlight", "12 jan. 2014"),
new CommentPOJO(placePenguin(100, 100), "EMMA STONE", "La La Land", "1 jun. 2015"),
new CommentPOJO(placePenguin(100, 100), "VIOLA DAVIS", "Fences", "21 sep. 1937"),
new CommentPOJO(placePenguin(100, 100), "ZOOTOPIA", "Byron Howard, Rich Moore and Clark Spencer", "21 sep. 1937"),
new CommentPOJO(placePenguin(100, 100), "LA LA LAND", "Linus Sandgren", "21 sep. 1937"),
new CommentPOJO(placePenguin(100, 100), "THE WHITE HELMETS", "Orlando von Einsiedel and Joanna Natasegara", "21 sep. 1937"),
new CommentPOJO(placePenguin(100, 100), "LA LA LAND", "Damien Chazelle", "27 dec. 1952"),
new CommentPOJO(placePenguin(100, 100), "HACKSAW RIDGE", "John Gilbert", "21 sep. 1937"),
new CommentPOJO(placePenguin(100, 100), "THE SALESMAN", "Iran", "22 sep. 1938"),
new CommentPOJO(placePenguin(100, 100), "SUICIDE SQUAD", "Alessandro Bertolazzi, Giorgio Gregorini and Christopher Nelson", "13 nov. 1964"),
new CommentPOJO(placePenguin(100, 100), "LA LA LAND", "Justin Hurwitz", "11 oct. 1987"));
}

public Drawable placePenguin(int w, int h) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import com.ramotion.expandingcollection.ECCardData;

import java.util.Collection;
import java.util.List;
import java.util.Random;

public class PagerCardDataPOJO implements ECCardData {
public class PagerCardDataPOJO implements ECCardData<CommentPOJO> {

private String headTitle;
private Drawable headBgImageDrawable;
Expand All @@ -18,7 +20,7 @@ public class PagerCardDataPOJO implements ECCardData {
private int personViewsCount;
private int personCommentsCount;
private int personLikesCount;
private CommentPOJO[] comments;
private List<CommentPOJO> comments;

public PagerCardDataPOJO() {
Random rnd = new Random();
Expand Down Expand Up @@ -105,11 +107,12 @@ public void setPersonLikesCount(int personLikesCount) {
this.personLikesCount = personLikesCount;
}

public CommentPOJO[] getComments() {
@Override
public List<CommentPOJO> getComments() {
return comments;
}

public void setComments(CommentPOJO[] comments) {
public void setComments(List<CommentPOJO> comments) {
this.comments = comments;
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="100dp"
android:padding="15dip">

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true" />

<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:id="@+id/firstLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/icon"
android:layout_toEndOf="@id/icon"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:maxLines="1"
android:text="Description"
android:textSize="12sp" />
android:text="Example application" />

<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:id="@+id/secondLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_alignLeft="@id/firstLine"
android:layout_alignStart="@id/firstLine"
android:layout_below="@id/firstLine"
android:layout_toEndOf="@id/icon"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
android:text="Description" />

</RelativeLayout>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ramotion.expandingcollection;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.util.List;

public abstract class ECCardContentListAdapter<T> extends ArrayAdapter<T> {
private boolean zeroItemsMode = true;

public ECCardContentListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<T> objects) {
super(context, resource, objects);
}

@Override
public final int getCount() {
return zeroItemsMode ? 0 : super.getCount();
}

@NonNull
@Override
public abstract View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent);

protected final void enableZeroItemsMode() {
this.zeroItemsMode = true;
notifyDataSetChanged();
}

protected final void disableZeroItemsMode() {
this.zeroItemsMode = false;
notifyDataSetChanged();
}

}
Loading

0 comments on commit daae27c

Please sign in to comment.