正如gif在滚动时可以看到的那样,在该活动中使用时,启动代码保留在该启动代码中,但是当置于片段中时会出现此问题。以下是文件任何建议或更改非常感谢。
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>
答案 0 :(得分:1)
您无法替换XML中包含的片段。它会将新片段放在“旧”片段的顶部。
有关详细信息,请参阅此问题:Android - fragment .replace() doesn't replace content - puts it on top