How to achieve Card Stack UI just like tinder? - android

I am not able to achieve proper UI for card stack.
Please check this picture this is requirement which I have to fulfill soon.
This is image for Card Stack UI please refer this
I had used https://github.com/aaronbond/Swipe-Deck too but UI is not as per requirement I tried to customize also but not success.
Your help will be highly appreciated
Thank you.

Use the below library which is working perfectly.
https://github.com/flschweiger/SwipeStack
XML file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<link.fls.swipestack.SwipeStack
android:id="#+id/swipeStack"
android:layout_width="320dp"
android:layout_height="240dp"
android:padding="32dp"/>
</FrameLayout>
Adapter Code
public class SwipeStackAdapter extends BaseAdapter {
private List<String> mData;
public SwipeStackAdapter(List<String> data) {
this.mData = data;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.card, parent, false);
TextView textViewCard = (TextView) convertView.findViewById(R.id.textViewCard);
textViewCard.setText(mData.get(position));
return convertView;
}
}

Related

Android Studio: Simple BaseAdapter Setup Fail

I would like to create a simple populated GridView using BaseAdapter but I keep getting empty list - application runs but there are no Views displayed.
Below are all files are use for this task:
Main:
String[] items = {"Some", "items", "to", "display"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView grid = (GridView)findViewById(R.id.grid);
final TextView text = (TextView)findViewById(R.id.text);
MyAdapter adapter = new MyAdapter(getApplicationContext(), items);
grid.setAdapter(adapter);
MyAdapter:
LayoutInflater inflater;
Context context;
String[] myData;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.myData = myData;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return myData[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.grid, parent, false);
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(myData[position]);
return convertView;
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.random.MainActivity">
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="40dip"
android:horizontalSpacing="5dip"
android:numColumns="auto_fit"
android:columnWidth="100dip"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</android.support.constraint.ConstraintLayout>
grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
I have checked in various sources countles times and I did not manage to find an error in my code. It would be amazing if someone would take a look at it and find a reason for empty activity as the reason is probably obvious.
Your getCount method always return 0. Return the number of elements in your myData array instead.
#Override
public int getCount() {
return myData.length;
}
You can try this my friend
public class MyAdapter extends BaseAdapter {
Context context;
String[] data;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.data = myData;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.grid, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(data[position]);
return convertView;
}
}

Android Custom ListView has no items

I tried to set up a custom ListView as a table to display Product Details. As you see in my Code I use a custom adapter and a additional xml file to populate the ListView. My problem is that the ListView ist empty. There are no items displayed and I do not see my mistake. Can you help me?
Fragment:
public class ProductDetail1Fragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_product_detail1,container,false);
HashMap<String,String> mapProduct = new HashMap<String,String>();
for(int i=0;i<10;i++) {
mapProduct.put("Key" + i, "Value" + i);
}
ListView listView=(ListView) view.findViewById(R.id.productListview);
ProductDetailAdapter adapter = new ProductDetailAdapter(getActivity(),mapProduct);
listView.setAdapter(adapter);
return view;
}
Adapter:
public class ProductDetailAdapter extends BaseAdapter {
private HashMap<String,String> list;
private Context context;
public ProductDetailAdapter(Context c, HashMap<String,String> list){
super();
this.context = c;
this.list=list;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView=inflater.inflate(R.layout.product_detail_data_row,null);
}
TextView textViewKey = (TextView)convertView.findViewById(R.id.productDataKey);
TextView textViewValue = (TextView)convertView.findViewById(R.id.productDataValue);
textViewKey.setText("tst");
textViewValue.setText("ddfadfs");
return convertView;
}
}
Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.parker.tfdeapp.ProductDetail2Fragment">
<ListView
android:id="#+id/productListview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Listview_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/productDataKey"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold" />
<TextView
android:id="#+id/productDataValue"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
Add getCount method in your adapter
#Override
public int getCount() {
return list.size();
}
Add this Override methods in your adapter class
#Override
public int getCount() {
return list.size();
}
#Override
public ListData getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
Missing class implementations of BaseAdapter.
See the documentation
#Override
public int getCount() {
return list.size();
}
#Override
public ListData getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
Implementing getCount() only makes items visible. Because it provides
item counts.
1) Need to change ListView to match parent height (its bad performance):
<ListView
android:id="#+id/productListview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2) Just to override missing functions in your BaseAdapte:
#Override
public int getCount() {
return mList.size();
}
#Override
public ListData getItem(int index) {
return mList.get(index);
}
#Override
public long getItemId(int index) {
return index;
}
3) Use ViewHolder... its very good pattern

Set image to ViewPager from List of Objects

I would like to make banner carousel. Instead of RecyclerView I choose to use ViewPager. I have a problems with adapter implementation:
public class BannerAdapter extends PagerAdapter {
List<Banner> bannerList;
Context context;
ImageView imageView;
public BannerAdapter(List<Banner> list, Context context) {
bannerList = list;
this.context = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = View.inflate(context, R.layout.fragment_home_layout, null);
imageView = (ImageView) v.findViewById(R.id.image);
Picasso.with(context).load(OpApp.IMAGEORGINAL + bannerList.get(position).getMobileImage() + ".jpg").placeholder(R.drawable.op_logo).into(imageView);
return v;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
}
#Override
public int getCount() {
return bannerList.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
}
The image is not loaded to the ViewPager.
fragment_home_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background">
<-- Other Views -->
<android.support.v4.view.ViewPager
android:id="#+id/home_banner_carousel"
android:layout_width="match_parent"
android:layout_height="#dimen/_150sdp"
android:adjustViewBounds="true"
>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.view.ViewPager>
<-- Other Views -->
</FrameLayout>
Actual result:
Expected result:
Additional to this question, what you can suggest to achieve position indicator functionality ? I think about implements ViewPager.OnPageChangeListener but not sure with that.
I know how to implement 'ViewPager' using 'Fragments' but in my case I need to use 'List' instead of 'Fragments', and I'm confused with that.
P.S. Please don't minus my question, just comment what you don't like and what is wrong and I will try to edit it. I'm newbie here and in android. Anyway, Thank you!
Solution founded, I have changed adapter`s 2 methods and all is working!
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.with(context).load(OpApp.IMAGEORGINAL + bannerList.get(position).getMobileImage() + ".jpg")
.placeholder(R.drawable.op_logo).into(imageView);
((ViewPager) container).addView(imageView);
return imageView;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}

Image slider using View Pager issue

I have an activity which contains a listView. Each item of the listView contains a swipeable set of images (which I have unsuccessfully tried to implement using a ViewPager).
My issue is this: when I try to implement a simple image slider using a view pager (i.e. Activity contains a ViewPager, and the View Pager's adapter supplies the images), the output is as expected, but if I try doing what I have mentioned in the previous paragraph (i.e. The Activity contains a listView and each item of the listView is a ViewPager which displays a swipeable set of images), I get a blank output. Please help me out! I have posted some code below:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.stylistDisplay);
//To keep things simple I am sending only one item of the list to the adapter
list.setAdapter(new StylistAdapter(Cart.getList().get(0), this));
}
static class StylistAdapter extends BaseAdapter {
Stylist stylistObj;
Context context;
LayoutInflater inflater;
public StylistAdapter(Stylist obj, Context context) {
this.stylistObj = obj;
this.context = context;
inflater = ((Activity)this.context).getLayoutInflater();
}
#Override
public int getCount() {
return 1;
}
#Override
public Object getItem(int position) {
return stylistObj;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewItem item;
if (convertView == null) {
convertView = inflater.inflate(R.layout.stylist_photos_view_pager, null);
item = new ViewItem();
item.photosViewPager = (ViewPager) convertView.findViewById(R.id.photos_view_pager);
convertView.setTag(item);
} else {
item = (ViewItem) convertView.getTag();
}
PhotosAdapter adapter = new PhotosAdapter(context);
item.photosViewPager.setAdapter(adapter);
return convertView;
}
private class ViewItem {
ViewPager photosViewPager;
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.temp.customer.MainActivity">
<ListView
android:id="#+id/stylistDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:background="#color/backGround"
android:padding="13.3dp"
android:clickable="true"
android:dividerHeight="5dp" />
</FrameLayout>
stylist_photos_view_pager
<RelativeLayout xmlns:android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/photos_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
PhotosAdapter.java
public class PhotosAdapter extends PagerAdapter {
private Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
public PhotosAdapter(Context context) {
this.context = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View viewItem = inflater.inflate(R.layout.stylist_individual_details, container, false);
ImageView imageView = (ImageView) viewItem.findViewById(R.id.iv1);
imageView.setImageResource(GalImages[position]);
TextView textView1 = (TextView) viewItem.findViewById(R.id.tv1);
textView1.setText("hello world");
((ViewPager)container).addView(viewItem);
return viewItem;
}
#Override
public int getCount() {
return 3;
}
#Override
public boolean isViewFromObject(View view, Object object) {
boolean temp = view == ((LinearLayout) object);
return temp;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((LinearLayout) object);
}
}
I've experienced similar issue, and have to say that it is a bad idea to use ViewPager as a listItem, since ViewPager is supposed to be used for fragments primarily.
I guess that your functionality should be similar to some horizontal pictures, on the main feed which is vertical. You can think of using horizontal scroll views which is a bit of a pain, but still more realistic to do that. You might also end up managing your own scrolling behavior, which might already be implemented by some libraries.
BUT I MIGHT BE WRONG!
This thread has a similar issue:
Placing ViewPager as a row in ListView
After reading around for a while I tried explicitly setting the height of the viewPager and its parent. This worked for me.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip" >
</android.support.v4.view.ViewPager>
</LinearLayout>
I don't know if there is a better solution, but if you do get a better solution please comment!

Custom GridView scrolling issue in Android

I am having a custom GridView which displays data dynamically. The data is loaded, but scrolling seems not working. When I scroll down the gridview, the scroll bar moves up without retaining the scrolled position.
Following is my code for Custom GridView,
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<PropertiesList> myList;
public CustomAdapter(List<PropertiesList> paramList, Context paramContext)
{
this.myList = paramList;
this.context = paramContext;
}
public int getCount()
{
return this.myList.size();
}
public Object getItem(int paramInt)
{
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt)
{
return paramInt;
}
public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup)
{
View localView = View.inflate(this.context, R.layout.grid_content, null);
localView.setMinimumHeight((int)(0.3D * ((WindowManager)this.context.getSystemService("window")).getDefaultDisplay().getWidth()));
((TextView)localView.findViewById(R.prop_type)).setText(((PropertiesList)this.myList.get(paramInt)).PropertyAddress);
localView.findViewById(R.id.prop_type).setBackgroundResource(color.darker_gray);
((TextView)localView.findViewById(R.id.prop_price)).setText(((PropertiesList)this.myList.get(paramInt)).PropertyPrice);
localView.setId(paramInt);
((TextView)localView.findViewById(R.id.prop_type)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "PropertyID: "+myList.get(paramInt).PropertyID, Toast.LENGTH_SHORT).show();
}
});
return localView;
}
}
xml,
<GridView
android:id="#+id/camera_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:columnWidth="110.0dip"
android:gravity="center"
android:horizontalSpacing="5.0dip"
android:numColumns="auto_fit"
android:orientation="vertical"
android:scrollingCache="false"
android:stretchMode="columnWidth"
android:verticalSpacing="5.0dip"
android:scrollbars="vertical" />
Any wrong implementation in my code? Please help me out in solving this.
Thanks in advance!!
public Object getItem(int paramInt)
{
return Integer.valueOf(paramInt);
}
this method return statement change to
mylist.get(paramInt);`

Categories

Resources