Custom GridView scrolling issue in Android - 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);`

Related

Next switch in ListView get also checked when checked the previous one

I have a Todo project in Android using a ListView. Each item of my listview have one switch in order to delete the task, one TextView and one ImageView. The problem is when I check a switch of a specific task, the task is deleted (desired behavior) but it checks the following one too (without deleting it) .
Here is the code of my Adapter:
public class Adapter extends BaseAdapter {
private List<Tache> lesTaches;
private Context context;
private LayoutInflater inflater;
public Adapter(Context context, List<Tache> list) {
this.context = context;
lesTaches = list;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return lesTaches.size();
}
#Override
public Object getItem(int position) {
return lesTaches.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
view = (View) inflater.inflate(R.layout.tache_item, parent, false);
} else {
view = (View) convertView;
}
TextView intitule = (TextView) view.findViewById(R.id.intitule);
intitule.setText(lesTaches.get(position).getIntitule());
final Switch switch1 = (Switch) view.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.isPressed()) {
if(isChecked) {
remove(lesTaches.get(position));
}
}
}
});
return view;
}
public void remove(Tache toDel) {
lesTaches.remove(toDel);
notifyDataSetChanged();
}
public void addItem(Tache toAdd) {
lesTaches.add(toAdd);
notifyDataSetChanged();
}}
And here is my tache_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginHorizontal="5dp"
android:focusable="false" />
<TextView
android:id="#+id/intitule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/switch1"
android:layout_toRightOf="#+id/switch1" />
<ImageView
android:id="#+id/icon_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:layout_marginHorizontal="5dp"
app:srcCompat="#drawable/ic_info_black" />
</RelativeLayout>
Here is a screen of my app showing the problem (I deleted the first task so we do not see it but the second one get checked):
Image showing the problem:
I already search on the net and test to replace the setOnCheckedChangeListener by an onClickListener but the same problem appear.

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

RecyclerView behaviour issue in nestedscrollview

I have a nested scrollview as parent and 2 recyclerviews as its children. What my issue is the recycler view draws it's children at one shot instead of drawing on scroll. How can I prevent this. I read that if we add
android:nestedScrollingEnabled="false"
property this issue comes. But I added this property to make the scroll smoother. Given below is my xml file.
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_fafafa"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false"
android:paddingLeft="#dimen/dp_5"
android:paddingRight="#dimen/dp_5"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Can anybody tell me how can I solve this issue?
If you only have 2 RecyclerViews as children I would suggest removing one and and using RecyclerViews ViewType.
Use an Adapter like this:
public class ExampleAdapter extends RecyclerView.Adapter<BindableViewHolder> {
private static final int VIEW_TYPE_CLASS_A = 0;
private static final int VIEW_TYPE_CLASS_B = 1;
private List<ClassA> class_a_list;
private List<ClassB> class_b_list;
public ExampleAdapter(List<ClassA> class_a_list, List<ClassB> class_b_list) {
this.class_a_list = class_a_list;
this.class_b_list = class_b_list;
}
#Override
public BindableViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case VIEW_TYPE_CLASS_A:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_class_a, parent, false);
return new ClassAHolder(view);
default:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_class_b, parent, false);
return new ClassBHolder(view);
}
}
#Override
public void onBindViewHolder(BindableViewHolder holder, int position) {
if(position < class_a_list.size()) {
((ClassAHolder) holder).bind(class_a_list.get(position));
} else {
((ClassBHolder) holder).bind(class_b_list.get(position - class_a_list.size()));
}
}
#Override
public int getItemCount() {
return class_a_list.size() + class_b_list.size();
}
#Override
public int getItemViewType(int position) {
if(position < class_a_list.size()) {
return VIEW_TYPE_CLASS_A;
} else {
return VIEW_TYPE_CLASS_B;
}
}
}
With the help of getItemViewType(int position) you determine which kind of View there should be in the specific position.
Then you can use ViewHolders like these:
public abstract class BindableViewHolder<T> extends RecyclerView.ViewHolder {
public BindableViewHolder(View itemView) {
super(itemView);
}
public abstract void bind(T data);
}
public class ClassAHolder extends BindableViewHolder<ClassA> {
public ClassAHolder(View itemView) {
super(itemView);
}
#Override
public void bind(ClassA data) {
// populate your views
}
}
public class ClassBHolder extends BindableViewHolder<ClassB> {
public ClassBHolder(View itemView) {
super(itemView);
}
#Override
public void bind(ClassB data) {
// populate your views
}
}

How to achieve Card Stack UI just like tinder?

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

How to add a group of horizontally swipeable items at a particular position inside a vertical listview? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the right procedure to implement a swipeable view as a row item in a vertical listview?
I tried googling, but I haven't found a good way of implementing this.
Here is a screenshot of my requirement.
In my opinion, the correct way to handle this behaviour is to override getViewTypeCount() and getViewItemType(), where the latter should return as type a normal and the swipeable, probably a ViewPager. The drawing part is not that tricky. I would rather expect issues in the Vertical/Horizontal scroll
Here is an example using ViewPager inside ListView. (Seems to work fine.)
I've also tried FragmentPagerAdapter and Fragments within the ListView, but that produced odd results.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.listView);
MultiLayoutAdapter adapter = new MultiLayoutAdapter();
adapter.addItem(new Content("First", Content.TYPE_TEXT));
adapter.addItem(new Content("Second", Content.TYPE_TEXT));
adapter.addItem(new Content("Imageset 1", Content.TYPE_IMAGESET));
adapter.addItem(new Content("Third", Content.TYPE_TEXT));
adapter.addItem(new Content("Imageset 2", Content.TYPE_IMAGESET));
adapter.addItem(new Content("Fourth", Content.TYPE_TEXT));
listview.setAdapter(adapter);
}
/* Simple class for multi-type content */
private class Content {
private static final int TYPE_TEXT = 0;
private static final int TYPE_IMAGESET = 1;
private static final int TYPE_COUNT = TYPE_IMAGESET + 1;
private String mString;
private int mType;
public Content(String s, int type) {
mString = s;
mType = type;
}
public String getString() { return mString; }
public int getType() { return mType; }
}
/* Adapter supporting multiple layouts */
private class MultiLayoutAdapter extends BaseAdapter {
private ArrayList<Content> mData = new ArrayList<Content>();
private LayoutInflater mInflater;
public MultiLayoutAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final Content item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) { return mData.get(position).getType(); }
#Override
public int getViewTypeCount() { return Content.TYPE_COUNT; }
#Override
public int getCount() { return mData.size(); }
#Override
public String getItem(int position) { return mData.get(position).getString(); }
#Override
public long getItemId(int position) { return position; }
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = mInflater.inflate(R.layout.row_text, parent, false);
/* TODO: Recycle convertView! */
switch (getItemViewType(position)) {
case Content.TYPE_TEXT:
rowView = mInflater.inflate(R.layout.row_text, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
textView.setText(mData.get(position).getString());
break;
case Content.TYPE_IMAGESET:
rowView = mInflater.inflate(R.layout.row_imageset, parent, false);
ViewPager viewPager = (ViewPager) rowView.findViewById(R.id.viewPager);
viewPager.setAdapter(new ImagePagerAdapter(getApplicationContext()));
break;
}
return rowView;
}
}
}
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
int NumberOfPages = 3;
LayoutInflater mInflater;
int[] res = {
android.R.drawable.ic_menu_camera,
android.R.drawable.ic_menu_compass,
android.R.drawable.ic_menu_directions};
ImagePagerAdapter(Context c) {
mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() { return NumberOfPages; }
#Override
public boolean isViewFromObject(View view, Object object) { return view == object; }
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mInflater.inflate(R.layout.image, container, false);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(res[position]);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
And here are the layouts for reference:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingLeft="16dp" android:paddingRight="16dp">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</RelativeLayout>
row_text.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="50dp"
android:id="#+id/textView"/>
</LinearLayout>
row_imageset.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</LinearLayout>
and image.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
</LinearLayout>

Categories

Resources