标签: android-recyclerview

使用GridLayoutManager在RecyclerView中拖放项目

我想要实现的目标:拥有一个带有GridLayoutManager的RecyclerView,它支持drag'n'drop并在拖动时重新排列项目.

旁注:第一次用拖放开发任何东西.

有很多关于如何使用的ListView来实现这一功能的主题,例如:https://raw.githubusercontent.com/btownrippleman/FurthestProgress/master/FurthestProgress/src/com/anappforthat/android/languagelineup/DynamicListView.java

然而,通常的例子是很多的代码,创建拖累视图的位图和感觉应该可以使用来实现相同的结果View.startDrag(...),并与RecyclerView notifyItemAdded(),notifyItemMoved()而且notifyItemRemoved()因为它们提供了重新排列动画.

所以我玩了一些并想出了这个:

final CardAdapter adapter = new CardAdapter(list);
adapter.setHasStableIds(true);
adapter.setListener(new CardAdapter.OnLongClickListener() {
    @Override
    public void onLongClick(View view) {
        ClipData data = ClipData.newPlainText("","");
        View.DragShadowBuilder builder = new View.DragShadowBuilder(view);
        final int pos = mRecyclerView.getChildAdapterPosition(view);
        final Goal item = list.remove(pos);

        mRecyclerView.setOnDragListener(new View.OnDragListener() {
            int prevPos = pos;

            @Override
            public boolean onDrag(View view, DragEvent dragEvent) {
                final int action = dragEvent.getAction();
                switch(action) {
                    case DragEvent.ACTION_DRAG_LOCATION:
                        View onTopOf = mRecyclerView.findChildViewUnder(dragEvent.getX(), dragEvent.getY());
                        int i …
Run Code Online (Sandbox Code Playgroud)

android drag-and-drop gridlayoutmanager android-recyclerview

40
推荐指数
3
解决办法
4万
查看次数

Android Horizo​​ntal RecyclerView滚动方向

我做了一个Horizo​​ntal RecyclerView,它运行正常(感谢这一点),但滚动和数据的方向从左向右扩展; 那么如何更改RecyclerView滚动方向,如下图所示?

在此输入图像描述

我的代码:

StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(
                        2, //The number of Columns in the grid
                        LinearLayoutManager.HORIZONTAL);
Run Code Online (Sandbox Code Playgroud)

android horizontal-scrolling android-recyclerview

40
推荐指数
4
解决办法
10万
查看次数

RecyclerView项目中的背景选择器

我正在使用RecyclerView下面的代码:

<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="320dp"
    android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

和我的清单项目:

<LinearLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_medium_high">
    <com.basf.suvinil.crie.ui.common.widget.CircleView
        android:id="@+id/circle"
        android:layout_width="22dp"
        android:layout_height="22dp"/>
    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="57.5dp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

详细看这部分android:background="@drawable/selector_medium_high"它是一个普通的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background_high" android:state_activated="true"/>
    <item android:drawable="@color/background_high" android:state_pressed="true"/>
    <item android:drawable="@color/background_high" android:state_checked="true"/>
    <item android:drawable="@color/background_high" android:state_focused="true"/>
    <item android:drawable="@color/background_medium"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

但是当我运行这段代码时,触摸行时我的背景颜色没有变化....

android selector android-recyclerview

39
推荐指数
6
解决办法
6万
查看次数

如何保持RecyclerView始终滚动到底部

我使用Recyclerview替换列表视图我想保持Recyclerview始终滚动到底部.

ListView可以使用此方法 setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL)

RecyclerView我用的方法 smoothScrollToPosition(myAdapter.getItemCount() - 1)

但是当软键盘弹出时,它取代了RecyclerView的内容.

android android-recyclerview

39
推荐指数
4
解决办法
4万
查看次数

LinearLayoutManager setReverseLayout()== true但是项目从底部堆叠

这似乎是一个简单的解决方案,但似乎设置

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mLayoutManager;

.... // More code

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    // Add item decoration
    mRecyclerView.addItemDecoration(new SpacesItemDecoration(DIVIDER_SPACE));

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setReverseLayout(true); // THIS ALSO SETS setStackFromBottom to true
    mRecyclerView.setLayoutManager(mLayoutManager);
Run Code Online (Sandbox Code Playgroud)

似乎还要从底部设置要堆叠的项目

我试图设置setStackFromBottom为false但是没有做任何事情,什么是扭转项目顺序但仍然从顶部填充的最佳方法?我应该使用Custom Comparator类吗?我希望这比创建另一个类更容易.

java reverse android layout-manager android-recyclerview

39
推荐指数
2
解决办法
4万
查看次数

Kotlin中的RecyclerView itemClickListener

我在使用Android 3年后,正在Kotlin编写我的第一个应用程序.只是混淆了如何在Kotlin中使用itemClickListener和RecyclerView.

我尝试过特征(编辑:现在界面)方法,非常类似于Java

public class MainActivity : ActionBarActivity() {

  protected override fun onCreate(savedInstanceState: Bundle?) {

    // set content view etc go above this line

    class itemClickListener : ItemClickListener {
      override fun onItemClick(view: View, position: Int) {
        Toast.makeText(this@MainActivity, "TEST: " + position, Toast.LENGTH_SHORT).show()
      }
    }

    val adapter = DrawerAdapter(itemClickListener())
    mRecyclerView.setAdapter(adapter)
 }

  trait ItemClickListener {
    fun onItemClick(view: View, position: Int)
  }
}
Run Code Online (Sandbox Code Playgroud)

这似乎非常多余,所以我尝试了内部类方法:

inner class ItemClickListener {
    fun onItemClick(view: View, position: Int) {
        startActivityFromFragmentForResult<SelectExerciseActivity>(SELECT_EXERCISES)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后只需设置适配器的点击监听器,如下所示:

val adapter = WorkoutsAdapter(ItemClickListener())
Run Code Online (Sandbox Code Playgroud)

但我仍然对此不满意,因为我认为可能会有更好,更清洁的方式.我试图基本上实现这样的事情: …

android kotlin android-recyclerview

39
推荐指数
11
解决办法
5万
查看次数

getAdapterPosition() 已弃用

我已将我targetSdkVersion的 28更新到 30,我注意到它getAdapterPosition()已被弃用(我不确定何时发生这种情况)。

文档中,他们说:

此方法已弃用。
当适配器嵌套其他适配器时,此方法会造成混淆。如果您在 Adapter 的上下文中调用它,您可能想要调用getBindingAdapterPosition()或者如果您想要 RecyclerView 看到的位置,您应该调用getAbsoluteAdapterPosition()

该文档还说明了以下内容:

请注意,如果您正在查询 RecyclerView 看到的位置,则应使用getAbsoluteAdapterPosition()(例如,您想使用它来保存滚动状态)。如果您要查询访问 RecyclerView.Adapter 内容的位置,则应使用getBindingAdapterPosition()

我的理解是:

  • getBindingAdapterPosition当您想要获取适配器位置时应该使用它(如果它仍然存在于适配器中)。如果它不再存在于适配器中,它将返回-1( NO_POSITION )。
  • getAbsoluteAdapterPosition应该用于获取位置,如RecyclerView所见。例如,如果某个项目已被删除,但尚未从ViewHolder.

换句话说,如果我的 中有4项目Adapter,我删除位置0和查询,getAbsoluteAdapterPosition并且getBindingAdapterPosition在项目从 中删除之前ViewHolder,然后getAbsoluteAdapterPosition将返回0(因为视图仍在 中ViewHolder)并getBindingAdapterPosition返回-1(因为它不再存在于适配器中)。


我通过记录以下内容测试了差异:

Log.e("difference", "getAdapterPosition = "+myHolder.getAdapterPosition()+" getBindingAdapterPosition = "+myHolder.getBindingAdapterPosition()+" getAbsoluteAdapterPosition = "+myHolder.getAbsoluteAdapterPosition()); …
Run Code Online (Sandbox Code Playgroud)

android android-adapter android-recyclerview

39
推荐指数
1
解决办法
9103
查看次数

滚动条未显示在RecyclerView中

我有一个RecyclerView并且希望滚动条显示,当它覆盖多个页面时.

我根本没有滚动条.任何的想法?

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/cl_only_empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="5dp"
        android:text="@string/cl_only_empty"
        android:textColor="@color/white" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/callsList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-scrollbar android-recyclerview

38
推荐指数
7
解决办法
3万
查看次数

装饰RecyclerView(使用GridLayoutManager)以显示项目之间的分隔符

装饰RecyclerView以获得这样的外观和感觉的最佳和最简单的方法是什么?

在此输入图像描述

这里的主要挑战是仅在项目之间具有分隔符,而不是在项目与屏幕的左/右边界之间.

有任何想法吗?

android android-recyclerview

38
推荐指数
3
解决办法
4万
查看次数

如何在CoordinatorLayout中的Viewpager下方获取Adview

目前AdView正在内部出现,ViewPager因此它阻止了应用程序中的内容.我怎样才能让它AdView出现在ViewPager而不是在里面.

我试图把它AdView放在一个RelativeLayout下面ViewPager然后AdView根本不显示.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.candyx.testapp.Activity">

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/main_button_background">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorTabIndicator"
            app:tabIndicatorHeight="3dp"/>

    </android.support.design.widget.AppBarLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
        android:layout_gravity="center|bottom"/>

</android.support.design.widget.CoordinatorLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview android-coordinatorlayout

38
推荐指数
2
解决办法
2万
查看次数