Recyclerview重绘本身导航到选项卡式活动中的另一个回收站视图

时间:2017-09-15 17:32:13

标签: android android-recyclerview android-viewpager

所以这是我在第一个片段

中的onCreateView中运行的AsyncTask
class GetDatas extends AsyncTask<Void, Void, Void>
{

    Collection<ProfileDataModel> enums = Collections.EMPTY_LIST;
    @Override
    protected Void doInBackground(Void... voids) {
        try{
            client = new OkHttpClient();
            String response = OkHttpService.GET(client, webUrl);
            Gson gson = new Gson();
            Type type = new TypeToken<Collection<ProfileDataModel>>(){}.getType();
            enums = gson.fromJson(response, type);
            profileDataModels = enums.toArray(new ProfileDataModel[enums.size()]);
            listAdapter = new ListAdapter(Arrays.asList(profileDataModels), getContext());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        profileDataModels = enums.toArray(new ProfileDataModel[enums.size()]);
        listAdapter = new ListAdapter(Arrays.asList(profileDataModels), getContext());
        recyclerView.setAdapter(listAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    }
}

在第三个片段中,我使用不需要提取的虚拟数据

public class MainFragment extends Fragment {
RecyclerView recyclerView;
LinearLayout linearLayout;


@Override
public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_video_list_pages, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.video_list_recycler_view);
    recyclerView.setHasFixedSize(true);

    linearLayout = (LinearLayout) rootView.findViewById(R.id.video_list_adapter_rootframe);

    ListAdapter adapter = new ListAdapter(
            new String[] {
                    "mm","aa","sss","mm","aa","sss","mm","aa","sss","mm","aa","sss","mm","aa","sss"

            }
    );
    recyclerView.setAdapter(adapter);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);


    return rootView;
}

}

你们中的一些人可能会对命名感到困惑,但我喜欢这样做, My Directory names

问题在于,当我从第一个片段滑动到第三个片段时,循环视图会重绘自身。当我转到仅包含textview的第二个片段时,这不会发生 有人可以解释为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

好的是,当你转到第三个片段时,片段一个被销毁,所以要停止此功能,你必须覆盖destroyItem并保持它是空白的,在您的Activity类中使用它

 @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }

并且您的问题将得到解决,下次在发布问题之前尝试通过课程的所有方法
假设你在Tabs中有4个片段它将只保留2并且为了节省你的内存而销毁休息,如果你有3个标签,如果你有下一个和前一个标签如2号,你也可以保留3,你可以通过保持destroyItem为空来覆盖它干杯!

  

如果您滚动10-20个标签,您的应用程序可能会崩溃,请不要尝试实施