gpt4 book ai didi

android - 如何不回收我的 recyclerview 或任何项目的前 2 个 View ?

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:18 39 4
gpt4 key购买 nike

我正在使用 recyclerview 来显示和播放用户的视频。但是,当我滚动浏览回收器 View 时,我看到了我的第一个 View ,其中我的视频被回收了,因此其他用户看不到它。有没有一种方法可以确保第一个 View 不被回收,这样我就不必担心每次滚动列表时我的视频 View 都会被重新回收?

这是我的代码:在我的 fragment 中......

 <android.support.v7.widget.RecyclerView
android:id="@+id/videoList"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/myButtonContainer"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/myoldContainer">

...

和相应的适配器...

public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.myViewHolder> {

private CopyOnWriteArrayList<Person> persons;
private Context mContext;

public GroupAdapter(@NonNull final CopyOnWriteArrayList<Person> persons , Context context) {
this.persons = persons;
this.mContext= context;

for (Person person : this.persons) {
//get names
}
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_person, parent, false);
final MyViewHolder viewHolder = new MyViewHolder(layout);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {



final Person person = person.get(position);
final Participant participant = person.getParticipant();


if (person.getName() != null) {
holder.personName.setText(person.getName());
}

if (person.getImage() != null) {
holder.personImage.setImageBitmap(person.getImage());
} else {
holder.personImage.setImageResource(R.drawable.default_profile);
}
holder.personImage.setVisibility(View.INVISIBLE);
holder.personImage.setVisibility(View.VISIBLE);

final VideoView videoView;
if (participant.isMe) {
videoView = participant.videoStreamer.videoView;
} else {
videoView = participant.videoPlayer.videoView;
}
if (holder.personVideo.getChildCount() != 0) {
holder.personVideo.removeAllViews();

}
if (videoView.getParent() != null) {
ViewGroup parent = (ViewGroup) videoView.getParent();
parent.removeView(videoView);

}
holder.personVideo.addView(videoView, myViewHolder.videoLayoutParams);

if (person.isVideoPaused()) {
holder.personVideo.setVisibility(View.INVISIBLE);
holder.personImage.setVisibility(View.VISIBLE);
} else {

holder.personVideo.setVisibility(View.VISIBLE);
holder.personImage.setVisibility(View.INVISIBLE);
}


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

public static final class MyViewHolder extends RecyclerView.ViewHolder {

@BindView(R.id.personVideo)
public ViewGroup personVideo;
@BindView(R.id.personImage)
public ImageView personImage;
@BindView(R.id.personName)
public TextView personName;

protected static FrameLayout.LayoutParams videoLayoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);

public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}

}
}

这是我在 fragment 中设置它的方式:

  LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
videoAdapter = new VideoAdapter(myHelper.getPeople(), getContext());
videoList.setLayoutManager(manager);
videoList.setAdapter(videoAdapter);

item_person:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="@drawable/person_border"
xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
android:id="@+id/personContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent">

<FrameLayout
android:id="@+id/personVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" />

<ImageView
android:id="@+id/personImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:src="@drawable/default_profile" />

<TextView
android:id="@+id/personName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#AA555555"
android:gravity="center_horizontal|bottom"
android:textColor="@color/green"
android:textSize="12sp"
android:lines="1"
android:ellipsize="end"
tools:text="androiduser@gmail.com"
android:layout_alignParentBottom="true" />

</RelativeLayout>




</android.support.constraint.ConstraintLayout>

带有回收 View 的 fragment :xml

<android.support.constraint.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">
<RelativeLayout>
....
</RelativeLayout>
<include containers>...</include>
...
<android.support.v7.widget.RecyclerView
android:id="@+id/personList"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"
>

</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

最佳答案

我不认为“不回收”视频 View 实际上会阻止它在您滚动时被破坏。问题不在于回收,而在于 View 解除绑定(bind)。

我认为像 VideoView 这样复杂的组件根本不应该放在 RecyclerView 中。您可以尝试将它作为静态内容添加到顶部,这很可能将解决问题。您可以为此使用 NestedScrollView。看这里:Recyclerview inside scrollview- How to scroll whole content?

如果您仍然认为要将其保留在 RecyclerView 中并禁用回收,请执行以下操作。

为您的视频 View 项目创建一个单独的 View 类型。这是一个例子: https://stackoverflow.com/a/26573338/3086818 .将您的视频 View 项视为示例中的标题 View 。

执行此操作后,将有一个 RecycledViewPool 类管理 RecyclerView 中项目的回收。您可以告诉它哪些 View 要回收,哪些不回收。默认情况下,它会回收所有 View 。要禁用视频 View 项目的回收,请使用您的新 View 类型,如下所示:

recyclerView.getRecycledViewPool().setMaxRecycledViews(TYPE_VIDEO_VIEW, 0);

其中 TYPE_VIDEO_VIEW 是您使用上一个示例创建的新类型。数字 0 告诉 RecyclerView 要回收多少项目 - 在本例中为 0,表示“不回收”。此处有更多相关信息:https://stackoverflow.com/a/36313437/3086818 .

希望这对您有所帮助...祝您好运!

关于android - 如何不回收我的 recyclerview 或任何项目的前 2 个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50355507/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com