下面附有模拟器的屏幕截图。
最初,当设置运行应用程序时,布局会失真。
仅在滚动项目后才能正确布局。
似乎只有在视图被回收之后,项目的位置才会知道。我该如何解决这个问题?
如果有人能帮助我,我会很高兴。我还能在哪里找到回收者视图实施的例子。
RecyclerViewAdapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<RecyclerViewClass1> mItems;
Context mContext;
public RecyclerViewAdapter(Context mContext,ArrayList<RecyclerViewClass1> mItems ) {
this.mItems = mItems;
this.mContext = mContext;
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext)
.inflate(R.layout.topfrag11, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
holder.binddata(mItems.get(position));
}
@Override
public int getItemCount() {
return mItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView mtext2, mtext3;
//public LinearLayout mLL2;
public TextView mtext1,mtext6;
//public LinearLayout mLL1;
public TextView mtext4, mtext5;
//public LinearLayout mLL3;
private Context mContext;
public ViewHolder(View itemView) {
super(itemView);
mtext2 = (TextView) itemView.findViewById(R.id.textView2);
mtext3 = (TextView) itemView.findViewById(R.id.textView3);
//mLL2 = (LinearLayout) itemView.findViewById(R.id.LL2);
mtext1 = (TextView)itemView.findViewById(R.id.textView1);
mtext6 = (TextView)itemView.findViewById(R.id.textView6);
//mLL1 = (LinearLayout)itemView.findViewById(R.id.LL1);
mtext4 = (TextView) itemView.findViewById(R.id.textView4);
mtext5 = (TextView) itemView.findViewById(R.id.textView5);
//mLL3 = (LinearLayout) itemView.findViewById(R.id.LL3);
//mContext = itemView.getContext();
}
public void binddata(RecyclerViewClass1 recyclerViewClass1)
{
mtext1.setText(recyclerViewClass1.getText1());
mtext2.setText(recyclerViewClass1.getText2());
mtext3.setText(recyclerViewClass1.getText3());
mtext4.setText(recyclerViewClass1.getText4());
mtext5.setText(recyclerViewClass1.getText5());
mtext6.setText(recyclerViewClass1.getText6());
}
}
}
这就是我设置的方式
public class topfragtab1 extends Fragment {
private static final String TAG = "Tab1Fragment";
// private RecyclerView mRecyclerView ;
//private RecyclerViewAdapter mRecyclerViewAdapter;
private ArrayList<RecyclerViewClass1> mItems;
//private RecyclerView.LayoutManager mlayoutManager;
//Context mContext;
//FavEditBtnRVA adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.topfrag1,container,false);
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview1);
//mContext = this.getContext();
//adapter = new FavEditBtnRVA();
FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.editbtn);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent openeditfav = new Intent(getActivity(),faveditbtn.class);
startActivity(openeditfav);
}
});
mItems = new ArrayList<>();
for(int i=0;i<100;i++){
mItems.add(new RecyclerViewClass1(i+"string1",i+"string2",i+"string3",i+"string4",i+"string5",i+"string6"));
}
RecyclerViewAdapter mRecyclerViewAdapter = new RecyclerViewAdapter(getActivity(),mItems);
recyclerView.setAdapter(mRecyclerViewAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
return view;
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/editbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:src="@drawable/ic_floatedit"
app:backgroundTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
修改后的XML文件
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/editbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:src="@drawable/ic_floatedit"
app:backgroundTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>