向下滚动时,它会显示网格项,但在项目后面会显示相同的项目。不知道为什么会这样。使用Custom适配器,Grid Layout Manager。在这里,我在活动中添加片段,Recyclerview布局在片段中膨胀,自定义行在recyclerview中膨胀。
showing items behind items when scrolling, click this to view the screenshot of my layout
MainActivity onCreate方法::
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.containerGrid, new FragmentGrid()).commit();
}
}
FragmentGrid类::::
public class FragmentGrid extends Fragment {
RecyclerView recyclerView;
VivzAdapter vivzAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fragment_grid, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycleView);
int spanCount = 3; // 3 columns
int spacing = 50; // 50px
boolean includeEdge = false;
vivzAdapter = new VivzAdapter(getActivity(), getData());
recyclerView.setAdapter(vivzAdapter);
recyclerView.setHasFixedSize(true);
//recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
return rootView;
}
public static List<Information> getData() {
List<Information> data = new ArrayList<>();
int[] images = {R.drawable.gridee, R.drawable.gridee, R.drawable.gridee, R.drawable.gridee, R.drawable.gridee, R.drawable.gridee, R.drawable.gride, R.drawable.gride, R.drawable.gride, R.drawable.gride};
String[] names = {"Garlic Nan", "Caffe Latte", "Ice Cream with Ceramal", "Noodle Foodie", "Rice Organ", "Jesan", "Hemal", "FoodMe", "Noodle", "Rice Organ"};
String[] prices = {"209", "103", "760", "120", "450", "23", "90", "87", "67", "109"};
for (int i = 0; i < images.length && i < names.length && i < prices.length; i++) {
Information current = new Information();
current.title = names[i];
current.price = prices[i];
current.iconId = images[i];
data.add(current);
}
return data;
}
}
VivzAdapter ::
public class VivzAdapter扩展了RecyclerView.Adapter {
private LayoutInflater inflater;
private Context context;
List<Information> data = Collections.emptyList();
public VivzAdapter(Context context, List<Information> data){
inflater = LayoutInflater.from(context);
this.data = data;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current = data.get(position);
holder.foodPrice.setText(current.price);
holder.productNameG.setText(current.title);
Picasso.with(context).load(current.iconId).noFade().resize(150,150).centerCrop().into(holder.foodImage);
//Picasso.with(context).load(current.iconId).resize(240, 120).into(holder.foodImage);
holder.foodImage.setImageResource(current.iconId);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView foodImage;
TextView productNameG;
TextView foodPrice;
public MyViewHolder(View itemView) {
super(itemView);
foodImage = (ImageView) itemView.findViewById(R.id.foodImage);
productNameG = (TextView) itemView.findViewById(R.id.productNameG);
foodPrice = (TextView) itemView.findViewById(R.id.foodPrice);
}
}
}
activity_main Layout ::
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<fragment
android:id="@+id/containerGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:name="com.pos.hassanmashraful.recyclerviewdick.FragmentGrid"
android:orientation="vertical">
</fragment>
</RelativeLayout>
custom_row :::布局实施
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/foodImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@android:drawable/btn_star" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#55000000">
<TextView
android:id="@+id/productNameG"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:text="@string/productNameG"
android:textColor="@android:color/white"
android:textSize="17sp"
/>
<TextView
android:id="@+id/foodPrice"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/productPrice"
android:textColor="@android:color/white"
android:textSize="18sp"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:textStyle="italic|bold" />
</LinearLayout>
</RelativeLayout>
::::: RecyclerView ::::布局实施
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pos.hassanmashraful.recyclerviewdick.FragmentGrid">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
答案 0 :(得分:0)
这里,Fragment每次都在创建,因此它显示了项目背后的项目。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.containerGrid, new FragmentGrid()).commit();
}
}
将代码更改为此
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getFragmentManager().findFragmentById(R.id.containerGrid) == null) {
getFragmentManager().beginTransaction().add(R.id.containerGrid, new FragmentGrid()).commit();
}
}