Recycleview滚动子项重叠 [英] Recycleview scrolling child overlaps

查看:139
本文介绍了Recycleview滚动子项重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过gif可以观察到,滚动时,伯爵儿童保留在该起始代码在活动中使用时运行良好的位置,但是当放在片段中时,会发生此问题.以下是任何建议或更改的文件,将不胜感激.

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>

推荐答案

您不能替换XML中包含的片段.它将新片段放在旧"片段的顶部.

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

有关更多详细信息,请参见此问题: Android -片段.replace()不会替换内容-将其放在顶部

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

这篇关于Recycleview滚动子项重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆