【问题标题】:Change the margin of first visible item in recyclerview更改 recyclerview 中第一个可见项目的边距
【发布时间】:2016-09-02 00:58:15
【问题描述】:

如何使用卡片更改水平recycleview上第一个完整可见项目的边距(底部为20 dp)?我只能获得第一个完整可见项的索引,但不能获得对视图的任何引用。

 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
       @Override
       public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
           super.onScrolled(recyclerView, dx, dy);

           firstVisibleItem = lm.findFirstCompletelyVisibleItemPosition();
        View view=  mRecyclerView.getChildAt(firstVisibleItem);

        RelativeLayout.LayoutParams lp= new RelativeLayout.LayoutParams(300, 150);
        lp.setMargins(0, 0, 0, 20);
        view.setLayoutParams(lp);

       }
    });

【问题讨论】:

  • 我尝试使用 View view= mRecyclerView.getChildAt(firstVisibleItem); 获取视图并使用边距设置布局参数 lp.setMargins(0, 0, 0, 20);但没用

标签: android android-recyclerview margin selecteditem


【解决方案1】:

假设 view 包含对要更改其边距的视图的引用,您可以使用以下内容将底部边距设置为 20 像素:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 20);
view.setLayoutParams(lp);

请注意,这会将下边距更改为 20 像素,而不是 20dp。您需要将 20dp 值转换为像素。 Converting pixels to dp 是了解如何进行转换的好地方。

您可能还需要更改 LinearLayout.LayoutParams 的构造函数的高度和宽度参数以满足您的需要。

如果问题是获取对视图的引用,发布一些代码会有所帮助。

编辑/更新 该代码有所帮助。我认为您没有设置对所需视图的访问权限。这是另一种方法:

 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
   @Override
   public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
       super.onScrolled(recyclerView, dx, dy);

       // Find the adapter position of the first fully visible item
       // May return RecyclerView.NO_POSITION that is not handled here.
       firstVisibleItem = lm.findFirstCompletelyVisibleItemPosition();

       // Find the corresponding view holder for this position.
       // MyViewHolder should already be defined (just don't know the name).
       MyViewHolder vh = (MyViewHolder) mRecyclerView
           .findViewHolderForAdapterPosition(firstVisibleItem);

       // Get the layout parameters from the top-level item of this view.
       // This could also be another view captured within the view holder.
       RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) vh.itemView.getLayoutParams();

       // Make our changes and set them.
       lp.setMargins(0, 0, 0, 50);
       vh.itemView.setLayoutParams(lp);
   }
});

【讨论】:

  • 有我的代码,我编辑了它..它与你在recycleview的onscroll方法中的建议相同,lm是recycleview的布局管理器但没有任何反应,请帮助..我需要一个漂亮的界面很像实地考察应用程序
【解决方案2】:

首先,如果您要更改 RecyclerView 中的布局或非常具体的项目,您可能没有正确设计布局。你可能更想做这样的事情:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="20dp"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

如果您仍然只想使用 RecyclerView,那么您应该重写 onBindViewHolder。在这种方法中,您可以根据 ViewHolder 的位置进行区分,并为您的 View 设置不同的布局。

@Override
    public void onBindViewHolder(ViewHolder holder,
                                 @SuppressLint("RecyclerView") final int position) {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多