3

我正在尝试使用实现材料视图寻呼机。寻呼机工作正常。但我需要的是视图寻呼机下的卡片网格布局,例如这 .

但是我正在使用GridLayoutManager并最终得到类似的结果这。谁能帮我重新调整一下。

这是我的代码:内部 recyclerview 片段:

@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);
}

在 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);
    }       
}

}

还需要一个 onclick 侦听器来移动到每个 gridview 项目单击的不同片段。我无法获得childposition。有人可以帮我吗。

4

3 回答 3

1

万一其他人将来像我一样遇到这个问题:解决这个问题的“正确”方法,定义如下:https ://github.com/florent37/MaterialViewPager/issues/179 是声明列数设置适配器时的网格:

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

我希望这有帮助。

于 2015-12-18T23:08:27.080 回答
0

解决了问题:)不知道它是否正确:

将 mItems 的第一项的额外副本添加到第一个位置。即,arraylist mItems 的第 0 和第 1 索引处有 2 个相同的项目。然后在网格适配器中,这样做了。

@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);}

    }
}

不知道它是否合适。此外,需要每个 itemview 的 onclicklistener 来获取 griditem 的位置。

于 2015-07-27T14:16:35.980 回答
0

也解决了点击监听器部分:)

像这样修改了 RecyclerViewFragment 的方法:

@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);
}

并在 gridadapter 中添加了 itemClickListener 和回调:

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());
            }
        });
    }

}

}

现在一切都很好:)

于 2015-07-27T16:16:53.320 回答