我正在使用以下布局在recyclerView中显示项目列表。
但textview
anchorGravity在滚动时从一个角落跳到另一个角落。
滚动后,大多数项目都会被带到原始位置。 为什么会这样?
我该如何解决?
item_shopperitems.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="wrap_content">
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/cyan"
android:elevation="8dp"
android:orientation="horizontal"
android:padding="8dp"
android:weightSum="4">
<LinearLayout
android:id="@+id/layout_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/text_title"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:text="Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach"
android:textColor="@color/tw__composer_white"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text_unit"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:text="100 gms"
android:textColor="@color/tw__composer_white"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/text_quantity"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textColor="@color/tw__composer_white"
android:textSize="16sp"
android:textStyle="bold"
tools:text="10 x" />
<TextView
android:id="@+id/text_singleprice"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:textColor="@color/tw__composer_white"
android:textSize="16sp"
tools:text="₹ 10000.22" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/submit_button"
android:elevation="8dp"
android:paddingBottom="8dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="8dp"
android:text="₹ 10000.22"
android:textColor="@color/tw__composer_white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_anchor="@id/parent"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
ADAPTER
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private ArrayList<DataModel> dataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewVersion;
ImageView imageViewIcon;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
this.textViewVersion = (TextView) itemView.findViewById(R.id.textViewVersion);
}
}
public CustomAdapter(ArrayList<DataModel> data) {
this.dataSet = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_shopperitems, parent, false);
view.setOnClickListener(MainActivity.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView textViewName = holder.textViewName;
TextView textViewVersion = holder.textViewVersion;
textViewName.setText(dataSet.get(listPosition).getName());
textViewVersion.setText(dataSet.get(listPosition).getVersion());
}
@Override
public int getItemCount() {
return dataSet.size();
}
}
注意
如果我有一个简单的线性布局,也没有任何孩子。出现此问题。所以我认为这个问题与CoordinatorLayout
最小布局
<android.support.design.widget.CoordinatorLayout 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="wrap_content">
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="8dp"
android:background="@color/cyan"
android:elevation="8dp"
android:orientation="horizontal"
android:padding="8dp"
android:weightSum="4">
</LinearLayout>
<TextView
android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/submit_button"
android:elevation="8dp"
android:paddingBottom="8dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="8dp"
android:text="₹ 10000.22"
android:textColor="@color/tw__composer_white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_anchor="@id/parent"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="wrap_content">
<LinearLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/cyan"
android:elevation="8dp"
android:orientation="horizontal"
android:padding="8dp"
android:weightSum="4">
<LinearLayout
android:id="@+id/layout_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/text_title"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:text="Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach Spinach"
android:textColor="@color/tw__composer_white"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text_unit"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:text="100 gms"
android:textColor="@color/tw__composer_white"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/text_quantity"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textColor="@color/tw__composer_white"
android:textSize="16sp"
android:textStyle="bold"
tools:text="10 x" />
<TextView
android:id="@+id/text_singleprice"
fontPath="fonts/Sofia-Pro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp"
android:textColor="@color/tw__composer_white"
android:textSize="16sp"
tools:text="₹ 10000.22" />
<TextView
android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/submit_button"
android:elevation="8dp"
android:paddingBottom="8dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="8dp"
android:text="₹ 10000.22"
android:textColor="@color/tw__composer_white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_anchor="@id/parent"
app:layout_anchorGravity="bottom|end" />
</LinearLayout>
</LinearLayout>