Android Pager Motherboard Design GridView Design Design Does Not Fit

I am trying to implement a pager with a material view using this . The pager is working properly. But I need a map grid layout under the view pager for example <w640 ".

But I am using GridLayoutManager

and getting the result like <w640 ". Can someone please help me rebuild this.

This is my code: Inside a recyclerview slice:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Calling the RecyclerView
    mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);


            mLayoutManager = new GridLayoutManager(context, 2);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mAdapter = new RecyclerViewMaterialAdapter(new GridAdapter(context, mContentItems));
            mRecyclerView.setAdapter(mAdapter);               

    MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}

      

And inside the GridAdapter:

public class GridAdapter  extends RecyclerView.Adapter<GridAdapter.ViewHolder> {

List<CommonCategoryItem> mItems;
Context context;
public GridAdapter(Context context,ArrayList<CommonCategoryItem> list) {
    super();
    mItems = new ArrayList<>();
    mItems = list;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    final View view= LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.list_item_card_small, viewGroup, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {

            CommonCategoryItem item = mItems.get(i);
            viewHolder.title.setText(item.getTitle());
            int resId = ImageHelper.getResourceId(context,mItems.get(i).getImage());
            if(resId!=0)
            {viewHolder.imageView.setImageResource(resId);}

}

@Override
public int getItemCount() {

    return mItems.size();
}


public static class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView imageView;
    public TextView title;
    public CardView cardView;

    public ViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView)itemView.findViewById(R.id.img_thumbnail);
        title = (TextView)itemView.findViewById(R.id.title);
        cardView = (CardView)itemView.findViewById(R.id.smallCardView);
    }       
}

}

      

It also needs the onclick listener to move to a different fragment for each click of the gridview item. I cannot get the childposition. Can someone help me.

+3


source to share


3 answers


In case anyone else has such an issue in the future as I did: the "correct" way to fix this as defined here: https://github.com/florent37/MaterialViewPager/issues/179 is to declare the number of columns in your mesh when configuring the adapter:

adapter = new RecyclerViewMaterialAdapter(new albumadapter(getActivity(), items),NUMBER_OF_COLUMNS);



Hope this helps.

+1


source


Solved the problem :) I don't know if this is correct or not:

Added an extra copy of the first element of the object to the very first place. those. 2 identical elements at the 0th and 1st indexes of the array. Then in the adpater grid, did this.



@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    switch (i) {
        case 0:viewHolder.cardView.setVisibility(View.GONE);
            break;
        default:
            viewHolder.cardView.setVisibility(View.VISIBLE);
            CommonCategoryItem item = mItems.get(i);
            viewHolder.title.setText(item.getTitle());
            int resId = ImageHelper.getResourceId(context,mItems.get(i).getImage());
            if(resId!=0)
            {viewHolder.imageView.setImageResource(resId);}

    }
}

      

I don't know if this is correct. Also, to get the position of the griditem, you need to use an onclicklistener for each item.

0


source


Solved the tooo click listener part :)

Changed RecyclerViewFragment method as follows:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Calling the RecyclerView
    mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);


            mLayoutManager = new GridLayoutManager(context, 2);
            mRecyclerView.setLayoutManager(mLayoutManager);
            GridAdapter gridAdapter = new GridAdapter(context, mContentItems,parentPosition);
            mAdapter = new RecyclerViewMaterialAdapter(gridAdapter);
            mRecyclerView.setAdapter(mAdapter);
            gridAdapter.setOnItemClickListener(new GridAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Log.d("helo", position  + " was clicked!");
                    Toast.makeText(getActivity(), position  + " was clicked!", Toast.LENGTH_SHORT).show();
                }
            });

    MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}

      

And added itemClickListener and callback to gridadapter:

public class GridAdapter  extends RecyclerView.Adapter<GridAdapter.ViewHolder> {

List<CommonCategoryItem> mItems;
Context context;
int parentPosition;
private OnItemClickListener listener;

public interface OnItemClickListener {
    void onItemClick(View itemView, int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
}

public GridAdapter(Context context,ArrayList<CommonCategoryItem> list,int parentPosition) {
    super();
    mItems = new ArrayList<>();
    mItems = list;
    this.context = context;
    this.parentPosition = parentPosition;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    final View view= LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.list_item_card_small, viewGroup, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    switch (i) {
        case 0:viewHolder.cardView.setVisibility(View.GONE);
            break;
        default:
            viewHolder.cardView.setVisibility(View.VISIBLE);
            CommonCategoryItem item = mItems.get(i);
            viewHolder.title.setText(item.getTitle());
            int resId = ImageHelper.getResourceId(context,mItems.get(i).getImage());
            if(resId!=0)
            {viewHolder.imageView.setImageResource(resId);}

    }
}

@Override
public int getItemCount() {

    return mItems.size();
}


public class ViewHolder extends RecyclerView.ViewHolder{

    public ImageView imageView;
    public TextView title;
    public CardView cardView;

    public ViewHolder(final View itemView) {
        super(itemView);
        imageView = (ImageView)itemView.findViewById(R.id.img_thumbnail);
        title = (TextView)itemView.findViewById(R.id.title);
        cardView = (CardView)itemView.findViewById(R.id.smallCardView);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Triggers click upwards to the adapter on click
                    listener.onItemClick(itemView, getLayoutPosition());
            }
        });
    }

}

}

      

Now everything is all right:)

0


source







All Articles