嵌套的固定高度垂直循环视图(滚动问题)

huangapple go评论89阅读模式
英文:

Nested Vertical RecycleView of fixed height (Scroll issue)

问题

我在这个布局中设计了一个`NestdScrollView`,其中包含3个`RecycleViews`。在每个`RecycleView`项目单击时会出现另一个高度为`200dp``ScrollView`中的`NestedRecycleView`。`NestedRecycleView`的滚动不起作用图像中有超过4个列表项”,但只显示了4个无法滚动当我滚动父`NestdScrollView`它会滚动

> 有人评论说这是不好的设计如果有人有想法
> 如何处理这个问题请分享您的想法

```java
// RecycleView适配器

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

    // ... 其他部分 ...

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // ... 其他部分 ...

        ViewHolder(View itemView) {
            // ... 其他部分 ...

            myTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<String> list12 = new ArrayList<String>(
                            Arrays.asList("列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项", "列表项"));
                    nestedScrollViewRecycleView.setVisibility(View.VISIBLE);

                    MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(context, list12);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(context, recyclerViewNested.VERTICAL, false);
                    recyclerViewNested.setLayoutManager(layoutManager);
                    recyclerViewNested.setAdapter(adapter);
                }
            });
        }

        // ... 其他部分 ...
    }

    // ... 其他部分 ...
}

// MainActivity

public class MainActivity extends AppCompatActivity {

    // ... 其他部分 ...

    private RecyclerView.OnItemTouchListener getOnItemTouchListener() {
        return new RecyclerView.OnItemTouchListener() {
            // ... 其他部分 ...
        };
    }

    // ... 其他部分 ...
}

// activity_main.xml

<!-- ... 其他部分 ... -->

// list_item_text.xml

<!-- ... 其他部分 ... -->

请注意,上面提供的只是翻译后的代码片段,其中涵盖了您提供的内容。如果您还需要其他帮助或解释,请随时告知。

英文:

I have designed this layout layout in which i have a NestdScrollView which contains 3 RecycleViews. On each RecycleView item click appears another NestedRecycleView appears with is inside ScrollView of height 200dp. NestedRecycleView scroll does not work. In the image there are more than 4 "list items" but only 4 are shown, it is not scrollable. When I scroll the parent NestdScrollView scrolls.

> Someone commented about this being bad design, if anyone has a idea
> how to approach this kindly share your ideas also.

嵌套的固定高度垂直循环视图(滚动问题)
RecycleView Adapter

  public class MyRecyclerViewAdapter extends RecyclerView.Adapter&lt;MyRecyclerViewAdapter.ViewHolder&gt; {
private List&lt;String&gt; mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List&lt;String&gt; data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.context=context;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.list_item_text, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}
// total number of rows
@Override
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
RecyclerView recyclerViewNested;
ScrollView nestedScrollViewRecycleView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.text_view);
recyclerViewNested = itemView.findViewById(R.id.recycle_view_nested);
nestedScrollViewRecycleView= itemView.findViewById(R.id.nested_scrollview_in_recycleview);
myTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList&lt;String&gt; list12 = new ArrayList&lt;String&gt;(
Arrays.asList(&quot;list item&quot;, &quot;list item&quot;, &quot;list item&quot;,&quot;list item&quot;,&quot;list item&quot;, &quot;list item&quot;, &quot;list item&quot;,&quot;list item&quot;,&quot;list item&quot;, &quot;list item&quot;, &quot;list item&quot;,&quot;list item&quot;,&quot;list item&quot;, &quot;list item&quot;, &quot;list item&quot;,&quot;list item&quot;,&quot;list item&quot;, &quot;list item&quot;, &quot;list item&quot;,&quot;list item&quot;));
nestedScrollViewRecycleView.setVisibility(View.VISIBLE);
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(context, list12);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, recyclerViewNested.VERTICAL, false);
recyclerViewNested.setLayoutManager(layoutManager);
recyclerViewNested.setAdapter(adapter);
}
});
//itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}

MainActivity

public class MainActivity extends AppCompatActivity {
public static final int SWIPE_THRESHOLD = 100;
public static final int SWIPE_VELOCITY_THRESHOLD = 100;
private static final String TAG = &quot;MainActivity&quot;;
ArrayList&lt;String&gt; list2;
NestedScrollView NestedScroll1;
ConstraintLayout constraintLayout;
RecyclerView recyclerView1;
//vars
private boolean mSwiping = false;
private float mDownX = 0;
private float xCoOrdinate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView1 = findViewById(R.id.firstColumn);
RecyclerView recyclerView2 = findViewById(R.id.secondColumn);
RecyclerView recyclerView3 = findViewById(R.id.thirdColumn);
NestedScroll1 = findViewById(R.id.scrollview_nested);
ArrayList&lt;String&gt; list12 = new ArrayList&lt;String&gt;(
Arrays.asList(&quot;A&quot;, &quot;column1&quot;, &quot;test1&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;));
list2 = new ArrayList&lt;String&gt;(
Arrays.asList(&quot;B&quot;, &quot;column3&quot;, &quot;test3&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;, &quot;311&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;, &quot;111&quot;, &quot;211&quot;, &quot;311&quot;));
// set up the RecyclerView
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(this, list12);
configRecyclerView(recyclerView1, adapter);
configRecyclerView(recyclerView2, adapter);
configRecyclerView(recyclerView3, adapter);
recyclerView2.addOnItemTouchListener(getOnItemTouchListener());
recyclerView3.addOnItemTouchListener(getOnItemTouchListener());
}
private void configRecyclerView(RecyclerView recyclerView, MyRecyclerViewAdapter adapter) {
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), recyclerView.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
private RecyclerView.OnItemTouchListener getOnItemTouchListener() {
return new RecyclerView.OnItemTouchListener() {
@SuppressLint(&quot;LongLogTag&quot;)
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
Log.d(&quot;TableView OnswipeTouchListener: &quot;, &quot; recycleViewOdds item onInterceptTouchEvent&quot;);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = ev.getX();
mSwiping = false;
//coordinate for moving
xCoOrdinate = constraintLayout.getX() - ev.getRawX();
break;
case MotionEvent.ACTION_CANCEL:
//return true;
case MotionEvent.ACTION_UP:
if (mSwiping) {
/// this return true block touch event
Log.d(&quot;TableView OnswipeTouchListener:&quot;, &quot;Recycle view odds OnItemTouchListener: you swiped!&quot;);
return true;
}
break;
case MotionEvent.ACTION_MOVE:
float xMove = ev.getX();
float xDelta = Math.abs(xMove - mDownX);
if (xDelta &gt; 5) {
mSwiping = true;
// this one also
return true;
}
break;
}
return false;
}
private Rect rect;
@SuppressLint(&quot;LongLogTag&quot;)
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent event) {
int limitLeft = constraintLayout.getLeft();
int limitRight = constraintLayout.getRight();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
//   Log.d(&quot;OnSwipeTouchListener Class&quot;, &quot;recycleViewOdds action move&quot;);
//Restrict move to constraintLayout width
// Log.d(&quot;Recycleview TouchListener windowwidth: &quot;, limitLeft + &quot;, &quot; + limitRight);
float range = event.getRawX() + xCoOrdinate;
if (Math.abs(range) &gt; limitLeft - 350 &amp;&amp; Math.abs(range) &lt; limitRight - 250) {
//constraintLayout.scrollTo((int) (event.getRawX() + xCoOrdinate),0);
constraintLayout.animate().x(event.getRawX() + xCoOrdinate).setDuration(0).start();
constraintLayout.setAlpha(0.7f);
}
break;
case MotionEvent.ACTION_UP:
Log.d(&quot;TableView OnswipeTouchListener:&quot;, &quot;recycleViewOdds action up&quot;);
constraintLayout.animate().alpha(1).translationX(0).translationY(0);
break;
case MotionEvent.ACTION_CANCEL:
constraintLayout.animate().alpha(1).translationX(0).translationY(0);
Log.d(&quot;TableView OnswipeTouchListener:&quot;, &quot;recycleViewOdds action cancel&quot;);
break;
}
}
@SuppressLint(&quot;LongLogTag&quot;)
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Log.e(&quot;TableView OnswipeTouchListener:&quot;, &quot;recycleViewOdds item touch intercept disallow request&quot;);
}
};
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

&lt;androidx.core.widget.NestedScrollView
android:id=&quot;@+id/scrollview_nested&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;
android:text=&quot;Hello World!&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/firstColumn&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:nestedScrollingEnabled=&quot;false&quot;
app:layout_constraintEnd_toStartOf=&quot;@id/secondColumn&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/secondColumn&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;16dp&quot;
app:layout_constraintEnd_toStartOf=&quot;@id/thirdColumn&quot;
app:layout_constraintLeft_toRightOf=&quot;@id/firstColumn&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/thirdColumn&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;16dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintLeft_toRightOf=&quot;@id/secondColumn&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.core.widget.NestedScrollView&gt;

</androidx.constraintlayout.widget.ConstraintLayout>

list_item_text.xml

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
android:padding=&quot;10dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/text_view&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:textSize=&quot;20sp&quot;
android:layout_marginBottom=&quot;8dp&quot;/&gt;
&lt;ScrollView
android:id=&quot;@+id/nested_scrollview_in_recycleview&quot;
android:layout_width=&quot;match_parent&quot;
android:visibility=&quot;gone&quot;
android:background=&quot;@color/colorAccent&quot;
android:layout_height=&quot;200dp&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recycle_view_nested&quot;
android:layout_width=&quot;match_parent&quot;
android:nestedScrollingEnabled=&quot;false&quot;
android:layout_height=&quot;wrap_content&quot;/&gt;
&lt;/ScrollView&gt;
&lt;/LinearLayout&gt;

答案1

得分: 1

我的建议是在“list_item_text”中,您可以在其中添加一个滚动视图和封装的回收视图,我假设这些项的数量很少,大约在5-10个左右,而且大多数都会显示在屏幕上,所以如果我们通过迭代循环直接将LinearLayout动态添加到父布局中,就不会有任何性能问题。这种方法的唯一缺点是,那些超出屏幕的视图将被加载到内存中(不在回收视图中),但考虑到您的物品数量,这是可以接受的,因为大多数物品都会显示在屏幕上。当然,这将节省回收视图填充、渲染等方面的额外开销。希望这能解决您的触摸问题。

英文:

My suggestion would be in "list_item_text", In this, you have a scroll view and recycler view encapsulated, I am assuming these items will be very less in number around 5-10 and mostly will be visible on the screen, So there won't be any performance issue If we directly add LinearLayout in a parent layout dynamically by iterating a loop, only con of this approach will be that the views which are out of the screen, will be loaded into memory (which is not in recycler view), But considering your number of items, This is accepted, most of your items will be visible on the screen, And obviously It will save an extra overhead of Recyclerview population, rendering and all. Hope It will solve your touch issue.

答案2

得分: 1

recyclerView.setNestedScrollingEnabled(false); 添加到所有的RecyclerView中,然后检查是否解决了滚动的问题。

英文:

Add recyclerView.setNestedScrollingEnabled(false); to all RecyclerViews and check if that solves the problem with scrolling.

答案3

得分: 0

activity_main.xml
请在所有 RecyclerView 标签中添加 android:nestedScrollingEnabled="true"...


**list_item_text.xml**
请在 ScrollView 标签中添加 `android:fillViewport="true"`
然后将 RecyclerView 标签中的 `android:layout_height="wrap_content"` 改为 `android:layout_height="0dp"`...

英文:

activity_main.xml<br>
Please add android:nestedScrollingEnabled=&quot;true&quot; in all RecyclerView Tags...<br>
<hr>
list_item_text.xml<br>
Please add android:fillViewport=&quot;true&quot; in ScrollView Tag<br>
then change android:layout_height=&quot;wrap_content&quot; to android:layout_height=&quot;0dp&quot; in RecyclerView Tag...

huangapple
  • 本文由 发表于 2020年4月10日 02:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61128254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定