简体   繁体   English

如何获取当前在回收站视图的线性布局中不可见的视图

[英]How to get the views which are not visible currently in the linear layout of the recycler view

I was designing an Instagram story type template.我正在设计一个 Instagram 故事类型模板。 I am stuck at a very weird problem.我陷入了一个非常奇怪的问题。 I have used recycler view in the main activity.我在主要活动中使用了回收站视图。

MainActivity: (This is just the layout shown. I have change the orientation to Horizontal). MainActivity:(这只是显示的布局。我已将方向更改为水平)。

在此处输入图像描述

My layout:我的布局:

在此处输入图像描述

Then I have designed a custom adapter and in the layout I have used a linearLayout.然后我设计了一个自定义适配器,并在布局中使用了线性布局。 When clicked on each view It opens a new Activity which shows the whole story content.单击每个视图时,它会打开一个显示整个故事内容的新活动。

Just like in Instagram when a user opens any story, the user can click on the right side of the screen to get to the next story, or left of the screen to get to the previous one.就像在 Instagram 中,当用户打开任何故事时,用户可以点击屏幕右侧进入下一个故事,或者点击屏幕左侧进入上一个故事。 I tried to implement this same functionality.我试图实现同样的功能。 Opening the story was implemented successfully.开启剧情成功实施。 The problem comes when I added the functionality of right and left click on the screen.当我在屏幕上添加右击和左击的功能时,问题就来了。 I added two button;我添加了两个按钮; one to the right and one to the left.一个在右边,一个在左边。 The problem is like, if there are currently 3 views visible, then I can navigate in between these stories only and not to the stories which are not visible in the screen because of the recycler view.问题是,如果当前有 3 个视图可见,那么我只能在这些故事之间导航,而不能导航到由于回收站视图而在屏幕上不可见的故事。

The below code is for right and left clicks下面的代码用于左右点击

leftArrow = findViewById(R.id.leftArrow);
    leftArrow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(myAdapter.currentPosition - 1 >= 0) {
                int firstVis = MainActivity.linearLayoutManager.findFirstVisibleItemPosition();
                MainActivity.linearLayoutManager.scrollToPosition(firstVis - 1);
                rightOrLeftClicks(myAdapter.currentPosition - 1);
            }
        }
    });

    rightArrow = findViewById(R.id.rightArrow);
    rightArrow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(myAdapter.currentPosition + 1 < myAdapter.localDataSet.length) {
                int lastVis = MainActivity.linearLayoutManager.findLastVisibleItemPosition();
                MainActivity.linearLayoutManager.scrollToPosition(lastVis + 1);
                rightOrLeftClicks(myAdapter.currentPosition + 1);
            }
        }
    });
}

Below code is of the function rightOrLeftClicks下面的代码是 function rightOrLeftClicks

public void rightOrLeftClicks(int position) {
    finish();
    Log.d("rl", "working");
    nextView = MainActivity.linearLayoutManager.getChildAt(position);
    Log.d("ll", "The last element is " + MainActivity.linearLayoutManager.findLastVisibleItemPosition());
    if(nextView != null)myAdapter.onClickView(position, myAdapter.localDataSet[position].symptom, nextView);
}

Below code is for onClickView , It is same code for clicking any view (Story) or clicking the right or left buttons.下面的代码用于onClickView ,它与单击任何视图(故事)或单击左右按钮的代码相同。 Basically I just opened the another activity by passing an intent with the next or previous view I obtained when clicked on right or left respectively.基本上,我只是通过传递一个意图来打开另一个活动,其中包含我分别单击右侧或左侧时获得的下一个或上一个视图。

public static void onClickView(int position, String element, View v) {
    Intent intent = new Intent(v.getContext(), MainActivity2.class);
    idPosition = "";
    idPosition = element;
    ArrayList<String> passingContent = new ArrayList<>();
    currentPosition = position;
    passingContent.add(localDataSet[position].description);
    passingContent.add(localDataSet[position].imageUrl);
    intent.putExtra(element + "", passingContent);
    v.getContext().startActivity(intent);
}

The problem is it only gives the visible views to me.问题是它只给我可见的视图。 Not the rest of the views.不是rest的意见。 I tried auto scrolling so that the next or the previous view become visible but it doesn't seems to work.我尝试了自动滚动,以便下一个或上一个视图变得可见,但它似乎不起作用。 The last visible position of the child remains the same and it return a null view to me when I try to open the view just after the last visible view.孩子的最后一个可见 position 保持不变,当我尝试在最后一个可见视图之后打开视图时,它返回一个 null 视图给我。

在此处输入图像描述

Assume there are 5 stories.假设有 5 个故事。 Then I will see only 3 stories in the screen the rest can be seen by scrolling.然后我将在屏幕上只看到 3 个故事,通过滚动可以看到 rest。 When I click any of the stories.当我点击任何故事时。 I can only navigate between 1,2 and 3. When I try to click right from story 3 to see the story 4. The current story is killed by finish() function and null is returned in the nextView variable because of which the story 4 is not loaded and I am returned to the MainActivity.我只能在 1,2 和 3 之间导航。当我尝试从故事 3 中单击以查看故事 4 时。当前故事被finish() function 和nullnextView变量中返回,因此故事 4未加载,我返回到 MainActivity。

I have to navigate to all stories present in my recycler view.我必须导航到我的回收站视图中存在的所有故事。

Do these changes may help:做这些改变可能会有所帮助:

1/ I think you should use ViewPager . 1/我认为你应该使用ViewPager It automatically snaps to the item without scrollTo to index.它会自动捕捉到没有 scrollTo 索引的项目。 Or keep using RecyclerView with the help of SnapHelper或者在SnapHelper的帮助下继续使用RecyclerView

2/ Modify onClickView method not to be static, and don't need View v to work. 2/修改onClickView方法不是static,不需要View v工作。 I see you just need View v just for the context.我看到你只需要View v就可以了。 Why not just pass the context to that method.为什么不直接将context传递给该方法。 It's not proper to use a parameter like that, and that approach traps you into 3/ problem.使用这样的参数是不合适的,这种方法会使您陷入 3/ 问题。

3/ 3/

nextView = MainActivity.linearLayoutManager.getChildAt(position);

You already know how RecyclerView works, just avoid using getChildAt because in some cases, the child you want hasn't been created yet.你已经知道 RecyclerView 是如何工作的,只是避免使用getChildAt因为在某些情况下,你想要的孩子还没有被创建。 Base on your code, I think you don't even need to get the nextView根据您的代码,我认为您甚至不需要获取nextView

As told by Tam Huynh.正如 Tam Huynh 所说。 The 1st point helped me in getting the new views in the linear layout.第一点帮助我在线性布局中获得新视图。 The views were working the fine.这些意见运作良好。

There was a problem in the parameter of the function getChildAt . function getChildAt的参数有问题。 I have to pass the same position I was in previously.我必须通过与之前相同的 position。 Because relatively the position(index) will not change for the child views.因为子视图的位置(索引)相对不会改变。

index 0 -> story number 1索引 0 -> 故事编号 1

index 1 -> story number 2索引 1 -> 故事编号 2

index 2 -> story number 3索引 2 -> 故事编号 3

Like if there were story number 1,2 and 3 visible, the index 0 will contain 1st story, index 1 will contain 2nd and index 2 will contain 3rd story.就像如果有 1,2 和 3 层可见,索引 0 将包含第 1 层,索引 1 将包含第 2 层,索引 2 将包含第 3 层。 when user clicked the right button on the 3rd story.当用户单击第三个故事的右键时。 The 4th story is first visible and now the screen have 2,3 and 4 visible.第 4 个故事首先可见,现在屏幕有 2,3 和 4 可见。 but the index of the 4th story will remain 3rd only.但第 4 层的索引将仅保持在第 3 层。 As now indexing will be like现在索引就像

index 0 -> story number 2索引 0 -> 故事编号 2

index 1 -> story number 3索引 1 -> 故事编号 3

index 2 -> story number 4索引 2 -> 故事编号 4

So, instead of passing position as parameter, currentPosition should be passed in getChildAt因此,不应将position作为参数传递,而应在getChildAt中传递currentPosition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM