-1

enter image description here

As can be observed by the gif when scrolled the the eariler child remain in the place this started code worked well when used in the activity but when placed in fragment this issue occurs. Below are files any suggestion or change is much appreciated.

Main2.java

public class Main2 extends AppCompatActivity {
String Tag="Main2";

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


        Log.i(Tag, "onCreate: ");

        FragmentRecy fr=new FragmentRecy();



        FragmentTransaction transaction = 
        getSupportFragmentManager().beginTransaction();


        transaction.replace(R.id.fragment,fr);

        transaction.commit();



    }
}

ProductAdapter.java

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductHolder> {


    private  List<ProductModel> productslist;
    private int rowLayout;
    private Context context;

    @Override
    public ProductAdapter.ProductHolder onCreateViewHolder(@NonNull 
    ViewGroup parent, int viewType) {

        View view = 
    LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, 
    false);
        return new ProductHolder(view);
    }
    public ProductAdapter(List<ProductModel> productslist, int 
    rowLayout, Context context) {
        this.productslist = productslist;
        this.rowLayout = rowLayout;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(@NonNull ProductAdapter.ProductHolder 
    holder, int position) {


        holder.productPrice.setText(productslist.get(position).getPrice()+" Per "+productslist.get(position).getQuantity() );
        holder.productText.setText(productslist.get(position).getName());


        Glide.with(context).load(productslist.get(position).getImageurl())
                .thumbnail(0.5f)

                .into(holder.productImage);


        final int pos=   position;

        holder.productLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"Clicked On"+productslist.get(pos).getName(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return productslist.size();
    }
    public interface ClickListener {
        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }
    public class ProductHolder  extends RecyclerView.ViewHolder{
        ConstraintLayout productLayout;
        ImageView productImage;
        TextView productPrice;
        TextView productText;
       // TextView rating;


        public ProductHolder(View itemView) {
            super(itemView);
           productLayout = (ConstraintLayout) itemView.findViewById(R.id.pro_layout);
            productImage = (ImageView) itemView.findViewById(R.id.productimageView);
            productPrice = (TextView) itemView.findViewById(R.id.pricetext);

            productText = (TextView) itemView.findViewById(R.id.nametext);
           // rating = (TextView) itemView.findViewById(R.id.rating);
        }
    }


    public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
ProductAdapter.ClickListener ClickListener;

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    }

    }

FragmentRecy.java

public class FragmentRecy extends Fragment {
     RecyclerView recyclerView;
   String TAG="FragmentRecy";

  public   FragmentRecy() {


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragmentrecyclemain, container, false);


    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Log.d(TAG, "onViewCreated: ");
        recyclerView   = (RecyclerView) view.findViewById(R.id.list2);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));


    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<List<ProductModel>> call = apiService.getTopRatedMovies();

        Log.e("Json recieved", call.toString() );
        call.enqueue(new Callback<List<ProductModel>>() {
            @Override
            public void onResponse(Call<List<ProductModel>> call, Response<List<ProductModel>> response) {
                List<ProductModel> proList= response.body();
                recyclerView.setAdapter(new ProductAdapter(proList, R.layout.productsitem, getContext()));

            }

            @Override
            public void onFailure(Call<List<ProductModel>> call, Throwable t) {

            }
        });
    }
}

main2.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">

    <fragment
        android:id="@+id/fragment"
        android:name="com.dbd.juggernaut.juggernaut.fragment.FragmentRecy"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

fragmentrecyclemain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
dreamer1989
  • 1,075
  • 3
  • 12
  • 29

1 Answers1

1

You cannot replace a fragment that is included in the XML. It will put the new fragment on the top of the "old" fragment.

Please see this question for more details: Android - fragment .replace() doesn't replace content - puts it on top

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Csaba Farkas
  • 794
  • 7
  • 11