我在活动中使用回收者视图显示某些项目的片段,项目在首次加载时显示正常,但当设备进入睡眠状态并在某段时间后恢复时,项目重叠超过上一个项目列表。
首次装载回收器时看起来很好:
但是当设备在不活动并恢复后进入休眠状态时,我可以看到回收器视图有2个列表(副本),一个在后台,一个在前台!滚动时很明显!
我的片段类是:
public class TablesFragment extends Fragment implements AsyncTaskCompleteListener<String> {
RecyclerView mTablesRecyclerView;
Context mContext;
ConnectivityManager mConnManager;
TableListAdapter mTableListAdapter;
TakeAwayOrderAdapter mTakeAwayListAdapter;
public static List<Table> mTableList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tables_pane_layout,container,false);
mTablesRecyclerView = view.findViewById(R.id.tablesListRecycler);
mContext = getActivity();
String urlString="http://"+MainActivity.IP_ADDRESS+"/Central_Webapp/TablesServlet";
ServerCalls serverCalls = new ServerCalls(mContext,TablesActivity.mProgressBar,this);
serverCalls.execute(urlString,"GET");
return view;
}
@Override
public void onTaskComplete(String result) {
if(isAdded())
{
if(result==null||result.isEmpty())
{
return;
}
try {
JSONArray jsonArray =new JSONArray(result);
mTableList = TableParser.getTableListFromJSONArray(jsonArray);
clearOrderPreferences(mTableList);
Table t = new Table();
t.setTableName(SharedStrings.addNew);
mTableList.add(t);
mTableListAdapter = new TableListAdapter(mTableList,this,mContext);
mTablesRecyclerView.setAdapter(mTableListAdapter);
mTablesRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
片段xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tablesListRecycler"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
Recycler item XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tableListItemLayoutID"
android:padding="10dp"
>
<TextView
android:id="@+id/tableNameTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:textStyle="bold"
android:text="TextView"
android:layout_marginLeft="10dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
/>
<TextView
android:id="@+id/tableCapacityTextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:gravity="bottom|right"
/>
</LinearLayout>