0

スクロールするとグリッド項目が表示されますが、項目の後ろには同じ項目が表示されます。なぜこれが起こっているのか分かりません。カスタムアダプタ、グリッドレイアウトマネージャを使用します。ここでは、アクティビティにフラグメントを追加していますが、Recyclerviewのレイアウトは断片的に膨らみ、カスタム行はrecyclerviewで膨らんでいます。MainActivityで追加されたフラグメントで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 ::

パブリッククラスVivzAdapterがRecyclerView.Adapter {

を拡張

レイアウトactivity_main
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); 
    } 
} 

}

::

<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

ここでは、フラグメントは毎回作成されるため、アイテムの背後にあるアイテムを表示します。

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(); 
    } 
} 
にコード
関連する問題