Search code examples
androidandroid-viewpager

how to remove an object in viewpager2 in android?


I'm making a viewpage2 view after the code is run in the display object appears as in the picture, how to delete objects circled in red, it appears when the slide / page runs out

image

This is Mainactivity class code

public class MainActivity extends AppCompatActivity {

ViewPager2 viewPager2;
List<user> userList;
userAdaptor userAdaptor;
DatabaseReference mydb;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager2 = findViewById(R.id.view2);

    userList = new ArrayList<>();

    userList.add(new user(R.drawable.ic_launcher_background, "tf","C"));
    userList.add(new user(R.drawable.ic_launcher_foreground,"wuiad","%"));

    userAdaptor = new userAdaptor(this, userList);
    viewPager2.setAdapter(userAdaptor);
  }

}

This is user class code

public class user {
private int item;
private String name;
private String ind;


public user(){

}
public user(int item,String name, String ind){

    this.item =item;
    this.name = name;
    this.ind = ind;
}

public int getItem(){
    return item;
}

public void setItem(int item){
    this.item = item;
}

public String getName(){
    return name;
}

public void setName(String name){
    this.name = name;
}

public String getInd(){
    return ind;
}

public void setInd(String ind){
    this.ind = ind;
}

}

This is useradapter class code

  public class userAdaptor extends 
RecyclerView.Adapter<userAdaptor.UserViewHolder> {

Context mcontext;
List<user> userList;

public userAdaptor(Context mcontext, List<user>userList){
    this.mcontext = mcontext;
    this.userList = userList;
}

@NonNull
@Override
public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
viewType) {

    View view = LayoutInflater.from(mcontext).inflate(R.layout.item, 
parent, false);
    return new UserViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull UserViewHolder holder, int position) 
{

    user user = userList.get(position);
    holder.tv_name.setText(user.getName());
    holder.tv_ind.setText(user.getInd());
    holder.item.setImageResource(user.getItem());

}

@Override
public int getItemCount() {
    return userList.size();
}

public class UserViewHolder extends RecyclerView.ViewHolder {

    ImageView item;
    TextView tv_name, tv_ind;

    public UserViewHolder(@NonNull View itemView) {
        super(itemView);

        item = itemView.findViewById(R.id.gambar);
        tv_name = itemView.findViewById(R.id.textValue);
        tv_ind =itemView.findViewById(R.id.indikator);
    }
   }
 }

This is activity_main.xml code

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="260dp"
        android:layout_height="260dp"
        android:layout_marginTop="70dp"
        android:layout_gravity="center"
        >

        <androidx.viewpager2.widget.ViewPager2
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/view2"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

</LinearLayout>

this is item.xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<ImageView
    android:id="@+id/gambar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/ic_launcher_background"/>

<TextView
    android:id="@+id/textValue"
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Value"
    android:textStyle="bold"
    android:textSize="25dp"/>

<TextView
    android:id="@+id/indikator"
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:text="C"
    android:textSize="20dp"/>

Solution

  • I am amusing you are talking about following scenario

    When there are no pages to scroll, you get the view as you circle in the image. If so then following changes will take care of it ,

    <androidx.viewpager2.widget.ViewPager2
        .............
        android:overScrollMode="never"/>
    

    As viewPager2 is a RecycleView kind, then it has the properties of RecycleView. What you are getting is a overScroll property of RecycleView which work as indication that there are no more item. Click here to know more about overScrollMode.