Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何防止RecyclerView单元的子进程在用户滚动时移动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的水平RecyclerView上有很长的单元格,
并且我希望他们有一个标题,当用户水平滚动时它仍然保持不动.
- Recycler View (A)
-   -   Cell (parent) (B)
-   -   -   Header (C) <-- We want that to be still
-   -   -   Content (D)

这是它在视觉上的样子:

因此,我正在寻找一种方法

1)当用户在RecyclerView(A)上拖动手指时,停止标题(C)更改位置

要么

2)像正常一样滚动单元格(B),但是将它的子项(C)的位置改变到相反的方向,以使标题即使在移动时也显示为静止(在父项的相反方向上(B) .

这是我正在尝试构建的内容

有任何想法吗?

p.s 1:我注意到很多SO答案,建议使用ItemDecoration,但所有可能的答案都有VERTICAL实现的代码,这与HORIZONTAL实现非常不同.

p.s 2我正在以编程方式创建所有视图内容,因此我不会使用布局文件. (那是因内容将是反应原生视图,我无法创建具有布局文件内容).

p.s 3:我还注意到ItemDecoration是旧策略,更新的第三方库扩展了LayoutManager.

请稍微说清楚,谢谢.

解决方法

然可以将标题视图保留在RecyclerView中并使其静态,但我建议采用另一种方法.

标题可以继续在RecyclerView内部表示,但显示将被带到RecyclerView的顶部,如下所示:

- title (C) <-- We want that to be still
- Recycler View (A)
-   -   Cell (parent) (B)
-   -   -   Content

RecyclerView.onScrollListener将侦听新项目的外观并相应地更改标题.这样,当出现新项目时,作为TextView的标题显示标题.以下说明了这一点.

(这是一个用于演示目的的简单实现.一个完整的应用程序将显示狗品种图像和某种有意义的描述.)

以下是实现此效果代码

@H_955_5@mainActivity.java

public class MainActivity extends AppCompatActivity {
    private linearlayoutmanager mLayoutManager;
    private RecyclerViewAdapter mAdapter;
    private TextView mBreedNametitle;
    privatE int mLastBreedtitlePosition = RecyclerView.NO_POSITION;

    protected void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.activity_main);

        List<String> breedList = createBreedList();

        // This is where the breed title is displayed.
        mBreedNametitle = findViewById(R.id.breedNametitlE);

        // Set up the RecyclerView.
        mLayoutManager = new linearlayoutmanager(this,linearlayoutmanager.HORIZONTAL,falsE);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        mAdapter = new RecyclerViewAdapter(breedList);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(mAdapter);

        // Add the OnScrollListener so we kNow when to change the breed title.
        recyclerView.addOnScrollListener(new RecyclerView.onScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView,int dx,int dy) {
                super.onScrolled(recyclerView,dx,dy);

                int lastVisible = mLayoutManager.findLastVisibleItemPosition();
                if (lastVisible == RecyclerView.NO_POSITION) {
                    return;
                }
                if (lastVisible != mLastBreedtitlePosition) {
                    mBreedNametitle.setText(mAdapter.getItems().get(lastVisiblE));
                    mLastBreedtitlePosition = lastVisible;
                }
            }
        });
    }

    private List<String> createBreedList() {
        List<String> breedList = new ArrayList<>();
        breedList.add("Affenpinscher");
        breedList.add("Afghan Hound");
        breedList.add("AireDale Terrier");
        breedList.add("Akita");
        breedList.add("Alaskan Malamute");
        breedList.add("American Cocker Spaniel");
        breedList.add("American Eskimo Dog (MiniaturE)");
        breedList.add("American Eskimo Dog (Standard)");
        breedList.add("American Eskimo Dog (Toy)");
        breedList.add("American Foxhound");
        breedList.add("American Staffordshire Terrier");
        breedList.add("American Eskimo Dog (Standard)");
        return breedList;
    }

    @SuppressWarnings("unused")
    private final static String TAG = "MainActivity";
}

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final List<String> mItems;

    RecyclerViewAdapter(List<String> items) {
        mItems = items;
    }

    @Override
    @NonNull
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewTypE) {
        View view;

        view = LayoutInflater.from(parent.getCo@R_618_10443@t()).inflate(R.layout.item_layout,parent,falsE);
        return new RecyclerViewAdapter.ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,int position) {
        RecyclerViewAdapter.ItemViewHolder vh = (RecyclerViewAdapter.ItemViewHolder) holder;

        vh.mBreedImage.setImageDrawable(holder.itemView.getresources().getDrawable(R.drawable.no_imagE));
        vh.mBreedName = mItems.get(position);
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public List<String> getItems() {
        return mItems;
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView mBreedImage;
        private String mBreedName;

        ItemViewHolder(View itemView) {
            super(itemView);
            mBreedImage = itemView.findViewById(R.id.breedImagE);
        }
    }
}

activity_main.xml中

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:orientation="vertical">

    <TextView
        android:id="@+id/breedNametitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:fontFamily="sans-serif"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        tools:text="Breed name" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" />
</LinearLayout>

item_layout.xml

<android.support.consTraint.ConsTraintLayout 
    android:id="@+id/cont_item_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:BACkground="@android:color/white">

    <ImageView
        android:id="@+id/breedImage"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:contentDescription="Dog breed image"
        app:layout_consTraintBottom_toBottomOf="parent"
        app:layout_consTraintStart_toStartOf="parent"
        app:layout_consTraintTop_toTopOf="parent"
        app:layout_consTraintVertical_bias="1.0"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:text="@String/large_text"
        app:layout_consTraintBottom_toBottomOf="parent"
        app:layout_consTraintEnd_toEndOf="parent"
        app:layout_consTraintStart_toEndOf="@+id/breedImage"
        app:layout_consTraintTop_toTopOf="parent" />

</android.support.consTraint.ConsTraintLayout>

更新:这是另一种设置TextView的左边距以使标头粘滞的方法. TextView的负x偏移量被视为标题的填充,使其在TextView中向右滑动并粘贴到屏幕的左侧.

结果如下:

@H_955_5@mainActivity.java

public class MainActivity extends AppCompatActivity {
    private linearlayoutmanager mLayoutManager;

    protected void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.activity_main);

        List<String> breedList = createBreedList();

        // Set up the RecyclerView.
        mLayoutManager = new linearlayoutmanager(this,falsE);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(breedList);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.onScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView,dy);

                // Pad the left of the breed name so it stays aligned with the left side of the display.
                int firstVisible = mLayoutManager.findFirstVisibleItemPosition();
                View firstView = mLayoutManager.findViewByPosition(firstVisiblE);
                firstView.findViewById(R.id.itemBreedName).setPadding((int) -firstView.getX(),0);

                // Make sure the other breed name has zero padding because we may have changed it.
                int lastVisible = mLayoutManager.findLastVisibleItemPosition();
                View lastView = mLayoutManager.findViewByPosition(lastVisiblE);
                lastView.findViewById(R.id.itemBreedName).setPadding(0,0);
            }
        });
    }

    private List<String> createBreedList() {
        List<String> breedList = new ArrayList<>();
        breedList.add("Affenpinscher");
        breedList.add("Afghan Hound");
        breedList.add("AireDale Terrier");
        breedList.add("Akita");
        breedList.add("Alaskan Malamute");
        breedList.add("American Cocker Spaniel");
        breedList.add("American Eskimo Dog (MiniaturE)");
        breedList.add("American Eskimo Dog (Standard)");
        breedList.add("American Eskimo Dog (Toy)");
        breedList.add("American Foxhound");
        breedList.add("American Staffordshire Terrier");
        breedList.add("American Eskimo Dog (Standard)");
        return breedList;
    }

    @SuppressWarnings("unused")
    private final static String TAG = "MainActivity";

}

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final List<String> mItems;

    RecyclerViewAdapter(List<String> items) {
        mItems = items;
    }

    @Override
    @NonNull
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int position) {
        RecyclerViewAdapter.ItemViewHolder vh = (RecyclerViewAdapter.ItemViewHolder) holder;

        vh.mBreedImage.setImageDrawable(holder.itemView.getresources().getDrawable(R.drawable.no_imagE));
        vh.mBreedName.setPadding(0,0);
        vh.mBreedName.setText(mItems.get(position));
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView mBreedImage;
        private TextView mBreedName;

        ItemViewHolder(View itemView) {
            super(itemView);
            mBreedImage = itemView.findViewById(R.id.breedImagE);
            mBreedName = itemView.findViewById(R.id.itemBreedName);
        }
    }
}

activity_main.xml中

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" />
</LinearLayout>

item_layout.xml

<android.support.consTraint.ConsTraintLayout 
    android:id="@+id/cont_item_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:BACkground="@android:color/white">

    <TextView
        android:id="@+id/itemBreedName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:ellipsize="none"
        android:fontFamily="sans-serif"
        android:singleLine="true"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        tools:text="Breed name" />

    <ImageView
        android:id="@+id/breedImage"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:contentDescription="Dog breed image"
        app:layout_consTraintBottom_toBottomOf="parent"
        app:layout_consTraintStart_toStartOf="parent"
        app:layout_consTraintTop_toBottomOf="@+id/itemBreedName"
        app:layout_consTraintVertical_bias="1.0"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:text="@String/large_text"
        app:layout_consTraintBottom_toBottomOf="parent"
        app:layout_consTraintEnd_toEndOf="parent"
        app:layout_consTraintStart_toEndOf="@+id/breedImage"
        app:layout_consTraintTop_toBottomOf="@+id/itemBreedName" />

</android.support.consTraint.ConsTraintLayout>

大佬总结

以上是大佬教程为你收集整理的android – 如何防止RecyclerView单元的子进程在用户滚动时移动全部内容,希望文章能够帮你解决android – 如何防止RecyclerView单元的子进程在用户滚动时移动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。