I implemented a viewPager inside my MotionLayout view, and created the Adapter and set the adapter.
Here is the MotionLayout view:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="#+id/bottom_component"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#drawable/bottom_sheet_bg"
android:clickable="true"
android:elevation="10dp"
android:minHeight="#dimen/_290sdp"
app:behavior_hideable="true"
app:behavior_peekHeight="#dimen/_124sdp"
app:layoutDescription="#xml/bottom_sheet_scene"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<View
android:id="#+id/slider"
android:layout_width="50dp"
android:layout_height="4dp"
android:layout_marginTop="#dimen/_4sdp"
android:background="#drawable/slider_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/placeName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_18sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginEnd="#dimen/_8sdp"
android:fontFamily="#font/janna_bold"
android:gravity="start|center_vertical"
android:textColor="#464954"
android:textSize="#dimen/_15sdp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#id/threeDots"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/slider"
tools:text="Irsal Gas Station" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/placeLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_5sdp"
android:fontFamily="#font/janna_regular"
android:text="Ramallah and Al Bireh - Al Irsal"
android:textColor="#B3B3C6"
android:textSize="#dimen/_12sdp"
app:layout_constraintEnd_toEndOf="#id/threeDots"
app:layout_constraintStart_toStartOf="#id/placeName"
app:layout_constraintTop_toBottomOf="#id/placeName" />
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/threeDots"
android:layout_width="#dimen/_20sdp"
android:layout_height="#dimen/_24sdp"
android:layout_marginEnd="#dimen/_18sdp"
android:tint="#color/blueText"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="#id/placeName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#id/placeName"
app:srcCompat="#drawable/ic_three_dots" />
<LinearLayout
android:id="#+id/routesContainer"
android:layout_width="match_parent"
android:layout_height="#dimen/_70sdp"
android:layout_marginTop="#dimen/_7sdp"
app:layout_constraintTop_toBottomOf="#id/placeLocation">
<androidx.viewpager.widget.ViewPager
android:id="#+id/routes"
android:layout_width="match_parent"
android:background="#EEEEEE"
android:elevation="10dp"
android:layout_height="match_parent" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/addedStop"
android:layout_width="#dimen/_18sdp"
android:layout_height="#dimen/_18sdp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="#id/routes"
app:layout_constraintTop_toBottomOf="#id/routes"
app:srcCompat="#drawable/ic_plus" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/stops"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/janna_regular"
android:gravity="center_vertical"
android:text="0 Stops"
android:textColor="#464954"
android:textSize="#dimen/_11sdp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="#id/addedStop"
app:layout_constraintStart_toEndOf="#id/addedStop"
app:layout_constraintTop_toTopOf="#id/addedStop" />
<LinearLayout
android:id="#+id/actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_9sdp"
android:orientation="horizontal"
android:paddingVertical="10dp"
android:weightSum="4"
app:layout_constraintTop_toBottomOf="#id/addedStop">
<com.ideal.doroob.custom.ActionButton
android:id="#+id/addStop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:icon="#drawable/ic_addstop"
app:title="#string/AddStop" />
<com.ideal.doroob.custom.ActionButton
android:id="#+id/share"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:icon="#drawable/ic_share"
app:title="#string/Share" />
<com.ideal.doroob.custom.ActionButton
android:id="#+id/overview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:icon="#drawable/ic_overview"
app:title="#string/Overview" />
<com.ideal.doroob.custom.ActionButton
android:id="#+id/addToFavorite"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:icon="#drawable/ic_favorite"
app:title="#string/Favorite" />
</LinearLayout>
<LinearLayout
android:id="#+id/button_sticky"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="#dimen/_30sdp"
android:layout_marginEnd="#dimen/_3sdp"
android:layout_weight="1"
android:background="#drawable/button_bg"
android:fontFamily="#font/janna_regular"
android:gravity="center"
android:text="#string/cancel"
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/startNavigation"
android:layout_width="0dp"
android:layout_height="#dimen/_30sdp"
android:layout_marginStart="#dimen/_3sdp"
android:layout_weight="2"
android:background="#drawable/button_bg"
android:backgroundTint="#7ED321"
android:fontFamily="#font/janna_bold"
android:gravity="center"
android:text="#string/go"
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
</LinearLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
And the adapter is:
package com.ideal.doroob.adapters;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.ideal.doroob.data.RouteDetails;
import com.ideal.doroob.databinding.RouteTypeItemBinding;
import java.util.List;
public class RoutesAdapter extends PagerAdapter {
List<RouteDetails> routeDetailsList;
public RoutesAdapter(List<RouteDetails> routeDetails) {
this.routeDetailsList = routeDetails;
}
#Override
public int getCount() {
return 3;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
RouteDetails routeDetails = routeDetailsList.get(position);
RouteTypeItemBinding binding = RouteTypeItemBinding.inflate(
LayoutInflater.from(container.getContext()));
binding.distance.setText(routeDetails.getDistance() + " " + routeDetails.getUnit());
binding.time.setText(routeDetails.getHours() + ":" + routeDetails.getMinutes());
// ViewPager.LayoutParams params = new ViewPager.LayoutParams();
// params.width = MATCH_PARENT;
// params.height = MATCH_PARENT;
// binding.getRoot().setLayoutParams(params);
container.addView(binding.getRoot(), 0);
return binding.getRoot();
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
public void replaceRoutes(List<RouteDetails> details) {
routeDetailsList.clear();
routeDetailsList.addAll(details);
}
}
The view pager is shown but the items are not shown inside the view pager.
I checked the layout inspector and the views are added to the view pager, but they are not showing in the UI
I tried to add the view pager inside my activity and it worked well, so I guess the issue is in the MotionLayout.
Any idea ?
Related
I have a problem with item height and gravity in a horizontal recyclerview as you can see in the picture: RecyclerView item height and gravity not working
How to set the item height to same height as recyclerciew and the gravity at the bottom of recyclerview ?
Thank you for your help.
My code for Activity_Drill_Graph.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=".Drill_Graph_Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar_drill_graph"
layout="#layout/toolbar_drill_graph" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/grey"
android:gravity="bottom"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:orientation="vertical"
android:padding="#dimen/padding_5dp">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_10"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_9"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_8"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_7"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_6"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_5"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_4"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_3"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_2"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_1"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_0"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textViewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/date"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewDrillGraph"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/white"
android:orientation="horizontal"
android:padding="#dimen/padding_5dp"
android:scrollbars="horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
item code: drill_graph_single_column.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:gravity="bottom"
android:orientation="vertical"
android:padding="#dimen/padding_1dp"
android:weightSum="110">
<Button
android:id="#+id/btnSkill"
style="#style/btnStyleScoreGrey"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:text="#string/skill"
android:textAllCaps="false"
android:textSize="16sp" />
<Button
android:id="#+id/btnDate"
style="#style/btnStyleDrillGraphGrey"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin_1dp"
android:layout_weight="10"
android:text="#string/date"
android:textAllCaps="false"
android:textSize="12sp" />
</LinearLayout>
My code for RecyclerView Adapter:
package org.poolshot.poolshotacademy;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.List;
public class My_DrillGraph_RecyclerView_Adapter extends RecyclerView.Adapter<My_DrillGraph_RecyclerView_Adapter.MyHolder> {
SharedPreferences defaultPreferencesSettings;
private Context myRCAContext;
private List<String> myDrillGraphDates;
private List<Integer> myDrillGraphSkill;
// constructor
public My_DrillGraph_RecyclerView_Adapter(Context applicationContext, List<String> mDrillGraphDates, List<Integer> mDrillGraphSkill) {
this.myRCAContext = applicationContext;
this.myDrillGraphDates = mDrillGraphDates;
this.myDrillGraphSkill = mDrillGraphSkill;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int position) {
View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drill_graph_single_column,null);
MyHolder myHolder = new MyHolder(layout);
return myHolder;
}
#Override
public void onBindViewHolder(#NonNull final MyHolder viewHolder, final int position) {
String skill = myDrillGraphSkill.get(position).toString();
// set color for buttons
viewHolder.btnDate.setBackground(myRCAContext.getResources().getDrawable(R.drawable.custom_btn_grey));
viewHolder.btnSkill.setTextColor(myRCAContext.getResources().getColor(R.color.white));
// set text for buttons
viewHolder.btnDate.setText(myDrillGraphDates.get(position));
viewHolder.btnSkill.setText(skill);
}
#Override
public int getItemCount() {
return myDrillGraphDates.size();
}
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
}
}
}
This is my solution :
I changed the code of the class MyHolder as :
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.BOTTOM;
itemView.setLayoutParams(layoutParams);
}
}
I am working with com.beardedhen.androidbootstrap.BootstrapDropDown and I don't understand how to get the value of the selected item from the dropdown
dropdownresource.xml
`<resources>
<string name="app_name">GTFirstPage</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string>
<array name="bootstrap_dropdown_example_data">
<item>Family Trip</item>
<item>Camping</item>
<item>Business Trip</item>
<item>{dropdown_separator}</item>
<item>Custom Trip</item>
</array>
</resources>`
plan.xml
<FrameLayout 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.hp.gtfirstpage.plantrip">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/autotv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal|top"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:hint="Where do you want to go?"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/textView2"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Destination"
android:textSize="30sp" />
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:ems="10"
android:textSize="20sp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.00"
android:text="Category"
android:textSize="30sp"/>
<com.beardedhen.androidbootstrap.BootstrapDropDown
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapBrand="regular"
app:bootstrapExpandDirection="down"
app:bootstrapSize="md"
app:bootstrapText="Select Category"
app:dropdownResource="#array/bootstrap_dropdown_example_data"
app:roundedCorners="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Date"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tv4"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
<TextView
android:id="#+id/textView14"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="to"
android:textSize="30sp" />
<TextView
android:id="#+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="#+id/bt1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="BACK" />
<Button
android:id="#+id/bt2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="DONE" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
plantrip.java
package com.example.hp.gtfirstpage;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import android.widget.Toast;
import com.beardedhen.androidbootstrap.BootstrapDropDown;
public class plantrip extends Fragment implements
AdapterView.OnItemSelectedListener, DatePickerDialog.OnDateSetListener
{
BootstrapDropDown spin;
String city,category,dest;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View root=null;
root= inflater.inflate(R.layout.fragment_plantrip, container, false);
spin = (BootstrapDropDown) root.findViewById(R.id.spinner);
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
}
});
return root;
}
}
I am new to android so if there is any mistake please explain properly.
Please tell me how to retrieve items from dropdown list.
Thank You!!!
BootstrapDropDown has a method called getDropdownData();
This method returns a String[] array containing the items in your BootstapDropDown. Use int id as an index for your array. The 'int id' gets passed to you as a parameter of the method:
public void onItemClick(ViewGroup parent, View v, int id) {...}
I believe you can grab the selected item by adding this line to your code:
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
}
});
// made a couple of edits to fix a mistake
// I hope this helped!
I have a spinner in my app where you can select categories and sub categories.
It was working perfect in the past but since i did alot of modifications in the app, the spinner does not scroll anymore, not just one spinner, but all spinners in this layout (Drawer menu).
Here's the XML for the right menu drawer that contains the spinner:
<?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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:rsb="http://schemas.android.com/apk/res-auto"
android:id="#+id/rightadvancedsearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Grey"
android:gravity="center"
tools:context="com.chno.v1.Home">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="16dp">
<RelativeLayout
android:id="#+id/AllCat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/query"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:backgroundTint="#color/Blue"
android:drawableLeft="#android:drawable/ic_menu_search"
android:ems="10"
android:hint="Search"
android:inputType="textPersonName"
android:paddingLeft="5dp"
android:textColor="#color/Black"
android:textColorHint="#color/GreyDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="6dp"
android:text="#string/Category"
android:textColor="#color/Blue"/>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="40dp"
android:textColor="#color/Blue" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/AllCat"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="6dp"
android:text="#string/Location"
android:textColor="#color/Blue" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/SelectCity" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<!--
<CheckBox
android:id="#+id/nearby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Nearby" />-->
<CheckBox
android:id="#+id/checkNearby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Nearby" />
<TextView
android:id="#+id/currentray"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/defaultRay"
android:textAlignment="textEnd"
android:visibility="gone" />
</LinearLayout>
<com.yahoo.mobile.client.android.util.rangeseekbar.RangeSeekBar
android:id="#+id/rangeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"
rsb:absoluteMaxValue="100"
rsb:absoluteMinValue="1"
rsb:singleThumb="true" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/includeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/city"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#color/BgLight">
<RelativeLayout
android:id="#+id/reset"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/GreyLight"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/Black"
android:layout_marginRight="12dp"
android:text="Ré-initialiser"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/Orange"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/imageView3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/White"
android:textStyle="bold"
android:text="Recherche"/>
</RelativeLayout>
</LinearLayout>
This is the spinner adapter:
package com.chno.v1;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
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 java.util.ArrayList;
import java.util.List;
/**
* List view
*/
public class SpinnerAdapter extends ArrayAdapter<ItemDate> {
private int groupid;
private Activity context;
private ArrayList<ItemDate> list;
private LayoutInflater inflater;
public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemDate> list) {
super(context,id, list);
this.list = list;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.groupid = groupid;
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(groupid,parent,false);
ImageView imageView = (ImageView)itemView.findViewById(R.id.img);
ItemDate item = list.get(position);
if(item != null) {
if(item.getType() != null) {
if (item.getType().equals("sub_category")) {
imageView.setVisibility(View.GONE);
} else {
imageView.setImageResource(item.getImageId());
}
} else {
imageView.setImageResource(item.getImageId());
}
} else {
imageView.setImageResource(item.getImageId());
}
imageView.setImageResource(item.getImageId());
TextView textView = (TextView)itemView.findViewById(R.id.txt);
textView.setText(item.getText());
return itemView;
}
public View getDropDownView(int position, View convertView, ViewGroup
parent) {
return getView(position,convertView,parent);
}
}
The thing is i can select items from the spinner but i cannot scroll it down for more items.
Here's the code that populates the spinner:
JSONObject result = config.getCategories();
Iterator<String> iter = result.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject cat = result.getJSONObject(key);
String category = cat.getString("n");
int drawable = helper.getCategoryDrawable(Integer.parseInt(key));
Log.e("" + category, "" + key);
list.add(new ItemDate("category", category, Integer.parseInt(key), drawable));
JSONObject subs = cat.getJSONObject("l");
Iterator<String> it2 = subs.keys();
while (it2.hasNext()) {
String sub_key = it2.next();
JSONObject sub_category = subs.getJSONObject(sub_key);
String name = sub_category.getString("n");
Log.e("FFROM LIST", "" + name);
list.add(new ItemDate("sub_category", name, Integer.parseInt(sub_key), Integer.parseInt(key), R.drawable.tool_icon_filter));
}
} catch (JSONException e) {
Log.e("JSONException", e.getMessage());
e.printStackTrace();
}
}
Spinner sp=(Spinner)findViewById(R.id.spinner1);
SpinnerAdapter adapter=new SpinnerAdapter((Activity) context, R.layout.sub_select_item, R.id.txt, list);
sp.setAdapter(adapter);
sp.setFocusable(true);
sp.setFocusableInTouchMode(true);
How can i solve this? Thanks.
when I scroll the listview the items changed automatically, in other word an item override other item's view like : when I click on favorite button other items has been changed like this
example:
the item that I clicked
I didn't click on this item
here is my code :
CustomListAdapter.java :
public class CustomListAdapter extends BaseAdapter {
private Context context;
LayoutInflater inflater;
boolean isVoter;
public CustomListAdapter(Context c ) {
this.context = c;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.date_view,null);
viewHolder = new ViewHolder();
viewHolder.vote = (ImageButton) convertView.findViewById(R.id.vote);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
isVoter = newsItemArray.get(position).isVoter();
viewHolder.vote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Utility.isNetworkAvailable(context))
Toast.makeText(context, R.string.no_network, Toast.LENGTH_LONG).show();
else
voteBtn( position);
}});
return convertView;
}
public void voteBtn(int position){
if ( !isVoter ) {
viewHolder.vote.setImageResource(R.drawable.button_pressed);
}
else{
viewHolder.vote.setImageResource(R.drawable.button_normal);
}
isVoter = !isVoter;
}
}
private class ViewHolder{
ImageButton vote ;
}
#Override
public int getCount() {
return newsItemArray.size();
}
#Override
public Object getItem(int position) {
return newsItemArray.get(position).getTitle();
}
#Override
public long getItemId(int position) {
return position;
}
}
date_view.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="wrap_content"
android:orientation="vertical"
android:background="#f2f2f2"
android:gravity="center"
android:elevation="8dp"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:id="#+id/newsCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/newsimage1"
android:layout_width="45px"
android:layout_height="45px"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/newsname"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fox News."
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/time"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 day ago"
android:textColor="#color/listsub1"
android:textSize="14dp"
android:layout_gravity="center"
/>
<ImageView
android:id="#+id/more"
android:visibility="invisible"
android:layout_width="15dp"
android:layout_height="22dp"
android:src="#drawable/more"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/sourceNews"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="5dp"
>
<TextView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous. Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous"
android:textSize="20dp"
android:textColor="#color/listtext"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/newssub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Why even a President Trump couldn’t make Apple manufacture iPhone in the state."
android:layout_marginTop="5dp"
android:textSize="13dp"
android:textColor="#color/listsub1"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/votes_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#f40000"
android:maxLines="1"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/vote"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star"
android:background="#00ffffff"
android:paddingLeft="5dp" />
<!--
<ImageButton
android:id="#+id/vote"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#drawable/star"
android:paddingLeft="5dp" />
<Button
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Share"
android:textColor="#color/background_material_light"/>
<Button
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Comment"
android:textColor="#color/background_material_light"/>
-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
isVoter and viewHolder should not be global variables within the Adapter. Meaning, you are not actually changing whatever member is returned by newsItemArray.get(position).isVoter()
Try adding the position to the ViewHolder object. Then pass the ViewHolder object into the constructor of the viewHolder.vote.setOnClickListener. Within the OnClickListener you can then call
isVoter = newsItemArray.get(viewHolder.position).isVoter();
newsItemArray.get(viewHolder.position).setVoter(!isVoter);
I am currently working with an Android App using RecyclerView and CardView .I am showing lists of UserPosts(Contains text and Image both) inside RecyclerView . The UserPost item has been created using CardView .
Edit
The problem I am facing here is onCreateViewHolder method is being called only half of the times the actual count returned by getItemCount method even I scrolls till end of the list .E.g I have a list of 10 posts then onCreateViewHolder calls for 5 times only . It should call 10 times (when I scroll till end of the list) first and then created views will be recycled . But it is not happening actually . I have visited many Stackoverflow posts but no luck .
Here is code UserPostAdapter.java :
package com.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.model.UserPostsListItem;
import com.musomeet.R;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by SI_Android on 10/21/2015 .
*
*/
public class UserPostAdapter extends RecyclerView.Adapter<UserPostAdapter.UserPostViewHolder> {
private List<UserPostsListItem> userPostList;
Context conetxt ;
LayoutInflater inflater ;
public UserPostAdapter(){
}
public UserPostAdapter(Context conetxt, List<UserPostsListItem> postlist) {
this.userPostList = postlist;
this.conetxt = conetxt ;
inflater = LayoutInflater.from(this.conetxt);
}
#Override
public int getItemCount() {
//int size = 0 ;
//if( userPostList != null )
//size = userPostList.size();
return userPostList.size();
}
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
videoViewHolder.userName.setText(item.getName());
videoViewHolder.userLocation.setText(item.getCity()+", "+item.getCountry());
videoViewHolder.timeAgo.setText(item.getAgo());
videoViewHolder.likes.setText(item.getNo_of_likes());
videoViewHolder.comments.setText(item.getNo_of_comments());
videoViewHolder.userMessage.setText(item.getContent());
if(!TextUtils.isEmpty(item.getPhoto())) {
Picasso.with(conetxt).load(item.getPhoto()).into(videoViewHolder.circleImageView);
}
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url)) {
if ((url.endsWith(".png") || url.endsWith(".jpg"))) {
//videoViewHolder.attachment.setVisibility(View.VISIBLE);
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
}
}
#Override
public UserPostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = inflater.inflate(R.layout.userposts_listitem_textmessage, viewGroup, false);
return new UserPostViewHolder(itemView);
}
public static class UserPostViewHolder extends RecyclerView.ViewHolder {
TextView userName, userLocation, timeAgo, likes, comments, userMessage;
ImageView attachment;
CircleImageView circleImageView;
public UserPostViewHolder(View v){
super(v);
attachment = (ImageView) v.findViewById(R.id.attachment);
circleImageView = (CircleImageView) v.findViewById(R.id.circleView);
userMessage = (TextView) v.findViewById(R.id.massage);
userName = (TextView) v.findViewById(R.id.name);
userLocation = (TextView) v.findViewById(R.id.location);
timeAgo = (TextView) v.findViewById(R.id.time);
likes = (TextView) v.findViewById(R.id.likespoints);
comments = (TextView) v.findViewById(R.id.commentspoints);
}
}
}
Here is the userposts_listitem_textmessage.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userpopstcardview"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
cardView:cardElevation="5dp"
cardView:cardCornerRadius="10dp"
cardView:cardBackgroundColor="#fff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact"
android:layout_margin="10dp"
android:id="#+id/circleView"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginLeft="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Jasmine"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="Los Angles, California"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="-"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="2 hours ago"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/userListContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:orientation="vertical">
<TextView
android:id="#+id/massage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/recet_text"
android:textColor="#000"
android:textSize="15sp"
android:padding="10dp"
android:scrollbars="vertical"/>
<ImageView
android:id="#+id/attachment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:tint="#00000000"
android:src="#drawable/contact"
android:visibility="visible"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/likespoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp">
<TextView
android:id="#+id/commentspoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="1dp"
android:layout_width="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/like"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Like"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Comment"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/share"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Share"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
What you see is an expected behaviour. RecylerView won't create as many view as adapter's getItemCount() returns. It create view as many as it's necessary for rendering a view.
For instance, if your adapter contains 100 elements, but only 5 are displayed at at time, system will create 5 views (it may create 1 or 2 more view as "buffer") and reuse these views when user scrolls.
When a View is recycled, it doesn't get "reset" to its original state, so it and its child Views still have the attributes that were set on them the last time the ViewHolder was passed into onBindViewHolder(). Since you are currently only setting the image when your if conditions are true, the ImageView is not updated if either of those conditions is false, and it will still have the image set on it previously. You simply need to set the ImageView's Bitmap to null when those conditions are not true. Also, your two if statements can be combined:
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
...
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url) && (url.endsWith(".png") || url.endsWith(".jpg"))) {
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
else {
videoViewHolder.attachment.setImageBitmap(null);
}
}