Android Recyclerview项目在上下滚动时受到干扰
在Android Recyclerview项目在上下滚动时受到干扰,android,scroll,android-recyclerview,Android,Scroll,Android Recyclerview,在recyclerview中,当我向上或向下滚动时,项目会变得一团糟。这意味着它会再次回收项目视图。请有人帮帮我,我已经试过很多次了。提前谢谢 以下是该问题的截图 当我上下滚动时,文本视图1(图像中标记为红色)出现在错误的位置 图1: 图2: 问题在于,如果(customerId!=mpendingfollow.getCustomerId())您编写了逻辑,认为它只能向下滚动。如果您可以通过存储以前的客户id来明确您想要实现的目标,那么解决您的问题将变得更容易这意味着。。如果已经插入了客户id,
recyclerview
中,当我向上或向下滚动时,项目会变得一团糟。这意味着它会再次回收项目视图。请有人帮帮我,我已经试过很多次了。提前谢谢
以下是该问题的截图
当我上下滚动时,文本视图1(图像中标记为红色)出现在错误的位置
图1:图2:
问题在于,如果(customerId!=mpendingfollow.getCustomerId())您编写了逻辑,认为它只能向下滚动。如果您可以通过存储以前的客户id来明确您想要实现的目标,那么解决您的问题将变得更容易这意味着。。如果已经插入了客户id,则我希望使线性布局不可见或隐藏。问题在于,如果(customerId!=mpendingfollow.getCustomerId())您编写了逻辑,认为它只能向下滚动。如果您可以通过存储以前的客户id来明确您想要实现的目标,那么解决您的问题将变得更容易这意味着。。如果已插入客户id,则我希望使线性布局不可见或隐藏。
private class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<PendingFollowupVO> mPendingFollowupVO;
public MyRecyclerViewAdapter(List<PendingFollowupVO> mpendingfollow) {
mPendingFollowupVO = mpendingfollow;
setHasStableIds(true);
}
//A ViewHolder object stores each of the component views inside the tag field of the Layout,
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_pending_follow_up, parent, false);
ViewHolder viewHolder=new ViewHolder(view);
//ViewDataBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.layout_pending_follow_up,parent,false);
return viewHolder;
}
// It will specified the postion of the elements
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//ViewDataBinding viewDataBinding=holder.getViewDataBinding;
//List<String> newList = new ArrayList<String>(new HashSet<String>(customerIdlist));
final PendingFollowupVO mpendingfollow = mPendingFollowupVO.get(position);
holder.linearLayout.setTag(mPendingFollowupVO.get(position));
holder.bal.setText(null);
holder.invam.setText(null);
holder.customerName.setText(null);
holder.promised.setText(null);
holder.invpfu.setText(null);
if (holder != null) {
setValue(mpendingfollow.getCustomerName(),mpendingfollow.getCustomerId(), holder.linearLayout, holder.customerName);
holder.invpfu.setText(mpendingfollow.getInvoiceNo());
holder.promised.setText(mpendingfollow.getPromisedDate());
holder.due.setText(mpendingfollow.getDueDate());
holder.invam.setText(CashPunditUtils.formatAmount(mpendingfollow.getInvAmount()));
holder.bal.setText(CashPunditUtils.formatAmount(mpendingfollow.getBalAmount()));
}
}
void setValue(String cnam,long mpendingfollow,LinearLayout linearLayout,TextView cn) {
if (customerId!=(mpendingfollow)) {
showViews(linearLayout, cn,cnam);
cn.setText(cnam);
customerId=mpendingfollow;
}
else
hideViews(linearLayout, cn);
}
private void showViews(LinearLayout layout, View seperator,String cnn) {
layout.setVisibility(View.VISIBLE);
seperator.setVisibility(View.VISIBLE);
}
private void hideViews(LinearLayout row, View seperator) {
row.setVisibility(View.GONE); // if there is a empty space change it with View.GONE
seperator.setVisibility(View.GONE);
}
@Override
public int getItemCount() {
return mPendingFollowupVO.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public int position;
protected TextView customerName, invpfu, due, promised, invam, bal;
LinearLayout linearLayout;
public ViewHolder(View view) {
super(view);
this.linearLayout = (LinearLayout) itemView.findViewById(R.id.head3);
this.customerName = (TextView) itemView.findViewById(R.id.customernamepfu);
this.invpfu = (TextView) itemView.findViewById(R.id.inv);
this.due = (TextView) itemView.findViewById(R.id.due2);
this.promised = (TextView) itemView.findViewById(R.id.prom);
this.invam = (TextView) itemView.findViewById(R.id.over);
this.bal = (TextView) itemView.findViewById(R.id.bal);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mFragmentActionListener.setFragmentTitle("Communication");
CommunicationFragment fragment = new CommunicationFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.home_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Toast.makeText(getActivity(),"ooooo",Toast.LENGTH_LONG).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#FFDFDFDF"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="@+id/head3"
android:layout_marginTop="3dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="@{mPendingFollowupVO.customerName}"
android:background="#2F75B5"
android:textColor="#ffff"
android:textSize="14dp"
android:id="@+id/customernamepfu"
android:textAlignment="center"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_below="@+id/head3"
android:id="@+id/head4"
android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="13dp"
android:text="Invoice#"
android:layout_marginRight="1dp"
android:background="#b1c1da"
android:textColor="#000"
android:textAlignment="center"/>
<TextView
android:layout_width="80dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="13dp"
android:layout_marginRight="1dp"
android:text="Due On"
android:background="#b1c1da"
android:textColor="#000"
android:textAlignment="center"/>
<TextView
android:layout_width="85dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="13dp"
android:text="Promised"
android:layout_marginRight="1dp"
android:background="#b1c1da"
android:textColor="#000"
android:textAlignment="center"/>
<TextView
android:layout_width="70dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="13dp"
android:text="Inv amount"
android:layout_marginRight="1dp"
android:background="#b1c1da"
android:textColor="#000"
android:textAlignment="center"
android:id="@+id/textView" />
<TextView
android:layout_width="75dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="13dp"
android:text="Balance "
android:background="#b1c1da"
android:textColor="#000"
android:textAlignment="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/head5"
android:layout_below="@+id/head4"
android:layout_marginBottom="1dp"
android:layout_marginTop="0dp"
>
<TextView
android:layout_width="65dp"
android:layout_height="25dp"
android:text="@{mPendingFollowupVO.invoiceNo}"
android:id="@+id/inv"
android:paddingTop="5dp"
android:textSize="12dp"
android:layout_marginRight="2dp"
android:background="#ffff"
android:textColor="#000"
android:textAlignment="center"/>
<TextView
android:layout_width="80dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="12dp"
android:id="@+id/due2"
android:text="@{mPendingFollowupVO.dueDate}"
android:layout_marginRight="1dp"
android:background="#ffff"
android:textColor="#000"
android:textAlignment="center"/>
<TextView
android:layout_width="85dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="12dp"
android:id="@+id/prom"
android:layout_marginRight="1dp"
android:text="@{mPendingFollowupVO.promisedDate}"
android:background="#ffff"
android:textColor="#000"
android:textAlignment="center"/>
<TextView
android:layout_width="70dp"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="12dp"
android:text="@{Double.toString(mPendingFollowupVO.invAmount)}"
android:id="@+id/bal"
android:layout_marginRight="1dp"
android:background="#ffff"
android:textColor="#000"
android:textAlignment="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="25dp"
android:paddingTop="5dp"
android:textSize="12dp"
android:id="@+id/over"
android:text="@{Double.toString(mPendingFollowupVO.balAmount)}"
android:background="#ffff"
android:textColor="#000"
android:textAlignment="center"/>
</LinearLayout>
</RelativeLayout>
public class PendingFollowupVO extends BaseObservable
{
private String CustomerName;
private long CustomerId;
private String InvoiceNo;
private String DueDate;
private String PromisedDate;
private double InvAmount;
private double BalAmount;
public PendingFollowupVO() {
super();
}
public long getCustomerId() {
return CustomerId;
}
public void setCustomerId(long customerId) {
CustomerId = customerId;
}
public double getInvAmount() {
return InvAmount;
}
public void setInvAmount(double invAmount) {
InvAmount = invAmount;
}
public String getCustomerName() {
return CustomerName;
}
public void setCustomerName(String customerName) {
CustomerName = customerName;
}
public String getInvoiceNo() {
return InvoiceNo;
}
public void setInvoiceNo(String invoiceNo) {
InvoiceNo = invoiceNo;
}
public String getDueDate() {
return DueDate;
}
public void setDueDate(String dueDate) {
DueDate = dueDate;
}
public String getPromisedDate() {
return PromisedDate;
}
public void setPromisedDate(String promisedDate) {
PromisedDate = promisedDate;
}
public double getBalAmount() {
return BalAmount;
}
public void setBalAmount(double balAmount) {
BalAmount = balAmount;
}
}